Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

kotlin - Getting duplicate BLE notifications on Android

I'm developing a frame exchange sequence between an nRF52840 and an Android smartphone using the BLE protocol.

The first time I connect, everything works fine. I activate the listening of BLE notifications by the Android smartphone with this method:

    fun enableBleNotificationsOnCentral(currentBluetoothGatt: BluetoothGatt, serviceUUID: UUID, characteristicUUID: UUID) {
        getMainDeviceService(currentBluetoothGatt, serviceUUID)?.let { service ->
            val notificationConfiguration = service.getCharacteristic(characteristicUUID)
            val result = currentBluetoothGatt.setCharacteristicNotification(notificationConfiguration, true)

            println(result)
        }
    }

And I enable sending BLE notifications on the nRF52840 with this method:

    fun enableBleNotificationsOnPeripheral(currentBluetoothGatt: BluetoothGatt, serviceUUID: UUID, characteristicUUID: UUID, descriptorUUID: UUID) {
        getMainDeviceService(currentBluetoothGatt, serviceUUID)?.let { service ->
            val descriptorConfiguration = service.getCharacteristic(characteristicUUID).getDescriptor(
                descriptorUUID).apply {
                value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
            }
            val result = currentBluetoothGatt.writeDescriptor(descriptorConfiguration)

            println(result)
        }
    }

These methods are called each time my smartphone is connected to the nRF52840.

But if I disconnect and connect a second time, I receive each of the notifications in duplicate. In addition, if I disconnect and connect a 3rd time, I receive each notification 3 times, and one more each time I reconnect.

I checked my code on the nRF52840 and it does not duplicate notifications.

Here is the method I call when I request a disconnection:

private fun disconnectFromCurrentDevice() {
    currentBluetoothGatt?.disconnect()
    BLECallbackManager.currentDevice = null
    setUiMode(false)
}

I guess my problem is related to the fact that I don't disable the receipt of BLE notifications by my Android application when I disconnect but I'm not sure. And if that's where the problem comes from, when should I do it in the disconnect method? Can you help me?

question from:https://stackoverflow.com/questions/65858118/getting-duplicate-ble-notifications-on-android

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I guess you're creating a new BluetoothGatt object for every new connection attempt, but you not destroy the previous one.

Try change disconnect() to close().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...