Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug with connecting to bonded printer #5

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions composeApp/src/commonMain/kotlin/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fun App() {
val scope = rememberCoroutineScope()
var message by remember { mutableStateOf("") }
var showDialog by remember { mutableStateOf(false) }
var scanCount by remember { mutableStateOf(0) }

LaunchedEffect(Unit){
if (!showContent) showContent = true
Expand All @@ -70,6 +71,23 @@ fun App() {
}
showDialog = message.isNotEmpty()
}
LaunchedEffect(connectionState){
if (connectionState.isBluetoothReady && !connectionState.discoveredPrinter && !connectionState.isScanning && scanCount == 0){
scanCount++
scope.launch {
bluetoothConnection.scanForPrinters()
}
//println("Scanning")
}
if (connectionState.discoveredPrinter && !connectionState.isConnected){
bluetoothConnection.connect()
println("Connecting")
}
if (connectionState.isPrinting){
//printingStarted = true
println("Printing")
}
}

Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally) {
TopAppBar(modifier = Modifier.fillMaxWidth(), title = { Text("Discovered Bluetooth devices") })
Expand Down
2 changes: 1 addition & 1 deletion library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ android {
@Suppress("UnstableApiUsage")
mavenPublishing {
publishToMavenCentral(SonatypeHost.S01, true)
val versionTxt = "0.0.2"
val versionTxt = "0.0.3"
val isDev = findProperty("env")?.equals("dev") ?: false
val version = if (isDev) "0.0.1-SNAPSHOT" else versionTxt

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ internal object AndroidBluetoothConnection: BlueLine {
//Service UUID is 16bit e.g FF00, to scan we'll have to use 0000xxxx-0000-1000-8000-00805F9B34FB
private val printerUUID = ParcelUuid.fromString("000018F0-0000-1000-8000-00805F9B34FB")
private val characterUuid = UUID.fromString("00002AF1-0000-1000-8000-00805F9B34FB")
private val bondedUUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb")

private var bluetoothDevice: BluetoothDevice? = null
private val bluetoothLeScanner = bluetoothAdapter?.bluetoothLeScanner
Expand Down Expand Up @@ -170,7 +171,7 @@ internal object AndroidBluetoothConnection: BlueLine {
return
}
val pairedDevices: Set<BluetoothDevice>? = bluetoothAdapter?.bondedDevices
val pairedPrinter = pairedDevices?.find { it.uuids.contains(printerUUID) }
val pairedPrinter = pairedDevices?.find { it.isPrinter() }
if (pairedPrinter != null){
bluetoothDevice = pairedPrinter
stateFlow.update { state -> state.copy(deviceName = pairedPrinter.name.orEmpty(), discoveredPrinter = true) }
Expand Down Expand Up @@ -213,16 +214,24 @@ internal object AndroidBluetoothConnection: BlueLine {
stateFlow.update { it.copy(isScanning = false) }
}
stateFlow.update { it.copy(bluetoothConnectionError = null, isScanning = true) }

bluetoothLeScanner?.startScan(
listOf(ScanFilter.Builder().setServiceUuid(printerUUID).build()),
ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_BALANCED).build(),
scanCallback
)
delay(10.seconds)

delay(5.seconds)
if (!stateFlow.value.discoveredPrinter){
stateFlow.update { it.copy(bluetoothConnectionError = ConnectionError.BLUETOOTH_PRINTER_DEVICE_NOT_FOUND, isScanning = false) }
}

bluetoothLeScanner?.stopScan(scanCallback)

}

private fun BluetoothDevice.isPrinter(): Boolean{
return uuids.any { it.uuid == bondedUUID }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ internal object IosBluetoothConnection: BlueLine {
}
stateFlow.update { it.copy(bluetoothConnectionError = null, isScanning = true) }
centralManager.scanForPeripheralsWithServices(serviceUUIDs = listOf(delegate.UUID), options = null)
delay(10.seconds)
delay(5.seconds)
if (!stateFlow.value.discoveredPrinter){
stateFlow.update { it.copy(bluetoothConnectionError = ConnectionError.BLUETOOTH_PRINTER_DEVICE_NOT_FOUND, isScanning = false) }
}
Expand Down
Loading