Skip to content

Commit

Permalink
Immediately try to disconnect devices when bluetooth is turned off
Browse files Browse the repository at this point in the history
  • Loading branch information
weliem committed Jun 28, 2022
1 parent 2874edc commit 55b8f04
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,11 @@ internal class BluetoothHandler private constructor(context: Context) {
ConnectionState.CONNECTED -> handlePeripheral(peripheral)
ConnectionState.DISCONNECTED -> scope.launch {
delay(15000)
central.autoConnectPeripheral(peripheral)

// Check if this peripheral should still be auto connected
if (central.getPeripheral(peripheral.address).getState() == ConnectionState.DISCONNECTED) {
central.autoConnectPeripheral(peripheral)
}
}
else -> {
}
Expand Down
39 changes: 4 additions & 35 deletions blessed/src/main/java/com/welie/blessed/BluetoothCentralManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class BluetoothCentralManager(private val context: Context) {
private var scanSettings: ScanSettings
private val autoConnectScanSettings: ScanSettings
private val connectionRetries: MutableMap<String, Int> = ConcurrentHashMap()
private var expectingBluetoothOffDisconnects = false
private var disconnectRunnable: Runnable? = null
private val pinCodes: MutableMap<String, String> = ConcurrentHashMap()
private var currentResultCallback : ((BluetoothPeripheral, ScanResult) -> Unit)? = null
Expand Down Expand Up @@ -197,10 +196,6 @@ class BluetoothCentralManager(private val context: Context) {
}

override fun disconnected(peripheral: BluetoothPeripheral, status: HciStatus) {
if (expectingBluetoothOffDisconnects) {
cancelDisconnectionTimer()
expectingBluetoothOffDisconnects = false
}
connectedPeripherals.remove(peripheral.address)
unconnectedPeripherals.remove(peripheral.address)
scannedPeripherals.remove(peripheral.address)
Expand Down Expand Up @@ -854,29 +849,6 @@ class BluetoothCentralManager(private val context: Context) {
reconnectPeripheralAddresses.clear()
}

/**
* Timer to determine if manual disconnection in case of bluetooth off is needed
*/
private fun startDisconnectionTimer() {
cancelDisconnectionTimer()
disconnectRunnable = Runnable {
Logger.e(TAG, "bluetooth turned off but no automatic disconnects happening, so doing it ourselves")
cancelAllConnectionsWhenBluetoothOff()
disconnectRunnable = null
}
mainHandler.postDelayed(disconnectRunnable!!, 1000)
}

/**
* Cancel timer for bluetooth off disconnects
*/
private fun cancelDisconnectionTimer() {
if (disconnectRunnable != null) {
mainHandler.removeCallbacks(disconnectRunnable!!)
disconnectRunnable = null
}
}

fun observeAdapterState(callback: (state: Int) -> Unit) {
this.adapterStateCallback = callback
}
Expand All @@ -897,13 +869,14 @@ class BluetoothCentralManager(private val context: Context) {
BluetoothAdapter.STATE_OFF -> {
// Check if there are any connected peripherals or connections in progress
if (connectedPeripherals.isNotEmpty() || unconnectedPeripherals.isNotEmpty()) {
// See if they are automatically disconnect
expectingBluetoothOffDisconnects = true
startDisconnectionTimer()
cancelAllConnectionsWhenBluetoothOff()
}
Logger.d(TAG, "bluetooth turned off")
}
BluetoothAdapter.STATE_TURNING_OFF -> {
// Try to disconnect all peripherals because Android doesn't always do that
connectedPeripherals.forEach { entry -> entry.value.cancelConnection()}

// Stop all scans so that we are back in a clean state
if (isScanning) {
// Note that we can't call stopScan if the adapter is off
Expand All @@ -922,8 +895,6 @@ class BluetoothCentralManager(private val context: Context) {
}
}

expectingBluetoothOffDisconnects = true

// Stop all scans so that we are back in a clean state
// Note that we can't call stopScan if the adapter is off
cancelTimeoutTimer()
Expand All @@ -940,11 +911,9 @@ class BluetoothCentralManager(private val context: Context) {
bluetoothScanner = bluetoothAdapter.bluetoothLeScanner
bluetoothScanner?.stopScan(defaultScanCallback)

expectingBluetoothOffDisconnects = false
Logger.d(TAG, "bluetooth turned on")
}
BluetoothAdapter.STATE_TURNING_ON -> {
expectingBluetoothOffDisconnects = false
Logger.d(TAG, "bluetooth turning on")
}
}
Expand Down

0 comments on commit 55b8f04

Please sign in to comment.