Cannot read characteristic. Android BLE

帅比萌擦擦* 提交于 2019-12-08 20:45:43

问题


I'd like to read the data from a specific characteristic of my remote BLE device to my Android tablet Nexus 7.

The problem is that, I can receive the data by enabling the notification of that characteristic even without calling readCharacteristic. But I cannot successfully read characteristic by calling readCharacteristicwithout enabling the notification.

mBluetoothGatt.readCharacteristic(characteristic) returns false. Thus the function onCharacteristicRead has never been triggered. I also checked the property value BluetoothGattCharacteristic.PROPERTY_READ, it is 30.

Does anyone have some ideas about what is going on here? I really need to read the characteristic separately. Because if I only analysis data based on notification, I can not figure out where the data begins. This is because that my device will send 12bytes each time. And it will continuously sending the byte array. However, the notification would bring me the data one byte a time. So I don't know which is the beginning byte of the byte array.

I'm using the sample code provided by Android right now.

Here is the snippet:

public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }

    boolean status = mBluetoothGatt.readCharacteristic(characteristic);
    System.out.println("Initialize reading process status:" + status);

}



public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                          boolean enabled) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

    // This is specific to Heart Rate Measurement.
    if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
                UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); 

        mBluetoothGatt.writeDescriptor(descriptor);
    }
}

The code in callback is:

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt,
                                     BluetoothGattCharacteristic characteristic,
                                     int status) {

        System.out.println("In onCharacteristicRead!!!!!!!");
        if (status == BluetoothGatt.GATT_SUCCESS) {

            broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
            System.out.println("Received Data Success!!!!!!");
        }
    }

I have read through the document of reading characteristic, but nothing helps. Can anyone help me? Thank you very much!


回答1:


you need first enable notifications for characteristic and then try to read it's value and remember to get the appropiate format of the byte array that return characteristic.getValue() method. Regards



来源:https://stackoverflow.com/questions/22671473/cannot-read-characteristic-android-ble

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!