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

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

问题描述

此问题与Android notificatinos无关,而是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应用程序中,我无法订阅此功能.显然,当订阅了一个char时,没有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的基础上:

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天全站免登陆