diff --git a/composeApp/src/commonMain/kotlin/App.kt b/composeApp/src/commonMain/kotlin/App.kt index f2b8ef0..10e3472 100644 --- a/composeApp/src/commonMain/kotlin/App.kt +++ b/composeApp/src/commonMain/kotlin/App.kt @@ -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 @@ -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") }) diff --git a/library/build.gradle.kts b/library/build.gradle.kts index 45a2fb2..8808b7d 100644 --- a/library/build.gradle.kts +++ b/library/build.gradle.kts @@ -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 diff --git a/library/src/androidMain/kotlin/com/dilivva/blueline/connection/bluetooth/AndroidBluetoothConnection.kt b/library/src/androidMain/kotlin/com/dilivva/blueline/connection/bluetooth/AndroidBluetoothConnection.kt index 1854ecf..ec96d8e 100644 --- a/library/src/androidMain/kotlin/com/dilivva/blueline/connection/bluetooth/AndroidBluetoothConnection.kt +++ b/library/src/androidMain/kotlin/com/dilivva/blueline/connection/bluetooth/AndroidBluetoothConnection.kt @@ -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 @@ -170,7 +171,7 @@ internal object AndroidBluetoothConnection: BlueLine { return } val pairedDevices: Set? = 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) } @@ -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 } } } \ No newline at end of file diff --git a/library/src/iosMain/kotlin/com/dilivva/blueline/connection/bluetooth/IosBluetoothConnection.kt b/library/src/iosMain/kotlin/com/dilivva/blueline/connection/bluetooth/IosBluetoothConnection.kt index fa5b99b..22717d1 100644 --- a/library/src/iosMain/kotlin/com/dilivva/blueline/connection/bluetooth/IosBluetoothConnection.kt +++ b/library/src/iosMain/kotlin/com/dilivva/blueline/connection/bluetooth/IosBluetoothConnection.kt @@ -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) } }