Skip to content

Commit

Permalink
chore: update formatting to appease ktlint
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorbell committed Jan 7, 2025
1 parent 607b802 commit 665619d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
}

Expand Down Expand Up @@ -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>(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>(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),
)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
fun RxBleDevice.getBondState(): BondState = BondState.fromRaw(bluetoothDevice.bondState)

0 comments on commit 665619d

Please sign in to comment.