如何解决在某些设备上使用热POS打印机时,UsbConnection.bulktransfer返回-1
我正在研究项目,我需要使用热敏打印机打印支票,该支票已连接到android设备。一切在xiaomi(android 9)上都可以正常工作,但是当尝试在其他设备(三星galaxy选项卡(android 8)和galaxy j3(android 6))上打印支票时,bulktransfer返回-1。
MainActivity类:Activity(){
/***/
/***/
var mPermissionIntent: PendingIntent? = null
var device: UsbDevice? = null
var manager: UsbManager? = null
var connection: UsbDeviceConnection? = null
var myInterface: UsbInterface? = null
var myEndpoint: UsbEndpoint? = null
var usbRequest: UsbRequest? = null
fun toast(value: String) {
Toast.makeText(this,value,Toast.LENGTH_SHORT).show()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Log.d("FROMAIR","working")
connect!!.setOnClickListener {
textView!!.text = ""
checkInfo()
}
print!!.setOnClickListener {
printSomeData()
}
val bmpWidth = 180
sasha_btn.setOnClickListener {
val tv = Integer
.toHexString(if (bmpWidth % 8 == 0) bmpWidth / 8 else bmpWidth / 8 + 1)
toast(tv)
}
}
private fun printSomeData() {
GlobalScope.launch(Dispatchers.IO) {
synchronized(this) {
try {
var bytes: ByteArray = byteArrayOf(0x1B,0x74,0x11)
connection!!.bulkTransfer(myEndpoint,bytes,bytes.size,10000)
for (i in 0..4) {
//print text
var text: String = to_printer_text.text.toString()
bytes = text.toByteArray(Charset.forName("Cp866"))
var res = connection!!.bulkTransfer(myEndpoint,10000)
bytes = byteArrayOf(0x0A)
connection!!.bulkTransfer(myEndpoint,10000)
}
}catch (e:Exception){
Log.d("EXCEPTION","UNABLE TO PRINT CHECK")
e.printStackTrace()
}
}
}
}
private fun checkInfo() {
manager = getSystemService(USB_SERVICE) as UsbManager
/*
* this block required if you need to communicate to USB devices it's
* take permission to device
* if you want than you can set this to which device you want to communicate
*/
// ------------------------------------------------------------------
mPermissionIntent = PendingIntent.getBroadcast(
this,Intent(
ACTION_USB_PERMISSION
),0
)
val filter = IntentFilter(ACTION_USB_PERMISSION)
registerReceiver(mUsbReceiver,filter)
// -------------------------------------------------------------------
val deviceList = manager!!.deviceList
val deviceIterator: Iterator<UsbDevice> = deviceList.values.iterator()
var i = ""
while (deviceIterator.hasNext()) {
device = deviceIterator.next()
manager!!.requestPermission(device,mPermissionIntent)
i += """
Interface0: ${device!!.getInterface(0).id}
Endpoint0: ${device!!.getInterface(0).getEndpoint(0).direction}
Interface1: ${device!!.getInterface(1).id}
Endpoint0: ${device!!.getInterface(1).getEndpoint(0).direction}
Interface1: ${device!!.getInterface(1).id}
Endpoint1: ${device!!.getInterface(1).getEndpoint(1).direction}
"""
}
textView!!.text = i
}
private val mUsbReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context,intent: Intent) {
val action = intent.action
if (ACTION_USB_PERMISSION == action) {
synchronized(this) {
val device = intent
.getParcelableExtra<Parcelable>(UsbManager.EXTRA_DEVICE) as UsbDevice?
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED,false)) {
if (device != null) {
myInterface = device.getInterface(1)
myEndpoint = myInterface!!.getEndpoint(0)
connection = manager!!.openDevice(device)
usbRequest = UsbRequest()
usbRequest!!.initialize(connection,myEndpoint)
} else {
}
} else {
Log.d("ERROR","permission denied for device $device")
Toast.makeText(
context,"PERMISSION DENIED FOR THIS DEVICE",Toast.LENGTH_SHORT
).show()
}
}
}
}
}
companion object {
private const val ACTION_USB_PERMISSION = "com.mitapp.myapplication.USB_PERMISSION"
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。