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 readCharacteristic
without 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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With