fix: 修复每次打印后关闭设备导致二次打印报设备未打开
移除 PrintHelper/PrintJob/PrinterManager/PrinterTestScreen 中每次打印后的 closeDevice() 调用, 设备生命周期由 MainActivity 管理(onStart 打开,onDestroy 关闭),避免反复 close->open 导致 SDK open() 失败。
This commit is contained in:
@@ -4,7 +4,6 @@ import com.example.ops_android.data.model.Item
|
|||||||
import com.example.ops_android.data.model.PurchaseOrder
|
import com.example.ops_android.data.model.PurchaseOrder
|
||||||
import com.example.ops_android.data.model.WorkOrder
|
import com.example.ops_android.data.model.WorkOrder
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.delay
|
|
||||||
import kotlinx.coroutines.sync.Mutex
|
import kotlinx.coroutines.sync.Mutex
|
||||||
import kotlinx.coroutines.sync.withLock
|
import kotlinx.coroutines.sync.withLock
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
@@ -36,14 +35,7 @@ object PrintHelper {
|
|||||||
} else {
|
} else {
|
||||||
printerManager.addLineFeed((unwindMm / 7).toInt().coerceAtLeast(2))
|
printerManager.addLineFeed((unwindMm / 7).toInt().coerceAtLeast(2))
|
||||||
}
|
}
|
||||||
printerManager.commit().fold(
|
printerManager.commit()
|
||||||
onSuccess = {
|
|
||||||
delay(1000)
|
|
||||||
printerManager.closeDevice()
|
|
||||||
Result.success(Unit)
|
|
||||||
},
|
|
||||||
onFailure = { Result.failure(it) }
|
|
||||||
)
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Result.failure(e)
|
Result.failure(e)
|
||||||
}
|
}
|
||||||
@@ -73,14 +65,7 @@ object PrintHelper {
|
|||||||
} else {
|
} else {
|
||||||
printerManager.addLineFeed((unwindMm / 7).toInt().coerceAtLeast(2))
|
printerManager.addLineFeed((unwindMm / 7).toInt().coerceAtLeast(2))
|
||||||
}
|
}
|
||||||
printerManager.commit().fold(
|
printerManager.commit()
|
||||||
onSuccess = {
|
|
||||||
delay(1000)
|
|
||||||
printerManager.closeDevice()
|
|
||||||
Result.success(Unit)
|
|
||||||
},
|
|
||||||
onFailure = { Result.failure(it) }
|
|
||||||
)
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Result.failure(e)
|
Result.failure(e)
|
||||||
}
|
}
|
||||||
@@ -111,14 +96,7 @@ object PrintHelper {
|
|||||||
} else {
|
} else {
|
||||||
printerManager.addLineFeed((unwindMm / 7).toInt().coerceAtLeast(2))
|
printerManager.addLineFeed((unwindMm / 7).toInt().coerceAtLeast(2))
|
||||||
}
|
}
|
||||||
printerManager.commit().fold(
|
printerManager.commit()
|
||||||
onSuccess = {
|
|
||||||
delay(1000)
|
|
||||||
printerManager.closeDevice()
|
|
||||||
Result.success(Unit)
|
|
||||||
},
|
|
||||||
onFailure = { Result.failure(it) }
|
|
||||||
)
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Result.failure(e)
|
Result.failure(e)
|
||||||
}
|
}
|
||||||
@@ -149,14 +127,7 @@ object PrintHelper {
|
|||||||
} else {
|
} else {
|
||||||
printerManager.addLineFeed((unwindMm / 7).toInt().coerceAtLeast(2))
|
printerManager.addLineFeed((unwindMm / 7).toInt().coerceAtLeast(2))
|
||||||
}
|
}
|
||||||
printerManager.commit().fold(
|
printerManager.commit()
|
||||||
onSuccess = {
|
|
||||||
delay(1000)
|
|
||||||
printerManager.closeDevice()
|
|
||||||
Result.success(Unit)
|
|
||||||
},
|
|
||||||
onFailure = { Result.failure(it) }
|
|
||||||
)
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Result.failure(e)
|
Result.failure(e)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,8 +46,6 @@ class PrintJob(private val manager: PrinterManager) {
|
|||||||
if (lineFeeds > 0) manager.addLineFeed(lineFeeds).getOrThrow()
|
if (lineFeeds > 0) manager.addLineFeed(lineFeeds).getOrThrow()
|
||||||
|
|
||||||
manager.commit().getOrThrow()
|
manager.commit().getOrThrow()
|
||||||
Thread.sleep(1500)
|
|
||||||
manager.closeDevice().getOrThrow()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 初始化 → 添加内容 → 提交(不关闭) */
|
/** 初始化 → 添加内容 → 提交(不关闭) */
|
||||||
|
|||||||
@@ -301,8 +301,6 @@ class PrinterManager(private val context: Context) {
|
|||||||
initDevice().getOrThrow()
|
initDevice().getOrThrow()
|
||||||
addText(text).getOrThrow()
|
addText(text).getOrThrow()
|
||||||
commit().getOrThrow()
|
commit().getOrThrow()
|
||||||
Thread.sleep(1500)
|
|
||||||
closeDevice().getOrThrow()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun quickLabel(text: String): Result<Unit> = runCatching {
|
fun quickLabel(text: String): Result<Unit> = runCatching {
|
||||||
@@ -310,8 +308,6 @@ class PrinterManager(private val context: Context) {
|
|||||||
enableMarkDetection(true).getOrThrow()
|
enableMarkDetection(true).getOrThrow()
|
||||||
addText(text).getOrThrow()
|
addText(text).getOrThrow()
|
||||||
commit().getOrThrow()
|
commit().getOrThrow()
|
||||||
Thread.sleep(1500)
|
|
||||||
closeDevice().getOrThrow()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 创建链式打印任务 */
|
/** 创建链式打印任务 */
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ import androidx.compose.ui.unit.dp
|
|||||||
import androidx.compose.ui.unit.sp
|
import androidx.compose.ui.unit.sp
|
||||||
import com.example.ops_android.ui.LocalAppContainer
|
import com.example.ops_android.ui.LocalAppContainer
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.delay
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
@@ -120,8 +119,6 @@ fun PrinterTestScreen(onBack: () -> Unit) {
|
|||||||
val pm = printerManager!!
|
val pm = printerManager!!
|
||||||
pm.initDevice().getOrThrow()
|
pm.initDevice().getOrThrow()
|
||||||
pm.commit().getOrThrow()
|
pm.commit().getOrThrow()
|
||||||
delay(1000)
|
|
||||||
pm.closeDevice().getOrThrow()
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
modifier = Modifier.fillMaxWidth(),
|
modifier = Modifier.fillMaxWidth(),
|
||||||
@@ -137,8 +134,6 @@ fun PrinterTestScreen(onBack: () -> Unit) {
|
|||||||
pm.initDevice().getOrThrow()
|
pm.initDevice().getOrThrow()
|
||||||
pm.addLineFeed(5)
|
pm.addLineFeed(5)
|
||||||
pm.commit().getOrThrow()
|
pm.commit().getOrThrow()
|
||||||
delay(1000)
|
|
||||||
pm.closeDevice().getOrThrow()
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
modifier = Modifier.fillMaxWidth(),
|
modifier = Modifier.fillMaxWidth(),
|
||||||
@@ -155,8 +150,6 @@ fun PrinterTestScreen(onBack: () -> Unit) {
|
|||||||
pm.initDevice().getOrThrow()
|
pm.initDevice().getOrThrow()
|
||||||
pm.addLineFeed(10)
|
pm.addLineFeed(10)
|
||||||
pm.commit().getOrThrow()
|
pm.commit().getOrThrow()
|
||||||
delay(1000)
|
|
||||||
pm.closeDevice().getOrThrow()
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
modifier = Modifier.fillMaxWidth(),
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
|||||||
Reference in New Issue
Block a user