在 Android-L 预览版中实现 BLE 通知的任何方式 [英] Any way to implement BLE notifications in Android-L preview

查看:27
本文介绍了在 Android-L 预览版中实现 BLE 通知的任何方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题不是关于 Android 通知,而是关于 BLE 通知(正如标题可能暗示的那样)

This question is not about Android notificatinos, but BLE notifications (as the title may hint)

我已经在 Android-L 上使用了基本的 BLE 外设模式

I have got basic BLE peripheral mode working on Android-L

有什么方法可以在 Android-L 预览版中实现 BLE 通知.我可以做一些像下面这样的事情,让一个特性能够通知,但试图听

Is there any way to implement BLE notifications in Android-L preview. I could do some thing like the following to make a charecteritic be able to notify, but trying to listen for

BluetoothGattCharacteristic firstServiceChar = new BluetoothGattCharacteristic(
        UUID.fromString(serviceOneCharUuid),
                BluetoothGattCharacteristic.PROPERTY_NOTIFY, BluetoothGattCharacteristic.PERMISSION_READ );

但在 iOS 上的 LightBlue 应用程序中,我无法订阅此特性.显然没有 API 可用于在订阅字符时响应调用(就像在 iOS 中一样)

But in LightBlue app on iOS I cannot subscribe to this characteristic. Apprently there is no API that could be use to respond to the calls when a char is subscribed (like there are in iOS)

如果您已在 Android-L 上成功启用 BLE 通知,请分享您的代码

Kindly share your code if you have successfully enabled BLE notifications on Android-L

推荐答案

关于 OP 所做的事情:

On top of what OP has done:

BluetoothGattCharacteristic firstServiceChar = new BluetoothGattCharacteristic(UUID.fromString(serviceOneCharUuid), BluetoothGattCharacteristic.PROPERTY_NOTIFY, BluetoothGattCharacteristic.PERMISSION_READ )

接下来是添加客户端特征配置描述符(UUID 是使用蓝牙基础 UUID 的 16 位 0x2902 的 128 位版本),以便连接的设备可以告诉您它需要通知(或指示),然后将该描述符添加到您的特征中:

The next thing is to add a Client Characteristic Configuration descriptor (UUID is the 128 bit version of the 16 bit 0x2902 using the Bluetooth base UUID), so that the connected device can tell yours that it wants notifications (or indications), and then add that descriptor to your characteristic:

BluetoothGattDescriptor gD = new BluetoothGattDescriptor(UUID.fromString("00002902-0000-1000-8000-00805F9B34FB"), BluetoothGattDescriptor.PERMISSION_WRITE | BluetoothGattDescriptor.PERMISSION_READ);

firstServiceChar.addDescriptor(gD);

该 UUID 来自蓝牙规范.显然,设备通过更新此描述符来订阅通知,因此您必须通过覆盖 onDescriptorWriteRequest 在 BluetoothGattServerCallback 中处理此问题:

That UUID is from the Bluetooth spec. Apparently a device subscribes to notifications by updating this descriptor, so you've got to handle this in your BluetoothGattServerCallback by overriding onDescriptorWriteRequest:

@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);

}

现在更新您的值并通知连接方:

Now update your value and notify the connectee:

firstServiceChar.setValue("HI");
btGattServer.notifyCharacteristicChanged(btClient, firstServiceChar, false);

希望这个快速而肮脏的代码会有所帮助,因为我首先使用 OP 的代码来使基本的外围模式工作:)

Hopefully this quick and dirty code will help, because it was OP's code that I used in the first place to even get basic peripheral mode working :)

这篇关于在 Android-L 预览版中实现 BLE 通知的任何方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆