function initPrinter() {
console.log('打印初始化开始');
var outputStream = bluetoothSocket.getOutputStream();
plus.android.importClass(outputStream);
OutputStreamWriter = plus.android.importClass('java.io.OutputStreamWriter')
writer = new OutputStreamWriter(outputStream, 'GBK')
plus.android.importClass(writer)
writer.write(0x1B)
writer.write(0x40)
writer.flush()
console.log('打印初始化结束');
}
function printTM(byteStr) {
if (!bluetoothSocket.isConnected()) {
console.log('断开了,需要重新连接,连接中');
bluetoothSocket.connect();
}
initPrinter();
var bytes = plus.android.invoke(byteStr, 'getBytes', 'gbk')
var length = bytes.length
outputStream = bluetoothSocket.getOutputStream()
plus.android.importClass(outputStream)
OutputStreamWriter = plus.android.importClass('java.io.OutputStreamWriter')
writer = new OutputStreamWriter(outputStream, 'GBK')
plus.android.importClass(writer)
writer.write(0x1B);
writer.write(0x40);
writer.flush();
var vstr = '{B' + byteStr;
console.log(vstr);
var bytes = plus.android.invoke(vstr, 'getBytes', 'gbk');
var length = bytes.length
writer.write(0x1D);
writer.write(0x6B);
writer.write(73);
writer.write(length);
writer.write(vstr);
writer.flush();
writer.write("\n");
writer.flush();
writer.write(byteStr);
writer.flush();
setAlignPosition(1);
console.log('条码打印成功');
ff(2);
}
function setAlignPosition(align) {
writer.write(0x1B);
writer.write(0x61);
writer.write(1);
writer.flush();
}
function printQrcode(byteStr) {
initPrinter();
console.log(byteStr);
var moduleSize = 8
var bytes = plus.android.invoke(byteStr, 'getBytes', 'gbk')
var length = bytes.length
outputStream = bluetoothSocket.getOutputStream()
plus.android.importClass(outputStream)
OutputStreamWriter = plus.android.importClass('java.io.OutputStreamWriter')
writer = new OutputStreamWriter(outputStream, 'GBK')
plus.android.importClass(writer)
console.log(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(69)
writer.write(48)
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(48)
writer.flush()
ff(2);
console.log('print Qrcode')
}
function ff(n) {
if (!bluetoothSocket.isConnected()) {
console.log('断开了,需要重新连接,连接中');
bluetoothSocket.connect();
}
if (bluetoothSocket.isConnected()) {
console.log('连接成功');
const line = n || 1;
var outputStream = bluetoothSocket.getOutputStream();
plus.android.importClass(outputStream);
outputStream.write(0x1B);
outputStream.write(0x64);
outputStream.write(line);
outputStream.flush();
}
}
```javascript
在这里插入代码片