相关疑难解决方法(0)

前台服务重启后多次接收BluetoothGattCallback

我正在使用支持BLE的硬件,并使用Android的Foreground Service与该硬件进行通信。前台服务负责处理BLE相关事件,并且在一段时间内按照要求运行良好,但是由于某种原因,如果前台服务被杀死或BLE连接断开,则应用会尝试再次重新连接到BLE,然后BLE回调开始从BluetoothGattCallback获取重复事件,即使硬件将单个事件发送到Bluetooth,但Android BluetoothGattCallback接收到相同的多个回调,这在我们的实现中导致很多错误。

作为参考,请按以下方式查看日志,

Following are methods and callbacks from my foreground service,

BLEManagerService: *****onDescriptorWrite: 0*****<br>
BLEManagerService: *****onDescriptorWrite: 0*****<br>
BLEManagerService: *****onDescriptorWrite: 0*****<br>
BLEManagerService: Firmware: onCharacteristicRead true<br>
BLEManagerService: *****onDescriptorWrite: 0*****<br>
BLEManagerService: Firmware: onCharacteristicRead true<br>
BLEManagerService: *****onCharacteristicRead: 0*****<br>
BLEManagerService: *****onCharacteristicRead: 0*****<br>
Run Code Online (Sandbox Code Playgroud)

override fun onCreate() {
    super.onCreate()

    mBluetoothGatt?.let { refreshDeviceCache(it) }

    registerReceiver(btStateBroadcastReceiver, IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED))
}

    /**
 * Start BLE scan
 */
private fun scanLeDevice(enable: Boolean) {
    if (enable && bleConnectionState == DISCONNECTED) {
        //initialize scanning BLE
        startScan()
        scanTimer = scanTimer()
    } else {
        stopScan("scanLeDevice: …
Run Code Online (Sandbox Code Playgroud)

android bluetooth-lowenergy

5
推荐指数
1
解决办法
179
查看次数

标签 统计

android ×1

bluetooth-lowenergy ×1