From 345f7ee93fc5ffae6b92227afd90f3c62321cdae Mon Sep 17 00:00:00 2001 From: Sebastian Roth Date: Tue, 16 Jan 2024 23:44:42 +0000 Subject: [PATCH] various --- .../flutterreactiveble/ble/BondingManager.kt | 81 +++++++++---------- 1 file changed, 39 insertions(+), 42 deletions(-) diff --git a/packages/reactive_ble_mobile/android/src/main/kotlin/com/signify/hue/flutterreactiveble/ble/BondingManager.kt b/packages/reactive_ble_mobile/android/src/main/kotlin/com/signify/hue/flutterreactiveble/ble/BondingManager.kt index db859249..e2d77004 100644 --- a/packages/reactive_ble_mobile/android/src/main/kotlin/com/signify/hue/flutterreactiveble/ble/BondingManager.kt +++ b/packages/reactive_ble_mobile/android/src/main/kotlin/com/signify/hue/flutterreactiveble/ble/BondingManager.kt @@ -25,58 +25,55 @@ class BondingManager(private val context: Context) { @SuppressLint("MissingPermission") fun bondWithDevice(rxBleDevice: RxBleDevice): Single { return Single.create { completion -> - when (rxBleDevice.bluetoothDevice.bondState) { + if (rxBleDevice.bluetoothDevice.bondState == BluetoothDevice.BOND_BONDED) { + completion.onSuccess(BluetoothDevice.BOND_BONDED) + return@create + } - BluetoothDevice.BOND_BONDED -> completion.onSuccess(BluetoothDevice.BOND_BONDED) - else -> { - val receiver = object : BroadcastReceiver() { - override fun onReceive(context: Context, intent: Intent) { - val deviceBeingPaired: BluetoothDevice? = - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - intent.getParcelableExtra( - BluetoothDevice.EXTRA_DEVICE, - BluetoothDevice::class.java - ) - } else { - @Suppress("DEPRECATION") - intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) - } + val receiver = object : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + val deviceBeingPaired: BluetoothDevice? = + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + intent.getParcelableExtra( + BluetoothDevice.EXTRA_DEVICE, + BluetoothDevice::class.java + ) + } else { + @Suppress("DEPRECATION") + intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) + } - if (deviceBeingPaired?.address == rxBleDevice.bluetoothDevice.address) { - val state = intent.getIntExtra( - BluetoothDevice.EXTRA_BOND_STATE, - BluetoothDevice.BOND_NONE - ) + if (deviceBeingPaired?.address == rxBleDevice.bluetoothDevice.address) { + val state = intent.getIntExtra( + BluetoothDevice.EXTRA_BOND_STATE, + BluetoothDevice.BOND_NONE + ) - when (state) { - BluetoothDevice.BOND_BONDED -> completion.onSuccess(state) - BluetoothDevice.BOND_NONE -> completion.onSuccess(state) - // BOND_BONDING is a intermediate state - do not send this back. - } - } + when (state) { + BluetoothDevice.BOND_BONDED -> completion.onSuccess(state) + BluetoothDevice.BOND_NONE -> completion.onSuccess(state) + // BOND_BONDING is a intermediate state - do not send this back. } } + } + } - completion.setDisposable(Disposables.fromAction { - context.unregisterReceiver( - receiver - ) - }) + completion.setDisposable(Disposables.fromAction { + context.unregisterReceiver( + receiver + ) + }) - context.registerReceiver( - receiver, - IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED) - ) + context.registerReceiver( + receiver, + IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED) + ) - val createBondResult = rxBleDevice.bluetoothDevice.createBond() + val createBondResult = rxBleDevice.bluetoothDevice.createBond() - if (!createBondResult) { - completion.tryOnError(BondingFailedException()) - } - } + if (!createBondResult) { + completion.tryOnError(BondingFailedException()) } - } - } }