Android BLE GATT Peripheral Mode Notifications

狂风中的少年 提交于 2020-01-11 07:47:13

问题


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.

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

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

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.

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?

Any help would be greatly appreciated!


回答1:


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.

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



来源:https://stackoverflow.com/questions/34760164/android-ble-gatt-peripheral-mode-notifications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!