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)
You need to run a loop inside this function - displayGattServices(List gattServices) and get the entire services and characteristics of your connected BLE device.
For determining characteristics name and properties based on the UUID value you can refer to BLE characteristics
Once you have determined which characteristics is applicable for your BLE device, add it in a Queue and read/set them.
@ your DeviceActivity class
Add the below function also in your DeviceActivity class,
After setting them in your Service method, you need to have a broadcast receiver in your DeviceActivity activity class to handle the various events fired by your BluetoothLeService service class.
If you further need an example code to guide you through, you can refer to BLE sample code