简体   繁体   中英

BLE Gatt onCharacteristicChanged method not called

We are facing an issue with BLE Gatt onCharacteristicChanged method. This method is not called.

We have enabled notification using below code:

mBluetoothGatt!!.setCharacteristicNotification("NOTIFY_SERVICE_CHARACTERISTIC", true)

val descriptor = NOTIFY_SERVICE_CHARACTERISTIC.getDescriptor(convertFromInteger(0x2902))
descriptor.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
descriptor.value = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE
var status = mBluetoothGatt!!.writeDescriptor(descriptor)

After this, We are sending code to BLE from onDescriptorWrite Method.

{WRITE_SERVICE_CHARACTERISTIC}.setValue({BYTE_ARRAY}) 
val status = mBluetoothGatt!!.writeCharacteristic(RxChar)

After sending code to BLE. it will response in onCharacteristicWrite Method. but we are not getting any response in onCharacteristicChanged Method via Notify Service.

Same commands are working in other BLE Scanner Application but my application facing an issue.

Please help us to solve the issue.

Thanks

You are overriding the notification settings of the descriptor with the indication setting:

descriptor.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
descriptor.value = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE

You probably only need notifications and can remove the enabling of indications:

mBluetoothGatt!!.setCharacteristicNotification("NOTIFY_SERVICE_CHARACTERISTIC", true)

val descriptor = NOTIFY_SERVICE_CHARACTERISTIC.getDescriptor(convertFromInteger(0x2902))
descriptor.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
var status = mBluetoothGatt!!.writeDescriptor(descriptor)

If you truly need both notifications and indications you have to set the values like this:

descriptor.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE | BluetoothGattDescriptor.ENABLE_INDICATION_VALUE

The | operator does a bitwise OR operation and therefore combines both flags into one

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