Reputation: 4258
I am working on a BLE device. - I am able to connect this device with app Using BluetoothGATT - I am able to read all services, characteristic and advertiser data first time as soon as connect with BluetoothGatt. - We have two characteristics one we need to read and another one for writing, I am able to write characteristic.
But The issue it I am not able to read characteristic after write. As per my understanding
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic)
or
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status)
Should automatically called, these are Callback method of
My Characteristic Write code is
boolean status = mBluetoothGatt.setCharacteristicNotification(characteristic, state);
I am also enabling notification by below code
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
I am stuck on Reading characteristics part after writing
Any help appreciatable, Thank In Advance dear, I am stuck for 3 days on this point.
Upvotes: 1
Views: 2194
Reputation: 29
onCharacteristicRead() and onCharacteristicChanged() are not automatically called.
onCharacteristicRead is triggered when you call characteristic.getValue(). Something like this:
BluetoothGattCharacteristic charac = ... ;
byte[] messageBytes = charac.getValue();
Where charac holds the characteristic you're trying to read and messageBytes holds the value read from the characteristic.
onCharacteristicChanged is called once you enable notifications. Looks like you're not enabling notifications completely.
public static String CLIENT_CONFIGURATION_DESCRIPTOR_STRING = "00002902-0000-1000-8000-00805f9b34fb";
...
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
UUID.fromString(CLIENT_CONFIGURATION_DESCRIPTOR_STRING));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothgatt.writeDescriptor(descriptor);
You need to add a writeDescriptor call to successfully enable notifications.
Upvotes: 2