diff --git a/packages/reactive_ble_mobile/android/src/main/kotlin/com/signify/hue/flutterreactiveble/ble/BleWrapper.kt b/packages/reactive_ble_mobile/android/src/main/kotlin/com/signify/hue/flutterreactiveble/ble/BleWrapper.kt index 1cc63e32..2d9ebc31 100644 --- a/packages/reactive_ble_mobile/android/src/main/kotlin/com/signify/hue/flutterreactiveble/ble/BleWrapper.kt +++ b/packages/reactive_ble_mobile/android/src/main/kotlin/com/signify/hue/flutterreactiveble/ble/BleWrapper.kt @@ -41,8 +41,6 @@ data class ScanInfo( } } -data class BondUpdate(val deviceId: String, val bondState: Int) - sealed class ConnectionUpdate data class ConnectionUpdateSuccess(val deviceId: String, val connectionState: Int) : ConnectionUpdate() @@ -73,6 +71,11 @@ data class RequestConnectionPrioritySuccess(val deviceId: String) : RequestConne data class RequestConnectionPriorityFailed(val deviceId: String, val errorMessage: String) : RequestConnectionPriorityResult() +data class BondUpdate( + val deviceId: String, + val bondState: Int, +) + enum class BleStatus(val code: Int) { UNKNOWN(code = 0), UNSUPPORTED(code = 1), diff --git a/packages/reactive_ble_mobile/android/src/main/kotlin/com/signify/hue/flutterreactiveble/ble/ReactiveBleClient.kt b/packages/reactive_ble_mobile/android/src/main/kotlin/com/signify/hue/flutterreactiveble/ble/ReactiveBleClient.kt index 276ffdb1..d1126e88 100644 --- a/packages/reactive_ble_mobile/android/src/main/kotlin/com/signify/hue/flutterreactiveble/ble/ReactiveBleClient.kt +++ b/packages/reactive_ble_mobile/android/src/main/kotlin/com/signify/hue/flutterreactiveble/ble/ReactiveBleClient.kt @@ -72,7 +72,7 @@ open class ReactiveBleClient(private val context: Context) : BleClient { context.applicationContext.registerReceiver( bondStateReceiver, - IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED) + IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED), ) } @@ -428,31 +428,37 @@ open class ReactiveBleClient(private val context: Context) : BleClient { is ConnectionUpdateSuccess -> { val device = rxBleClient.getBleDevice(update.deviceId) bondUpdateBehaviorSubject.onNext( - BondUpdate(update.deviceId, device.getBondState().code) + BondUpdate(update.deviceId, device.getBondState().code), ) } + is ConnectionUpdateError -> { val device = rxBleClient.getBleDevice(update.deviceId) bondUpdateBehaviorSubject.onNext( - BondUpdate(update.deviceId, device.getBondState().code) + BondUpdate(update.deviceId, device.getBondState().code), ) } } connectionUpdateBehaviorSubject.onNext(update) - } - private val bondStateReceiver = object : BroadcastReceiver() { - override fun onReceive(context: Context, intent: Intent) { - val device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) - val state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR) - - if (device != null && state != BluetoothDevice.ERROR) { - bondUpdateBehaviorSubject.onNext( - BondUpdate(device.address, BondState.fromRaw(state).code) - ) + private val bondStateReceiver = + object : BroadcastReceiver() { + override fun onReceive( + context: Context, + intent: Intent, + ) { + val device = + intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) + val state = + intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR) + + if (device != null && state != BluetoothDevice.ERROR) { + bondUpdateBehaviorSubject.onNext( + BondUpdate(device.address, BondState.fromRaw(state).code), + ) + } } } - } } diff --git a/packages/reactive_ble_mobile/android/src/main/kotlin/com/signify/hue/flutterreactiveble/channelhandlers/DeviceBondHandler.kt b/packages/reactive_ble_mobile/android/src/main/kotlin/com/signify/hue/flutterreactiveble/channelhandlers/DeviceBondHandler.kt index f8673e10..554e118e 100644 --- a/packages/reactive_ble_mobile/android/src/main/kotlin/com/signify/hue/flutterreactiveble/channelhandlers/DeviceBondHandler.kt +++ b/packages/reactive_ble_mobile/android/src/main/kotlin/com/signify/hue/flutterreactiveble/channelhandlers/DeviceBondHandler.kt @@ -5,7 +5,9 @@ import io.flutter.plugin.common.EventChannel import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.Disposable -class DeviceBondHandler(private val bleClient: com.signify.hue.flutterreactiveble.ble.BleClient) : EventChannel.StreamHandler { +class DeviceBondHandler( + private val bleClient: com.signify.hue.flutterreactiveble.ble.BleClient, +) : EventChannel.StreamHandler { private var sink: EventChannel.EventSink? = null private val converter = ProtobufMessageConverter() @@ -17,16 +19,17 @@ class DeviceBondHandler(private val bleClient: com.signify.hue.flutterreactivebl ) { eventSink?.let { sink = eventSink - disposable = bleClient.bondUpdateSubject - .distinct() - .observeOn(AndroidSchedulers.mainThread()) - .map(converter::convertToBondInfo) - .map { it.toByteArray() } - .subscribe { sink?.success(it) } + disposable = + bleClient.bondUpdateSubject + .distinct() + .observeOn(AndroidSchedulers.mainThread()) + .map(converter::convertToBondInfo) + .map { it.toByteArray() } + .subscribe { sink?.success(it) } } } override fun onCancel(objectSink: Any?) { disposable.dispose() } -} \ No newline at end of file +} diff --git a/packages/reactive_ble_mobile/android/src/main/kotlin/com/signify/hue/flutterreactiveble/converters/ProtobufMessageConverter.kt b/packages/reactive_ble_mobile/android/src/main/kotlin/com/signify/hue/flutterreactiveble/converters/ProtobufMessageConverter.kt index ee23b04b..51ea1a25 100644 --- a/packages/reactive_ble_mobile/android/src/main/kotlin/com/signify/hue/flutterreactiveble/converters/ProtobufMessageConverter.kt +++ b/packages/reactive_ble_mobile/android/src/main/kotlin/com/signify/hue/flutterreactiveble/converters/ProtobufMessageConverter.kt @@ -57,7 +57,8 @@ class ProtobufMessageConverter { .build() fun convertToBondInfo(update: BondUpdate): pb.BondInfo = - pb.BondInfo.newBuilder() + pb.BondInfo + .newBuilder() .setId(update.deviceId) .setBondState(update.bondState) .build() diff --git a/packages/reactive_ble_mobile/android/src/main/kotlin/com/signify/hue/flutterreactiveble/model/BondState.kt b/packages/reactive_ble_mobile/android/src/main/kotlin/com/signify/hue/flutterreactiveble/model/BondState.kt index c2868261..64f53377 100644 --- a/packages/reactive_ble_mobile/android/src/main/kotlin/com/signify/hue/flutterreactiveble/model/BondState.kt +++ b/packages/reactive_ble_mobile/android/src/main/kotlin/com/signify/hue/flutterreactiveble/model/BondState.kt @@ -3,21 +3,22 @@ package com.signify.hue.flutterreactiveble.model import android.bluetooth.BluetoothDevice import com.polidea.rxandroidble2.RxBleDevice -enum class BondState(val code: Int) { +enum class BondState( + val code: Int, +) { NONE(0), BONDING(1), - BONDED(2); + BONDED(2), + ; companion object { - fun fromRaw(raw: Int): BondState { - return when (raw) { + fun fromRaw(raw: Int): BondState = + when (raw) { BluetoothDevice.BOND_BONDING -> BONDING BluetoothDevice.BOND_BONDED -> BONDED else -> NONE } - } } } -fun RxBleDevice.getBondState(): BondState = - BondState.fromRaw(bluetoothDevice.bondState) \ No newline at end of file +fun RxBleDevice.getBondState(): BondState = BondState.fromRaw(bluetoothDevice.bondState)