JavaScript使用原生JS(native.JS)连接蓝牙打印机,打印一维码、二维码
使用说明:
- 代码已经过测试,可正常使用
- 测试蓝牙打印机为 商米(SUNMI)V2S plus打印机,此设备为一体机,与PDA连接
- 打印出的一维码和二维码中包含汉字与字母,使用其他设备扫描时可能会出现乱码的情况,出现此现象的原因为扫描设备的配置太低,无法解析包含汉字的二维码,使用手机扫描时即可正常显示
- 代码中的MAC地址一定要改为自己蓝牙打印机的蓝牙Mac地址
<!DOCTYPE html>
<html lang="zh"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title></title><link rel="stylesheet" type="text/css" href="../../css/mui.css" /><script src="../../js/mui.js" type="text/javascript" charset="utf-8"></script></head><body><header class="mui-bar mui-bar-nav" style="background-color: #007AFF;"><a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left" style="color: #fff;"></a><h1 class="mui-title" style="color: #fff;">蓝牙打印</h1></header><div class="mui-content" style="padding-top: 50px;"><button type="button" class="mui-btn mui-btn-blue mui-btn-block" id="print1">打印条形码</button><button type="button" class="mui-btn mui-btn-blue mui-btn-block" id="print2">打印二维码</button></div><script type="text/javascript">var bluetoothSocket = null;var bluetoothPrinterMac = "00:11:22:33:44:55"; //蓝牙打印机的MAC地址window.onload = function() {concatBlue();};//打印条形码document.getElementById("print1").addEventListener("tap", () => {printTM();});//打印二维码document.getElementById("print2").addEventListener("tap", () => {printQrcode();});//连接蓝牙function concatBlue() {mui.plusReady(function() {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();BAdapter.cancelDiscovery(); //停止扫描device = BAdapter.getRemoteDevice(bluetoothPrinterMac);plus.android.importClass(device);bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);plus.android.importClass(bluetoothSocket);});};/*** @Description:打印初始化*/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('打印初始化结束');}/*** @Description: 打印条码*/function printTM() {var byteStr = '这是条形码Demo';if (!bluetoothSocket.isConnected()) {console.log('断开了,需要重新连接,连接中');bluetoothSocket.connect();}initPrinter();var bytes = plus.android.invoke(byteStr, 'getBytes', 'gbk')var length = bytes.lengthoutputStream = 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;var bytes = plus.android.invoke(vstr, 'getBytes', 'gbk');var length = bytes.lengthwriter.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);}/*** 方法说明 设置文本对齐方式* @param align 打印位置 0:居左(默认) 1:居中 2:居右*/function setAlignPosition(align) {writer.write(0x1B);writer.write(0x61);writer.write(1);writer.flush();}/*** @Description: 二维码打印*/function printQrcode() {var byteStr = '这是二维码Demo';if (!bluetoothSocket.isConnected()) {console.log('断开了,需要重新连接,连接中');bluetoothSocket.connect();}// initinitPrinter();var moduleSize = 8var bytes = plus.android.invoke(byteStr, 'getBytes', 'gbk')var length = bytes.lengthoutputStream = bluetoothSocket.getOutputStream()plus.android.importClass(outputStream)OutputStreamWriter = plus.android.importClass('java.io.OutputStreamWriter')writer = new OutputStreamWriter(outputStream, 'GBK')plus.android.importClass(writer)// 缓存二维码数据writer.write(0x1D) // initwriter.write('(k') // adjust height of barcodewriter.write(length + 3) // plwriter.write(0) // phwriter.write(49) // cnwriter.write(80) // fnwriter.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) // plwriter.write(0) // phwriter.write(49) // cnwriter.write(81) // fnwriter.write(48) // mwriter.flush()// 二维码打印 结束ff(2);console.log('二维码打印结束')}/*** @Description:走纸*/function ff(n) {// bluetoothSocket.close();if (!bluetoothSocket.isConnected()) {console.log('断开了,需要重新连接,连接中');bluetoothSocket.connect();}if (bluetoothSocket.isConnected()) {console.log('连接成功');//initPrinter();const line = n || 1;var outputStream = bluetoothSocket.getOutputStream();plus.android.importClass(outputStream);outputStream.write(0x1B);outputStream.write(0x64);outputStream.write(line); // 行数outputStream.flush();}}</script></body>
</html>