BluetoothGattCharacteristic

BluetoothGattCharacteristic

我在Nexus 9(作为外围设备)中托管了GATT服务器。我可以用Read属性和Notify属性分别实现一个特性。如何使用“读取”和“通知”属性来承载特征?

在下面的代码中实现了Read属性:



final String  SERVICE_A = "0000fff0-0000-1000-8000-00805f9b34fb";
final String  CHAR_READ1 = "0000fff1-0000-1000-8000-00805f9b34fb";




BluetoothGattService previousService =
          mGattServer.getService( UUID.fromString(SERVICE_A));

if(null != previousService)
         mGattServer.removeService(previousService);



        BluetoothGattCharacteristic read1Characteristic = new BluetoothGattCharacteristic(
          UUID.fromString(CHAR_READ1),
          BluetoothGattCharacteristic.PROPERTY_READ,
          BluetoothGattCharacteristic.PERMISSION_READ
          );

read1Characteristic.setValue(read1Data.getBytes());
BluetoothGattService AService = new BluetoothGattService(
          UUID.fromString(SERVICE_A),
          BluetoothGattService.SERVICE_TYPE_PRIMARY);


        AService.addCharacteristic(read1Characteristic);


完整的源代码here

最佳答案

这些属性是按位的,因此您可以执行以下操作:

BluetoothGattCharacteristic read1Characteristic = new BluetoothGattCharacteristic(
          UUID.fromString(CHAR_READ1),
          BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_NOTIFY,
          BluetoothGattCharacteristic.PERMISSION_READ
          );

10-08 20:11