2016-01-13 71 views
0

我试图设置一个应用程序来表现为使用Android 5.0(21)api的BLE外围设备。到目前为止,我已经建立了服务器,广告和连接工作,设置自定义的GATT服务和特性,并且可以在外围设备和其他测试设备之间读写。现在我试图将通知添加到一些参数中,但只是简单地放置;他们不工作。Android BLE GATT外设模式通知

当定义这些特性,它们包括通知属性:

BluetoothGattService battService = new BluetoothGattService(BatteryProfile.UUID_BATTERY_SERVICE, BluetoothGattService.SERVICE_TYPE_PRIMARY); 

BluetoothGattCharacteristic batteryLevelCharacteristic = 
     new BluetoothGattCharacteristic(BatteryProfile.UUID_BATTERY_LEVEL, 
       BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_NOTIFY, 
       BluetoothGattCharacteristic.PERMISSION_READ); 
battService.addCharacteristic(batteryLevelCharacteristic); 

mGattServer.addService(battService); 

在我BluetoothGattServerCallback,我包括方法:

@Override 
public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) { 
    Log.d(TAG, "Descriptor write: " + descriptor.toString()); 
    if (responseNeeded) { 
     mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, value); 
    } 
    super.onDescriptorWriteRequest(device, requestId, descriptor, preparedWrite, responseNeeded, offset, value); 
} 

,我希望记录的“描述写:”消息每当客户端启用通知时(或者为此写入任何描述符);但是没有记录过这样的消息(调试器也没有进入该功能)。

后来我尝试使用发送通知:

BluetoothGattCharacteristic batteryLevelCharacteristic = service.getCharacteristic(BatteryProfile.UUID_BATTERY_LEVEL); 
batteryLevelCharacteristic.setValue(new byte[]{ mBatteryLevel }); 
mGattServer.notifyCharacteristicChanged(mConnectedDevice, batteryLevelCharacteristic, false); 

那些虽然更新的价值;它不会向已连接的客户端发送通知。

我已经在Nexus 5X上测试了这个使用Android 6.0.1的版本(不幸的是我只能用于测试的Android设备)。在那里没有太多使用外设/ BluetoothGattServer API的例子,所以我有点迷路。是否有一些方法我忘记调用,或者我需要在某些未公开的顺序中执行某些操作才能使通知生效?

任何帮助将不胜感激!

回答

3

我不知道你是否已经找到了解决方案。但我认为你可以通过为你需要通知的特征定义客户端配置特征描述符来尝试。

这可以按照这篇文章所示完成。 Any way to implement BLE notifications in Android-L preview

+0

我忘了我问过,是的,我确实找到了解决方案。我无法确切地记得它修复了什么,但我确定你是对的 - 我需要使用并写入配置描述符。 – FuzzyWuzzie