geo*_*xis 2 bluetooth-lowenergy android-bluetooth android-5.0-lollipop
这个问题不是关于Android notificatinos,而是关于BLE通知(标题可能暗示)
我已经在Android-L上运行了基本的BLE外设模式
有没有办法在Android-L预览中实现BLE通知.我可以做一些类似下面的事情,使charecteritic能够通知,但试图听
BluetoothGattCharacteristic firstServiceChar = new BluetoothGattCharacteristic(
UUID.fromString(serviceOneCharUuid),
BluetoothGattCharacteristic.PROPERTY_NOTIFY, BluetoothGattCharacteristic.PERMISSION_READ );
Run Code Online (Sandbox Code Playgroud)
但在iOS上的LightBlue应用程序中,我无法订阅此特性.显然,在订阅char时没有可用于响应调用的API(就像在iOS中一样)
如果您已成功在Android-L上启用BLE通知,请分享您的代码
lud*_*ace 15
除了OP之外:
BluetoothGattCharacteristic firstServiceChar = new BluetoothGattCharacteristic(UUID.fromString(serviceOneCharUuid), BluetoothGattCharacteristic.PROPERTY_NOTIFY, BluetoothGattCharacteristic.PERMISSION_READ )
Run Code Online (Sandbox Code Playgroud)
接下来是添加客户端特性配置描述符(UUID是使用蓝牙基础UUID的16位0x2902的128位版本),以便连接的设备可以告诉您它想要通知(或指示),然后添加那个描述符符合你的特点:
BluetoothGattDescriptor gD = new BluetoothGattDescriptor(UUID.fromString("00002902-0000-1000-8000-00805F9B34FB"), BluetoothGattDescriptor.PERMISSION_WRITE | BluetoothGattDescriptor.PERMISSION_READ);
firstServiceChar.addDescriptor(gD);
Run Code Online (Sandbox Code Playgroud)
UUID来自蓝牙规范.显然,设备通过更新此描述符来订阅通知,因此您必须通过覆盖onDescriptorWriteRequest在BluetoothGattServerCallback中处理此问题:
@Override
public void onDescriptorWriteRequest (BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
btClient = device; // perhaps add to some kind of collection of devices to update?
// now tell the connected device that this was all successfull
btGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value);
}
Run Code Online (Sandbox Code Playgroud)
现在更新您的值并通知连接器:
firstServiceChar.setValue("HI");
btGattServer.notifyCharacteristicChanged(btClient, firstServiceChar, false);
Run Code Online (Sandbox Code Playgroud)
希望这个快速而又脏的代码会有所帮助,因为我首先使用OP代码甚至可以使基本的外设模式工作:)