Subscribe to a BLE Gatt notification Android

后端 未结 2 767
失恋的感觉
失恋的感觉 2021-02-02 09:58

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

相关标签:
2条回答
  • 2021-02-02 10:40

    In addition to the accepted answer, I had to set the Characteristic to WRITE_TYPE_DEFAULT when subscribing to peripheral running on OS X.

    characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
    
    0 讨论(0)
  • 2021-02-02 10:51

    To receive notification in Android you need to set characteristic notification to true

    gatt.setCharacteristicNotification(characteristic, true);
    

    You also need to set the client characteristic configuration descriptor 0x2902

    // 0x2902 org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
    UUID uuid = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(uuid);
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    gatt.writeDescriptor(descriptor);
    

    A better API would be for setCharacteristicNotification to set the descriptor, but unfortunately it doesn't seem to work that way.

    0 讨论(0)
提交回复
热议问题