Я разрабатываю приложение BLE, основанное на примере проекта Gatt, предоставленном google: https://developer.android.com/samples/BluetoothLeGatt/index.html. Таким образом, я могу отправить запись данных в характеристику успешно. Теперь мне нужно знать, когда эта характеристика меняет свою ценность.Подпишитесь на описание и поймите на это значение.
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);
};
Но я не знаю, как я могу поймать значение для чтения характеристика. На самом деле, я не знаю, подходит ли мой код к признаку или нет. Кто-нибудь может мне помочь? Как я могу проверить, действительно ли значение характерного изменения? И как я могу отобразить новое значение этого признака на экране? Правильно ли мой код, или мне нужно добавить, изменить или удалить что-то? Thaks заранее. I руководство по этому вопросу: Reading multiple characteristics from a BLE device synchronously (Recommended Method for Android)