Android:如何读取自定义GATT特征的值?

时间:2017-10-12 15:23:14

标签: java android bluetooth bluetooth-lowenergy gatt

我正在尝试从运行Mynewt的设备中读取自定义特征的数据。

直到我发现所有服务一切正常。

这是我的代码的一部分。

@Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        super.onServicesDiscovered(gatt, status);
        if (status == 0){
            List<BluetoothGattService> services = gatt.getServices();
            peripheralTextView.append("onServicesDiscovered " + services.toString() + "\n\n");
            readCustomCharacteristic();
        }
    }

    public void readCustomCharacteristic() {
        if (btAdapter == null || mGatt == null) {
            peripheralTextView.append("BluetoothAdapter not initialized \n");
            return;
        }
    /*check if the service is available on the device*/
        BluetoothGattService mCustomService = mGatt.getService(UUID.fromString("59462F12-9543-9999-12C8-58B459A2712D"));
        if(mCustomService == null){
            peripheralTextView.append("Custom BLE Service not found \n");
            return;
        }
    /*get the read characteristic from the service*/
        BluetoothGattCharacteristic mReadCharacteristic = mCustomService.getCharacteristic(UUID.fromString("5C3A659E-897E-45E1-B016-007107C96DF6"));
        mGatt.readCharacteristic(mReadCharacteristic);

        if(mReadCharacteristic == null){
            peripheralTextView.append("Failed to read characteristic \n");
        }
        else {
            peripheralTextView.append("Characteristic " + mGatt.getService(serviceUUID).getCharacteristic(characteristicUUID).toString() + "\n");
        }
        return;
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        peripheralTextView.append("The characteristic UUID is : " + characteristic.getUuid());
        peripheralTextView.append("The value is : " + characteristic.getStringValue(0));
    }

我还尝试过其他方法来读取数据,例如valueOf,getValue等。

使用其中一些方法,我得到的答案为null,而其他一些方法甚至不会在我的屏幕上打印整行。

我的手机屏幕输出就是这个 Screenshot

我总是在手机上运行我的应用程序:三星Galaxy A5(2016)

是否有其他方法/方法可以从特征中读取数据或者我是否遗漏了某些内容?

提前致谢

编辑: Screenshot for status parameter behavior 使用getValue方法

0 个答案:

没有答案
相关问题