I´m developing an BLE app, based on the Gatt sample project provided by google: https://developer.android.com/samples/BluetoothLeGatt/index.html. So, I can send data writing in a characteristic successfully. Now I need to know when this characteristic change its value.
DeviceActivity
private void displayGattServices(List<BluetoothGattService> gattServices)
{
// get services & characteristics
................
final BluetoothGattCharacteristic characteristic = mGattCharacteristics.get(2).get(0);
final int charaProp = characteristic.getProperties();
mWriteCharacteristic = characteristic;
mBluetoothLeService.setCharacteristicNotification(mWriteCharacteristic, true);
// write in the characteristic to send a reset command to BLE device
// Start the read method, that permit subscribe to the characteristic
BluetoothLeService.read(mWriteCharacteristic);
BluetoothLeService.set(mWriteCharacteristic,true);
};
BluetoothLeService
public static void read(BluetoothGattCharacteristic characteristic)
{
if (mBluetoothAdapter == null || mBluetoothGatt == null)
{
Log.w("BluetoothAdapter not initialized");
return;
};
mBluetoothGatt.readCharacteristic(characteristic);
};
public static void set(BluetoothGattCharacteristic characteristic, boolean enabled)
{
if(mBluetoothAdapter == null || mBluetoothGatt == null)
{
Log.w("BluetoothAdapter not initialized");
return;
};
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
UUID uuid = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(uuid);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
};
But I dont know how can I catch the value of the read characteristic. Actually, I dont know if my code subscribe succesful to the characteristic or not. Some one can help me? How can I check if the value of the characteristic change really? And how can I display the new value of this characteristic in the screen? Is correct my code or I need add, modify or remove something? Thaks in advance. I guide by this question: Reading multiple characteristics from a BLE device synchronously (Recommended Method for Android)