Android BLE GATT外围模式通知 [英] Android BLE GATT Peripheral Mode Notifications

查看:144
本文介绍了Android BLE GATT外围模式通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Android 5.0(21)API将应用设置为BLE外围设备.到目前为止,我已经设置了服务器,使广告和连接正常工作,设置了自定义GATT服务和特性,并且可以在外围设备与其他测试设备之间进行读写.现在,我试图将通知添加到一些参数中,但简而言之;他们没有工作.

I'm trying to set up an app to behave as a BLE peripheral device using the Android 5.0 (21) api. So far, I've set up the server, got advertising and connections working, set up custom GATT services and characteristics, and can read and write between the peripheral device and other testing devices. Now I'm trying to add notifications to a few of the parameters but simply put; they're not working.

定义这些特征时,它们包括notify属性:

When defining these characteristics, they include the notify property:

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中,我包括了方法:

In my BluetoothGattServerCallback, I included the method:

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

我希望每当客户端启用通知(或就此写入任何描述符)时记录"Descriptor write:"消息;但不会记录任何此类消息(调试器也不会进入该函数).

which I expect to log the "Descriptor write: " message whenever a client enables notifications (or writes to any descriptor for that matter); but no such message is ever logged (nor does the debugger ever get into that function).

我稍后尝试使用以下方式发送通知:

I later try to send notifications using:

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

尽管,它会更新值;它不会向连接的客户端发送通知.

Which, although it updates the value; it does not send a notification to the connected client.

我已经在Nexus 5X上使用Android 6.0.1进行了测试(不幸的是,我可以使用它进行测试的唯一Android设备).那里并没有很多使用外设/BluetoothGattServer API的示例,所以我有些迷茫.有什么我忘了打电话的方法,或者我需要以某种无证的方式做一些事情才能使通知正常工作吗?

I've tested this using Android 6.0.1 on a Nexus 5X (unfortunately the only Android device I have available to test with). There aren't really many examples of using the peripheral / BluetoothGattServer API out there, so I'm a little lost. Is there some method I'm forgetting to call, or something I need to do in some undocumented order to get notifications to work?

任何帮助将不胜感激!

推荐答案

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

I don't know if you have already found a solution for this. But I think you could try by defining the Client Configuration Characteristic Descriptor for the characteristic you need the notifications for.

可以按照这篇文章中的说明进行操作. 在Android中实现BLE通知的任何方法-L预览

This can be done as shown in this post. Any way to implement BLE notifications in Android-L preview

这篇关于Android BLE GATT外围模式通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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