function print(mac_address) { if (!mac_address) { mui.toast(\'请选择蓝牙打印机\'); return; } main = plus.android.runtimeMainActivity(); BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter"); UUID = plus.android.importClass("java.util.UUID"); uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); BAdapter = BluetoothAdapter.getDefaultAdapter(); device = BAdapter.getRemoteDevice(mac_address); plus.android.importClass(device); 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); // outputStream.write([0x1B,0x0E]); // 设置打印字符以正常宽度的2倍打印。 // outputStream.write([0x1B,0x14]); // 取消字符倍宽打印。 outputStream.write([0x1B, 0x57, 0x02]); // 设置字符倍宽倍高打印 outputStream.write([0x1B, 0x61, 0x01]); // 设置字符倍宽倍高打印 outputStream.write(plus.android.invoke("\n七天在线\n", \'getBytes\', \'gbk\')); //bytes outputStream.write([0x1B, 0x57, 0x01]); // 设置字符倍宽倍高打印 outputStream.write([0x1B, 0x61, 0x00]); // 选择对齐方式左对齐 outputStream.write(plus.android.invoke("\n结账单号:123456789\n", \'getBytes\', \'gbk\')); //bytes outputStream.write(plus.android.invoke("\n账单金额:100元\n", \'getBytes\', \'gbk\')); //bytes outputStream.write(plus.android.invoke("\n支付方式:支付宝\n", \'getBytes\', \'gbk\')); //bytes outputStream.write(plus.android.invoke("\n结账时间:2019年6月3日13:52:18\n", \'getBytes\', \'gbk\')); //bytes outputStream.write(plus.android.invoke("\n交易单号:8888888888\n\n\n\n\n\n\n", \'getBytes\', \'gbk\')); //bytes outputStream.write([0x1F, 0x11, 0x00, 0x16, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x62, 0x61, 0x69, 0x64, 0x75, 0x2e, 0x63, 0x6f, 0x6d, 0x2f ]); // 打印二维码 outputStream.flush(); device = null //这里关键 bluetoothSocket.close(); //必须关闭蓝牙连接否则意外断开的话打印错误 } }
☞:ESC参考博客