Skip to content

Commit

Permalink
Fix bug with scanning printer on Android with bonded devices
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayodele Kehinde authored and skyestudiosDev committed Jun 14, 2024
1 parent 52ac8d9 commit 117efbf
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 24 deletions.
36 changes: 20 additions & 16 deletions composeApp/src/commonMain/kotlin/App.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
Expand All @@ -8,7 +7,9 @@ import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.AlertDialog
import androidx.compose.material.Button
import androidx.compose.material.CircularProgressIndicator
Expand All @@ -35,7 +36,6 @@ import com.dilivva.blueline.builder.buildPrintData
import com.dilivva.blueline.connection.bluetooth.BlueLine
import com.dilivva.blueline.connection.bluetooth.ConnectionError
import com.dilivva.blueline.connection.bluetooth.ConnectionState
import escposprinter.composeapp.generated.resources.Res
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
Expand Down Expand Up @@ -143,18 +143,18 @@ fun ConnectionItem(
var image by remember { mutableStateOf<ImageBitmap?>(null) }
var imageBytes by remember { mutableStateOf(byteArrayOf()) }

LaunchedEffect(Unit){
val bytes = Res.readBytes("drawable/label.png")
imageBytes = bytes
image = getPlatform().toImage(bytes)
}
// LaunchedEffect(Unit){
// val bytes = Res.readBytes("drawable/label.png")
// imageBytes = bytes
// image = getPlatform().toImage(bytes)
// }

Box(
modifier = Modifier.fillMaxWidth().padding(20.dp)
.background(Color.LightGray.copy(alpha = 0.2f), RoundedCornerShape(15.dp))
.padding(8.dp)
){
Column(modifier = Modifier.fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(15.dp)){
Column(modifier = Modifier.fillMaxWidth().verticalScroll(rememberScrollState(), enabled = true), verticalArrangement = Arrangement.spacedBy(15.dp)){
Text(
text = connectionState.deviceName,
fontSize = 14.sp,
Expand Down Expand Up @@ -190,6 +190,11 @@ fun ConnectionItem(
enabled = !connectionState.isConnected && connectionState.discoveredPrinter
){
Text("Connect")
if (connectionState.isConnecting) {
CircularProgressIndicator(
color = Color.White
)
}
}

Button(
Expand Down Expand Up @@ -223,14 +228,13 @@ fun ConnectionItem(
}
}


image?.let {
Image(
bitmap = it,
contentDescription = null,
modifier = Modifier.fillMaxWidth()
)
}
// image?.let {
// Image(
// bitmap = it,
// contentDescription = null,
// modifier = Modifier.fillMaxWidth()
// )
// }
}
}
}
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.6"
val versionTxt = "0.0.7"
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 @@ -25,6 +25,7 @@ package com.dilivva.blueline.connection.bluetooth

import android.annotation.SuppressLint
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothClass
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothGatt
import android.bluetooth.BluetoothGattCallback
Expand Down Expand Up @@ -55,7 +56,6 @@ internal class 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 var bluetoothLeScanner = bluetoothAdapter?.bluetoothLeScanner
Expand Down Expand Up @@ -97,10 +97,16 @@ internal class AndroidBluetoothConnection: BlueLine {

override fun onConnectionStateChange(gatt: BluetoothGatt?, status: Int, newState: Int) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
stateFlow.update { state -> state.copy(isConnected = true) }
stateFlow.update { state -> state.copy(isConnected = true, isConnecting = false) }
gatt?.discoverServices()
}else{
stateFlow.update { state -> state.copy(isConnected = false) }
stateFlow.update {
it.copy(
isConnecting = false,
isConnected = false,
bluetoothConnectionError = ConnectionError.BLUETOOTH_PRINTER_DEVICE_NOT_FOUND
)
}
}
}

Expand Down Expand Up @@ -183,6 +189,7 @@ internal class AndroidBluetoothConnection: BlueLine {
}

override fun connect() {
stateFlow.update { it.copy(isConnecting = true) }
val state = stateFlow.value
if (!state.isBluetoothReady || state.isConnected){
return
Expand Down Expand Up @@ -235,7 +242,7 @@ internal class AndroidBluetoothConnection: BlueLine {
}

private fun BluetoothDevice.isPrinter(): Boolean{
return uuids.any { it.uuid == bondedUUID }
return bluetoothClass.majorDeviceClass == BluetoothClass.Device.Major.IMAGING && bluetoothClass.deviceClass == 1664
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,6 @@ data class ConnectionState(
val isBluetoothReady: Boolean = false,
val bluetoothConnectionError: ConnectionError? = null,
val isPrinting: Boolean = false,
val isScanning: Boolean = false
val isScanning: Boolean = false,
val isConnecting:Boolean = false
)
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ internal object IosBluetoothConnection: BlueLine {
stateFlow.update { state -> state.copy(deviceName = device.name.orEmpty(), discoveredPrinter = true, isScanning = false) }
},
onConnection = {
stateFlow.update { state -> state.copy(isConnected = it) }
stateFlow.update { state -> state.copy(isConnected = it, isConnecting = false) }
},
onBluetoothReady = {
stateFlow.update { state ->
Expand All @@ -71,7 +71,7 @@ internal object IosBluetoothConnection: BlueLine {
private val peripheralManager = PeripheralManager(
onCharacter = { character ->
characteristic = character
stateFlow.update { state -> state.copy(canPrint = true) }
stateFlow.update { state -> state.copy(canPrint = true, isConnecting = false) }
val mtu = peripheral?.maximumWriteValueLengthForType(CBCharacteristicWriteWithResponse)?.toInt()
printerHelper.mtu = mtu ?: 20
},
Expand Down Expand Up @@ -121,6 +121,7 @@ internal object IosBluetoothConnection: BlueLine {
}

override fun connect() {
stateFlow.update { it.copy(isConnecting = true) }
val state = stateFlow.value
if (!state.isBluetoothReady || state.isConnected){
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ internal class PeripheralManager(
onWrite(error == null)
}



}

0 comments on commit 117efbf

Please sign in to comment.