Skip to content

Commit

Permalink
various
Browse files Browse the repository at this point in the history
  • Loading branch information
ened committed Jan 16, 2024
1 parent 7145afe commit 345f7ee
Showing 1 changed file with 39 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,58 +25,55 @@ class BondingManager(private val context: Context) {
@SuppressLint("MissingPermission")
fun bondWithDevice(rxBleDevice: RxBleDevice): Single<Int> {
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())
}

}

}
}

0 comments on commit 345f7ee

Please sign in to comment.