Example for subscribing for BLE indicate in androi

2019-07-20 02:38发布

问题:

I have connected to a bluetooth device. I am able to read charectristic using

mGatt.readCharacteristic(getMiliService().getCharacteristic(uuid));

But I am not able to register indicate for a characteristic.
What I tried

boolean flag1 = m_Gatt.setCharacteristicNotification(bluetoothgattcharacteristic, flag);

BluetoothGattDescriptor descriptor= bluetoothgattcharacteristic.getDescriptor(Helper.UUID_DESCRIPTOR_CLIENT_CHARACTERISTIC_CONFIGURATION);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mGatt.writeDescriptor(descriptor);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
mGatt.writeDescriptor(descriptor);

and waiting for

onCharacteristicChanged(BluetoothGatt bluetoothgatt, final BluetoothGattCharacteristic characteristic)

method getting called. which never happend.
I am not sure what I am missing here.
Can anyone give a working example for indicate?

回答1:

It is important for you to note that Android BLE stack allows you to write characteristics one at a time only.

And this is a working example that helped me a lot:

https://github.com/devunwired/accessory-samples/blob/master/BluetoothGatt/src/com/example/bluetoothgatt/MainActivity.java



回答2:

this project allow you to connect to a BLE device, and contain all the methods of permission: write,read,indicate and notification

for the indication, you must set the CCCD to it's value. you will find this in the project.