简体   繁体   中英

Android BLE GATT Peripheral Mode Notifications

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM