Skip to content

Commit

Permalink
Merge pull request #11 from Dilivva/development
Browse files Browse the repository at this point in the history
Fix bug with state issues regarding printer connection
  • Loading branch information
ayodelekehinde authored Jun 15, 2024
2 parents 185f005 + def5261 commit c5753f4
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 8 deletions.
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.7"
val versionTxt = "0.0.8"
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 @@ -97,7 +97,7 @@ 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, isConnecting = false) }
stateFlow.update { state -> state.copy(isConnected = true, isConnecting = false, bluetoothConnectionError = null) }
gatt?.discoverServices()
}else{
stateFlow.update {
Expand Down Expand Up @@ -189,7 +189,7 @@ internal class AndroidBluetoothConnection: BlueLine {
}

override fun connect() {
stateFlow.update { it.copy(isConnecting = true) }
stateFlow.update { it.copy(isConnecting = true, bluetoothConnectionError = null) }
val state = stateFlow.value
if (!state.isBluetoothReady || state.isConnected){
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ interface BlueLine {
* * Call [init] before scanning to ensure Bluetooth is ready.
* * This method prioritizes the first discovered printer and might ignore others. Consider revising this behavior if you need to select a specific printer.
*
* @throws CancellationException if the coroutine is cancelled during the scan.
* @throws [CancellationException] if the coroutine is cancelled during the scan.
*/
suspend fun scanForPrinters()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ enum class ConnectionError {
* @property bluetoothConnectionError An optional `ConnectionError` object containing details about any errors encountered during Bluetooth connection attempts.
* @property isPrinting Indicates if a print job is currently being sent to the printer.
* @property isScanning Indicates if the system is actively searching for printers (relevant for discovery phase).
* @property isConnecting Indicates if the system is attempting to connect to the printer
*/
data class ConnectionState(
val deviceName: String = "Searching",
Expand Down
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, isConnecting = false) }
stateFlow.update { state -> state.copy(isConnected = it, isConnecting = false, bluetoothConnectionError = if (it) null else ConnectionError.BLUETOOTH_PRINTER_DEVICE_NOT_FOUND) }
},
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, isConnecting = false) }
stateFlow.update { state -> state.copy(canPrint = true) }
val mtu = peripheral?.maximumWriteValueLengthForType(CBCharacteristicWriteWithResponse)?.toInt()
printerHelper.mtu = mtu ?: 20
},
Expand Down Expand Up @@ -121,7 +121,7 @@ internal object IosBluetoothConnection: BlueLine {
}

override fun connect() {
stateFlow.update { it.copy(isConnecting = true) }
stateFlow.update { it.copy(isConnecting = true, bluetoothConnectionError = null) }
val state = stateFlow.value
if (!state.isBluetoothReady || state.isConnected){
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ internal class ScanningManager(
didDisconnectPeripheral: CBPeripheral,
error: NSError?
) {
println("Disconnected")
onConnection(false)
}

Expand Down

0 comments on commit c5753f4

Please sign in to comment.