From c7bed6c7a1ef566622ecadaffaa9783a3d1f019e Mon Sep 17 00:00:00 2001 From: Martijn van Welie Date: Sun, 26 May 2024 13:34:10 +0200 Subject: [PATCH] Don't do connections when bluetooth is off --- .../java/com/welie/blessed/BluetoothCentralManager.kt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/blessed/src/main/java/com/welie/blessed/BluetoothCentralManager.kt b/blessed/src/main/java/com/welie/blessed/BluetoothCentralManager.kt index 8e5de1b..6f50a5d 100644 --- a/blessed/src/main/java/com/welie/blessed/BluetoothCentralManager.kt +++ b/blessed/src/main/java/com/welie/blessed/BluetoothCentralManager.kt @@ -474,6 +474,11 @@ class BluetoothCentralManager(private val context: Context) { private fun connectPeripheral(peripheral: BluetoothPeripheral, resultCentralManagerCallback: BluetoothCentralManagerCallback) { synchronized(connectLock) { + if (bleNotReady()) { + Logger.e(TAG, "cannot connect peripheral '%s' because Bluetooth is off", peripheral.address) + return + } + checkPeripheralStatus(peripheral) // Check if the peripheral is cached or not. If not, issue a warning because connection may fail @@ -496,6 +501,11 @@ class BluetoothCentralManager(private val context: Context) { */ fun autoConnectPeripheral(peripheral: BluetoothPeripheral) { synchronized(connectLock) { + if (bleNotReady()) { + Logger.e(TAG, "cannot autoConnectPeripheral '%s' because Bluetooth is off", peripheral.address) + return + } + checkPeripheralStatus(peripheral) // Check if the peripheral is uncached and start autoConnectPeripheralByScan