UsbPrinterUtils.kt
4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.diligrp.printer
import android.annotation.SuppressLint
import android.content.Context
import android.hardware.usb.UsbManager
import android.os.Handler
import android.os.Message
import android.util.Log
import android.widget.Toast
import com.android.print.sdk.PrinterConstants
import com.android.print.sdk.PrinterInstance
import com.diligrp.printer.utils.IPrinterOpertion
import com.diligrp.printer.utils.UsbOperation
/**
* UsbPrinterUtils
*
* @Description:
* @Author:
* @CreateDate: 2025/8/11 12:38
*/
class UsbPrinterUtils {
private var mPrinterOpertion: IPrinterOpertion? = null
private var mPrinter: PrinterInstance? = null
private var isConnected = false //是否已经建立了连接
private var mContext: Context? = null
companion object {
const val LEFT = 0
const val RIGHT = 2
const val CENTER = 1
const val LINE_BREAK = "\n"
const val GAP_LINE = "--------------------------------${LINE_BREAK}"
@SuppressLint("StaticFieldLeak")
private var _instance: UsbPrinterUtils? = null
fun getInstance(): UsbPrinterUtils {
if (_instance == null) {
_instance = UsbPrinterUtils()
}
return _instance!!
}
}
fun initDevice(context: Context?) {
if (context == null) {
return
}
mContext = context
mPrinterOpertion = UsbOperation(mContext, mHandler)
val manager = mContext?.getSystemService(Context.USB_SERVICE) as UsbManager
mPrinterOpertion!!.usbAutoConn(manager);
}
fun prepare() {
if (mPrinter != null && isConnected) {
mPrinter?.init()
}
}
fun printTextLeft(str: String?) {
if (mPrinter != null && isConnected && !str.isNullOrEmpty()) {
mPrinter?.setPrinter(PrinterConstants.Command.ALIGN, LEFT)
mPrinter?.printText(str)
}
}
fun printTextCenter(str: String?) {
Log.d("Test", "printTextCenter $mPrinter ------- $isConnected --------> $str")
if (mPrinter != null && isConnected && !str.isNullOrEmpty()) {
mPrinter?.setPrinter(PrinterConstants.Command.ALIGN, CENTER)
mPrinter?.printText(str)
}
}
fun printTextRight(str: String?) {
if (mPrinter != null && isConnected && !str.isNullOrEmpty()) {
mPrinter?.setPrinter(PrinterConstants.Command.ALIGN, RIGHT)
mPrinter?.printText(str)
}
}
fun printEmptyLine(count: Int) {
if (mPrinter != null && isConnected) {
mPrinter?.setPrinter(PrinterConstants.Command.ALIGN, LEFT)
mPrinter?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, count)
}
}
fun printGapLine() {
if (mPrinter != null && isConnected) {
mPrinter?.setPrinter(PrinterConstants.Command.ALIGN, LEFT)
mPrinter?.printText(GAP_LINE)
}
}
fun onRelease() {
mHandler.removeCallbacksAndMessages(null)
isConnected = false
mPrinterOpertion?.close()
mPrinterOpertion = null
mPrinter = null
mContext = null
_instance = null
}
//用于接受连接状态消息的 Handler
private val mHandler: Handler by lazy {
@SuppressLint("HandlerLeak")
object : Handler() {
@SuppressLint("HandlerLeak")
override fun handleMessage(msg: Message) {
super.handleMessage(msg)
when (msg.what) {
PrinterConstants.Connect.SUCCESS -> {
isConnected = true;
mPrinter = mPrinterOpertion?.getPrinter()
}
PrinterConstants.Connect.FAILED -> {
showToast("打印机连接失败")
}
PrinterConstants.Connect.CLOSED -> {
isConnected = false;
// showToast("打印机已关闭")
}
PrinterConstants.Connect.NODEVICE -> {
isConnected = false;
showToast("未检测到打印机")
}
}
}
}
}
private fun showToast(msg: String?) {
if (msg.isNullOrEmpty() || mContext == null) {
return
}
Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show()
}
}