如何将READ,NOTIFY属性添加到Android BLE GATT Server中的自定义特性?

时间:2015-12-08 14:24:15

标签: android bluetooth bluetooth-lowenergy android-bluetooth

我在Nexus 9(作为外围设备)托管GATT服务器。我能够分别实现具有Read属性和Notify属性的特性。如何使用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

1 个答案:

答案 0 :(得分:1)

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

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