zkcPDA手持终端机5502型号打印功能实现
记录一下uniapp开发安卓终端设备遇到的坑,也给和我同样经历着前端开发安卓设备的工友提供一点点帮助
<div id="btnPrint" type="button" value="打印" @click="print('00:11:22:33:44:55')">打印</div>
print(mac_address) {
if (!mac_address) {
mui.toast('请选择蓝牙打印机');
return;
}
var main = plus.android.runtimeMainActivity();
var BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
var UUID = plus.android.importClass("java.util.UUID");
var uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
var BAdapter = BluetoothAdapter.getDefaultAdapter();
var device = BAdapter.getRemoteDevice(mac_address);
plus.android.importClass(device);
var bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);
plus.android.importClass(bluetoothSocket);
if (!bluetoothSocket.isConnected()) {
console.log('检测到设备未连接,尝试连接....');
bluetoothSocket.connect();
}
console.log('设备已连接');
if (bluetoothSocket.isConnected()) {
var outputStream = bluetoothSocket.getOutputStream();
plus.android.importClass(outputStream);
var OutputStreamWriter = plus.android.importClass('java.io.OutputStreamWriter')
var writer = new OutputStreamWriter(outputStream, 'GBK')
plus.android.importClass(writer)
var byteStr = '101013Q73898'
var moduleSize = 15
var bytes = plus.android.invoke(byteStr, 'getBytes', 'gbk')
var length = bytes.length
writer.write(0x1D)
writer.write('(k')
writer.write(length + 3)
writer.write(0)
writer.write(49)
writer.write(80)
writer.write(48)
writer.write(byteStr)
writer.write(0x1D)
writer.write('(k')
writer.write(3)
writer.write(0)
writer.write(49)
writer.write(67)
writer.write(moduleSize)
writer.write(0x1D)
writer.write('(k')
writer.write(3)
writer.write(0)
writer.write(49)
writer.write(81)
writer.write(75)
writer.flush()
var string = "打印测试\r\n";
var bytes = plus.android.invoke(string, 'getBytes', 'gbk');
outputStream.write(bytes);
outputStream.flush();
outputStream.write('\n')
outputStream.flush()
device = null
bluetoothSocket.close();
}
},
|