Skip to content

Commit

Permalink
make changes on kotlin side to support updated pigeon
Browse files Browse the repository at this point in the history
  • Loading branch information
crc-32 committed May 7, 2024
1 parent 4fbd6d6 commit c0daacc
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.bluetooth.BluetoothDevice
import android.util.Log
import androidx.test.filters.RequiresDevice
import androidx.test.platform.app.InstrumentationRegistry
import io.rebble.cobble.datasources.FlutterPreferences
import io.rebble.cobble.datasources.IncomingPacketsListener
import io.rebble.libpebblecommon.ProtocolHandlerImpl
import io.rebble.libpebblecommon.packets.PhoneAppVersion
Expand All @@ -22,11 +23,12 @@ class BlueGATTServerTest {
lateinit var blueLEDriver: BlueLEDriver
val protocolHandler = ProtocolHandlerImpl()
val incomingPacketsListener = IncomingPacketsListener()
val flutterPreferences = FlutterPreferences(InstrumentationRegistry.getInstrumentation().targetContext)
lateinit var remoteDevice: BluetoothDevice

@Before
fun setUp() {
blueLEDriver = BlueLEDriver(InstrumentationRegistry.getInstrumentation().targetContext, protocolHandler, incomingPacketsListener = incomingPacketsListener)
blueLEDriver = BlueLEDriver(InstrumentationRegistry.getInstrumentation().targetContext, protocolHandler, incomingPacketsListener = incomingPacketsListener, flutterPreferences = flutterPreferences)
remoteDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice("48:91:52:CC:D1:D5")
if (remoteDevice.bondState != BluetoothDevice.BOND_NONE) remoteDevice::class.java.getMethod("removeBond").invoke(remoteDevice)
}
Expand All @@ -46,7 +48,7 @@ class BlueGATTServerTest {

runBlocking {
while (true) {
blueLEDriver.startSingleWatchConnection(remoteDevice).collect { value ->
blueLEDriver.startSingleWatchConnection(PebbleBluetoothDevice(remoteDevice)).collect { value ->
when (value) {
is SingleConnectionStatus.Connected -> {
Log.d("Test", "Connected")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ class ConnectionLooper @Inject constructor(
private var currentConnection: Job? = null
private var lastConnectedWatch: String? = null

fun negotiationsComplete(watch: BluetoothDevice) {
fun negotiationsComplete(watch: PebbleBluetoothDevice) {
if (connectionState.value is ConnectionState.Negotiating) {
_connectionState.value = ConnectionState.Connected(watch)
} else {
Timber.w("negotiationsComplete state mismatch!")
}
}

fun recoveryMode(watch: BluetoothDevice) {
fun recoveryMode(watch: PebbleBluetoothDevice) {
if (connectionState.value is ConnectionState.Connected || connectionState.value is ConnectionState.Negotiating) {
_connectionState.value = ConnectionState.RecoveryMode(watch)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ sealed class ConnectionState {
class WaitingForBluetoothToEnable(val watch: PebbleBluetoothDevice?) : ConnectionState()
class WaitingForReconnect(val watch: PebbleBluetoothDevice?) : ConnectionState()
class Connecting(val watch: PebbleBluetoothDevice?) : ConnectionState()
class Negotiating(val watch: BluetoothDevice?) : ConnectionState()
class Negotiating(val watch: PebbleBluetoothDevice?) : ConnectionState()
class Connected(val watch: PebbleBluetoothDevice) : ConnectionState()
class RecoveryMode(val watch: BluetoothDevice) : ConnectionState()
class RecoveryMode(val watch: PebbleBluetoothDevice) : ConnectionState()
}

val ConnectionState.watchOrNull: PebbleBluetoothDevice?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import io.rebble.cobble.bridges.FlutterBridge
import io.rebble.cobble.bridges.ui.BridgeLifecycleController
import io.rebble.cobble.data.toPigeon
import io.rebble.cobble.datasources.WatchMetadataStore
import io.rebble.cobble.pigeons.BooleanWrapper
import io.rebble.cobble.pigeons.Pigeons
import io.rebble.cobble.util.macAddressToLong
import io.rebble.libpebblecommon.ProtocolHandler
Expand All @@ -34,7 +33,9 @@ class ConnectionFlutterBridge @Inject constructor(
}

override fun isConnected(): Pigeons.BooleanWrapper {
return BooleanWrapper(connectionLooper.connectionState.value is ConnectionState.Connected)
return Pigeons.BooleanWrapper().apply {
value = connectionLooper.connectionState.value is ConnectionState.Connected
}
}


Expand All @@ -57,17 +58,17 @@ class ConnectionFlutterBridge @Inject constructor(
watchMetadataStore.lastConnectedWatchMetadata,
watchMetadataStore.lastConnectedWatchModel
) { connectionState, watchMetadata, model ->
Pigeons.WatchConnectionStatePigeon().apply {
isConnected = connectionState is ConnectionState.Connected ||
connectionState is ConnectionState.RecoveryMode
isConnecting = connectionState is ConnectionState.Connecting ||
connectionState is ConnectionState.WaitingForReconnect ||
connectionState is ConnectionState.WaitingForBluetoothToEnable ||
connectionState is ConnectionState.Negotiating
val bluetoothDevice = connectionState.watchOrNull
currentWatchAddress = bluetoothDevice?.address
currentConnectedWatch = watchMetadata.toPigeon(bluetoothDevice, model)
}
val bluetoothDevice = connectionState.watchOrNull
Pigeons.WatchConnectionStatePigeon.Builder()
.setIsConnected(connectionState is ConnectionState.Connected ||
connectionState is ConnectionState.RecoveryMode)
.setIsConnecting(connectionState is ConnectionState.Connecting ||
connectionState is ConnectionState.WaitingForReconnect ||
connectionState is ConnectionState.WaitingForBluetoothToEnable ||
connectionState is ConnectionState.Negotiating)
.setCurrentWatchAddress(bluetoothDevice?.address)
.setCurrentConnectedWatch(watchMetadata.toPigeon(bluetoothDevice, model))
.build()
}.collect {
connectionCallbacks.onWatchConnectionStateChanged(
it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ class ScanFlutterBridge @Inject constructor(
scanCallbacks.onScanStarted { }

if (BuildConfig.DEBUG) {
scanCallbacks.onScanUpdate(ListWrapper(listOf(PebbleScanDevicePigeon().also {
it.address = "10.0.2.2" //TODO: make configurable
it.name = "Emulator"
it.firstUse = false
it.runningPRF = false
it.serialNumber = "EMULATOR"
}.toMapExt()))) {}
scanCallbacks.onScanUpdate(listOf(Pigeons.PebbleScanDevicePigeon.Builder()
.setAddress("10.0.2.2")
.setName("Emulator")
.setFirstUse(false)
.setRunningPRF(false)
.setSerialNumber("EMULATOR")
.build())) {}
}

bleScanner.getScanFlow().collect { foundDevices ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ class WatchService : LifecycleService() {
is ConnectionState.Connected -> {
icon = R.drawable.ic_notification_connected
titleText = "Connected to device"
deviceName = if (it.watch.emulated) "[EMU] ${it.watch.address}" else it.watch.bluetoothDevice?.name
deviceName = if (it.watch.emulated) "[EMU] ${it.watch.address}" else it.watch.bluetoothDevice?.name!!
channel = NOTIFICATION_CHANNEL_WATCH_CONNECTED
}
is ConnectionState.RecoveryMode -> {
icon = R.drawable.ic_notification_connected
titleText = "Connected to device (Recovery Mode)"
deviceName = it.watch.name
deviceName = if (it.watch.emulated) "[EMU] ${it.watch.address}" else it.watch.bluetoothDevice?.name!!
channel = NOTIFICATION_CHANNEL_WATCH_CONNECTED
}
}
Expand Down

0 comments on commit c0daacc

Please sign in to comment.