From 67cedddec56269e473adb595ad00b07cc9220ceb Mon Sep 17 00:00:00 2001 From: Martijn van Welie Date: Sat, 27 Jul 2024 22:22:02 +0200 Subject: [PATCH] Fix race condition for connection timer --- .../src/main/java/com/welie/blessed/BluetoothPeripheral.kt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/blessed/src/main/java/com/welie/blessed/BluetoothPeripheral.kt b/blessed/src/main/java/com/welie/blessed/BluetoothPeripheral.kt index e3209e5..df5af99 100644 --- a/blessed/src/main/java/com/welie/blessed/BluetoothPeripheral.kt +++ b/blessed/src/main/java/com/welie/blessed/BluetoothPeripheral.kt @@ -481,16 +481,18 @@ class BluetoothPeripheral internal constructor( Logger.d(TAG, "connect to '%s' (%s) using TRANSPORT_LE", name, address) registerBondingBroadcastReceivers() discoveryStarted = false + connectTimestamp = SystemClock.elapsedRealtime() + startConnectionTimer(this@BluetoothPeripheral) bluetoothGatt = try { device.connectGatt(context, false, bluetoothGattCallback, BluetoothDevice.TRANSPORT_LE) } catch (e: SecurityException) { Logger.d(TAG, "exception") + cancelConnectionTimer() null } + bluetoothGatt?.let { bluetoothGattCallback.onConnectionStateChange(it, HciStatus.SUCCESS.value, BluetoothProfile.STATE_CONNECTING) - connectTimestamp = SystemClock.elapsedRealtime() - startConnectionTimer(this@BluetoothPeripheral) } } } else {