js连接蓝牙打印机打印一维码和二维码

news/2024/11/24 1:57:26/

JavaScript使用原生JS(native.JS)连接蓝牙打印机,打印一维码、二维码

使用说明:

  1. 代码已经过测试,可正常使用
  2. 测试蓝牙打印机为 商米(SUNMI)V2S plus打印机,此设备为一体机,与PDA连接
  3. 打印出的一维码和二维码中包含汉字与字母,使用其他设备扫描时可能会出现乱码的情况,出现此现象的原因为扫描设备的配置太低,无法解析包含汉字的二维码,使用手机扫描时即可正常显示
  4. 代码中的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>

http://www.ppmy.cn/news/224693.html

相关文章

打印机共享怎么设置?如何设置打印机共享?

打印机共享怎么设置&#xff1f;如何设置打印机共享&#xff1f; 想要让两台打印机或者是多台打印机可以同时使用&#xff0c;首先要了解如何设置并共享局域网内已连接好电脑的打印机&#xff0c;之后需要解决的是局域网内其它电脑如何找到刚才那台电脑共享出去的打印机&#…

Deepin Linux系统怎安装打印机? 兄弟1618w打印机驱动安装图文教程

Deepin系统作为国产的一款电脑操作系统&#xff0c;拥有极为非常美观的UI界面。很多不熟悉该操作系统的朋友都不知道该怎么安装打印机驱动&#xff0c;今天我们就以兄弟1618w打印机为例&#xff0c;分享驱动下载&#xff0c;安装&#xff0c;调试的过程。 电脑环境和打印机型号…

win7或win10系统的打印机共享设置步骤

打印机共享怎么设置&#xff1f;如何设置打印机共享&#xff1f;要实现两台打印机或者是多台打印机共享&#xff0c;首先要了解如何设置并共享局域网内已连接好电脑的打印机&#xff0c;之后需要解决的是局域网内其它电脑如何找到刚才那台电脑共享出去的打印机&#xff0c;并且…

win10 链接github

https://github.com.ipaddress.com/ 在链接里面找到ip 打开系统host文件 Windows 系统&#xff1a;C:\Windows\System32\drivers\etc\hosts Linux 系统&#xff1a;/etc/hosts Mac&#xff08;苹果电脑&#xff09;系统&#xff1a;/etc/hosts Android&#xff08;安卓&#x…

外贸人注意!这件事不能再对客户承诺了!

你还在配合客户低开发票吗&#xff1f; 本文目录&#xff1a; 什么是低开发票&#xff1f; 低开发票有什么风险&#xff1f; 哪些国家客户喜欢低开发票&#xff1f; 哪些国家低开发票会被抓&#xff1f; 很多人认为客户索要低开发票偷税漏税是人之常情。为了加强合作关系&a…

代码分析工具 自定义插件——检查any类型的代码

代码分析工具 自定义插件——检查any类型的代码 本文是基于掘金小册《前端依赖治理&#xff1a;代码分析工具开发实战》 转载于我的掘金博客&#xff0c;并非抄袭 gitHub仓库&#xff1a;https://github.com/huang-jaskld/code-analysis/tree/master 逻辑步骤 源代码过于庞大&a…

Nginx location匹配优先级 与 Rewrite

目录 前言locationlocation 常用的匹配规则location 优先级实例演示 rewrite前言rewrite跳转实现rewrite 执行顺序语法格式rewrite全局变量实例演示 前言 从功能看 rewrite 和 location 似乎有点像&#xff0c;都能实现跳转&#xff0c;主要区别在于 rewrite 是在同一个域名内…

uniapp 中 uni.navigateTo跳转其他页面 并且带参数

1.可以带某一具体值 A页面跳转到B页面&#xff0c;并向B页面传递参数id,用?连接参数&#xff0c;参数之前用&连接 A页面&#xff1a; let id 5 uni.navigateTo({ url: /pages/B?id${id}}) B页面&#xff1a; 在onLoad中接收到A个页面传递的参数 onLoad(option){ …