2023年12月11日发(作者:少梦影)
H5开发连接蓝牙打印机打印标签(斑马ZR628)
1.连接蓝牙打印机(先用手机自带蓝牙进行配对),然后绑定出已配对的蓝牙设备(用来选择/切换打印机之用),代码如下
已配对蓝牙设备,中显示的就是已连接的,点击一下即可
代码:
1
2
3
4
5
6
7
8
9
10
11
12
13 .title {
14 margin: 20px 15px 7px;
15 color: #6d6d72;
16 font-size: 15px;
17 }
18
19 .mui-bar-nav {
20 background: #004A86;
21 }
22
23 .mui-icon-back:before,
24 .mui-icon-left-nav:before,
25 .mui-bar .mui-title {
26 color: #ffffff;
27 }
28
29 .mui-content>.mui-table-view:first-child {
30 margin-top: 0px;
31 }
32
33 .mui-btn {
34 padding: 1px 1px;
35 }
36 /*.mui-btn-primary{ border: 1px solid #004A86; background: #004A86;}*/
37
38 .mui-icon-search:before {
39 font-size: 16px;
40 }
41
42
43
44
45
101
102
103
104
105
106
107
108
109
110 //address=""搜索蓝牙//address=设备mac地址,自动配对给出mac地址的设备
111 function searchDevices(address) {
112 //注册类
113 var main = eMainActivity();
114 var IntentFilter = Class('Filter');
115 var BluetoothAdapter = Class("othAdapter");
116 var BluetoothDevice = Class("othDevice");
117 var BAdapter = aultAdapter();
118 ("开始搜索设备");
119 var filter = new IntentFilter();
120 var bdevice = new BluetoothDevice();
121 var on = null;
122 var un = null;
123 var vlist1 = mentById('list1'); //注册容器用来显示未配对设备
124 TML = ''; //清空容器
125 var vlist2 = mentById('list2'); //注册容器用来显示未配对设备
126 TML = ''; //清空容器
127 var button1 = mentById('bt1');
128 ed = true;
129 = '正在搜索请稍候...';
130 iscovery(); //开启搜索
131 var receiver;
132 receiver = ents('astReceiver', {
133 onReceive: function(context, intent) { //实现onReceiver回调函数
134 Class(intent); //通过intent实例引入intent类,方便以后的‘.’操作
135 (ion()); //获取action
136 if(ion() == "ERY_FINISHED") {
137 sterReceiver(receiver); //取消监听
138 ed = false;
139 = '搜索设备';
140 ("搜索结束")
141 } else {
142
143 BleDevice = celableExtra(_DEVICE);
144 //判断是否配对
145 if(dState() == _NONE) {
146 ("未配对蓝牙设备:" + e() + ' ' + ress());
147 //参数如果跟取得的mac地址一样就配对
148 if(address == ress()) {
149 if(Bond()) { //配对命令.createBond()
150 ("配对成功");
151 var li2 = Element('li'); //注册
152 ribute('id', ress()); //打印机mac地址
153 ribute('onclick', 'print(id)'); //注册click点击列表进行打印
154 ribute('class', 'mui-table-view-cell');
155 ext = e();
156 Child(li2);
157 }
158
159 } else {
160 if(e() != on) { //判断防止重复添加
161 var li1 = Element('li'); //注册
162 ribute('id', ress()); //打印机mac地址
163 ribute('onclick', 'searchDevices(id)'); //注册click点击列表进行配对
164 ribute('class', 'mui-table-view-cell');
165 on = e();
166 ext = on;
167 Child(li1);
168
169 }
170
171 }
172 } else {
173 if(e() != un) { //判断防止重复添加
174 ("已配对蓝牙设备:" + e() + ' ' + ress());
175 var li2 = Element('li'); //注册
176 ribute('id', ress()); //打印机mac地址
177 ribute('onclick', 'print(id)'); //注册click点击列表进行打印
178 ribute('class', 'mui-table-view-cell'); //注册click点击列表进行打印
179 un = e();
180 ext = un;
181 Child(li2);
182 }
183 }
184 }
185
186 }
187 });
188
189 ion(_FOUND);
190 ion(_DISCOVERY_STARTED);
191 ion(_DISCOVERY_FINISHED);
192 ion(_STATE_CHANGED);
193
194 erReceiver(receiver, filter); //注册监听
195 }
196
197 var device = null,
198 BAdapter = null,
199 BluetoothAdapter = null,
200 uuid = null,
201 main = null,
202 bluetoothSocket = null;
203
204 function print(mac_address) {
205 if(!mac_address) {
206 ('请选择蓝牙打印机');
207 return;
208 }
209 //alert(mac_address);
210 m("macaddress", mac_address);
211 alert('打印机已配对成功');
212 return;
213 }
214
215
216
2.打印页面,只有一个按钮(生成条码),见下图,代码如下
注:打印是用的斑马ZPL指令(如需学习 百度很多~)
代码如下:方法:printIndex(mol)
1
2 //address=""搜索蓝牙//address=设备mac地址,自动配对给出mac地址的设备
3 function searchDevices(address) {
4 //注册类
5 var main = eMainActivity();
6 var IntentFilter = Class('Filter');
7 var BluetoothAdapter = Class("othAdapter");
8 var BluetoothDevice = Class("othDevice");
9 var BAdapter = aultAdapter();
10 ("开始搜索设备");
11 var filter = new IntentFilter();
12 var bdevice = new BluetoothDevice();
13 var on = null;
14 var un = null;
15 var vlist1 = mentById('list1'); //注册容器用来显示未配对设备
16 TML = ''; //清空容器
17 var vlist2 = mentById('list2'); //注册容器用来显示未配对设备
18 TML = ''; //清空容器
19 var button1 = mentById('bt1');
20 ed = true;
21 = '正在搜索请稍候';
22 iscovery(); //开启搜索
23 var receiver;
24 receiver = ents('astReceiver', {
25 onReceive: function(context, intent) { //实现onReceiver回调函数
26 Class(intent); //通过intent实例引入intent类,方便以后的‘.’操作
27 (ion()); //获取action
28 if(ion() == "ERY_FINISHED") {
29 sterReceiver(receiver); //取消监听
30 ed = false;
31 = '搜索设备';
32 ("搜索结束")
33 } else {
34 BleDevice = celableExtra(_DEVICE);
35 //判断是否配对
36 if(dState() == _NONE) {
37 ("未配对蓝牙设备:" + e() + ' ' + ress());
38 //参数如果跟取得的mac地址一样就配对
39 if(address == ress()) {
40 if(Bond()) { //配对命令.createBond()
41 ("配对成功");
42 var li2 = Element('li'); //注册
43 ribute('id', ress()); //打印机mac地址
44 ribute('onclick', 'printIndex(id)'); //注册click点击列表进行打印
45 ext = e();
46 Child(li2);
47 }
48
49 } else {
50 if(e() != on) { //判断防止重复添加
51 var li1 = Element('li'); //注册
52 ribute('id', ress()); //打印机mac地址
53 ribute('onclick', 'searchDevices(id)'); //注册click点击列表进行配对
54 on = e();
55 ext = on;
56 Child(li1);
57
58 }
59
60 }
61 } else {
62 if(e() != un) { //判断防止重复添加
63 ("已配对蓝牙设备:" + e() + ' ' + ress());
64 var li2 = Element('li'); //注册
65 ribute('id', ress()); //打印机mac地址
66
67 ribute('onclick', 'printIndex(id)'); //注册click点击列表进行打印
68 un = e();
69 ext = un;
70 Child(li2);
71 }
72 }
73 }
74
75 }
76 });
77
78 ion(_FOUND);
79 ion(_DISCOVERY_STARTED);
80 ion(_DISCOVERY_FINISHED);
81 ion(_STATE_CHANGED);
82
83 erReceiver(receiver, filter); //注册监听
84 }
85
86 var device = null,
87 BAdapter = null,
88 BluetoothAdapter = null,
89 uuid = null,
90 main = null,
91 bluetoothSocket = null;
92
93 function printIndex(molList) {
94 var total = 1;
95 var btnArray = ['确定', '取消'];
96 ('请输入打印数量...', total, '', btnArray, function(e) {
97 if( == 0) {
98 total = parseFloat();
99 if(total == 0) {
100 alert("请输入大于0的数量!");
101 return false;
102 } else {
103 print(molList, total);
104 }
105 } else {
106 ("已取消");
107 }
108 });
109 }
110
111 function print(molList, total) {
112 iting("标签打印中,请稍等...");
113 var mac_address = m("macaddress");
114 if(!mac_address) {
115 ('请选择蓝牙打印机');
116 return;
117 }
118 m("macaddress", mac_address);
119 //alert(m("macaddress"));
120 main = eMainActivity();
121 BluetoothAdapter = Class("othAdapter");
122 UUID = Class("");
123 uuid = ring("00001101-0000-1000-8000-00805F9B34FB");
124 BAdapter = aultAdapter();
125 device = oteDevice(mac_address);
126 Class(device);
127 bluetoothSocket = InsecureRfcommSocketToServiceRecord(uuid);
128 Class(bluetoothSocket);
129
130 if(!ected()) {
131 ('检测到设备未连接,尝试连接....');
132 t();
133 }
134
135 ('设备已连接');
136
137 if(ected()) {
138 // alert(ected());
139 var outputStream = putStream();
140 Class(outputStream);
141 var wlbm = AL_(-10);
142 var wlms = AL_DESCRIPTION;
143 var ddh = _NUMBER;
144 var sl = _SUM;
145 var dw = _UNIT;
146
147 var string = "^XA^MNW^JUS^CI28" +
148 "^FO15,25" +
149 "^MD" +
150 "^A@N,20,20,E:" +
151 //"^A@N,20,20,E:" +
152 "^FD" + wlbm + "^FS" +
153 "^FO15,55" +
154 "^A@N,20,20,E:" +
155 "^FD名称:" + wlms + "^FS" +
156 "^FO15,85" +
157 "^A@N,20,20,E:" +
158 "^FD订单号:" + ddh + "^FS" +
159 "^FO15,115" +
160 "^A@N,20,20,E:" +
161 "^FD到货日期:" + _DATE + " 数量:" + sl + "^FS" +
162 "^FO15,145" +
163 "^A@N,20,20,E:" +
164 "^FD工厂:5020 单位:" + dw + "^FS" +
165 "^FO55,175^BY1.8" +
166 "^BCN,50,Y,N,N" +
167 "^FD" + wlbm + "^FS" +
168 "^PQ" + total + ",2,1,Y" +
169 "^XZ";
170
171 var bytes = (string, 'getBytes', 'UTF-8');
172 (bytes);
173 ();
174 aiting();
175
176 device = null //这里关键
177 (); //必须关闭蓝牙连接否则意外断开的话打印错误
178
179 }
180 }
181
182
补充下:本人已经离项目 没拿代码, 官网案例 请参考
2023年12月11日发(作者:少梦影)
H5开发连接蓝牙打印机打印标签(斑马ZR628)
1.连接蓝牙打印机(先用手机自带蓝牙进行配对),然后绑定出已配对的蓝牙设备(用来选择/切换打印机之用),代码如下
已配对蓝牙设备,中显示的就是已连接的,点击一下即可
代码:
1
2
3
4
5
6
7
8
9
10
11
12
13 .title {
14 margin: 20px 15px 7px;
15 color: #6d6d72;
16 font-size: 15px;
17 }
18
19 .mui-bar-nav {
20 background: #004A86;
21 }
22
23 .mui-icon-back:before,
24 .mui-icon-left-nav:before,
25 .mui-bar .mui-title {
26 color: #ffffff;
27 }
28
29 .mui-content>.mui-table-view:first-child {
30 margin-top: 0px;
31 }
32
33 .mui-btn {
34 padding: 1px 1px;
35 }
36 /*.mui-btn-primary{ border: 1px solid #004A86; background: #004A86;}*/
37
38 .mui-icon-search:before {
39 font-size: 16px;
40 }
41
42
43
44
45
101
102
103
104
105
106
107
108
109
110 //address=""搜索蓝牙//address=设备mac地址,自动配对给出mac地址的设备
111 function searchDevices(address) {
112 //注册类
113 var main = eMainActivity();
114 var IntentFilter = Class('Filter');
115 var BluetoothAdapter = Class("othAdapter");
116 var BluetoothDevice = Class("othDevice");
117 var BAdapter = aultAdapter();
118 ("开始搜索设备");
119 var filter = new IntentFilter();
120 var bdevice = new BluetoothDevice();
121 var on = null;
122 var un = null;
123 var vlist1 = mentById('list1'); //注册容器用来显示未配对设备
124 TML = ''; //清空容器
125 var vlist2 = mentById('list2'); //注册容器用来显示未配对设备
126 TML = ''; //清空容器
127 var button1 = mentById('bt1');
128 ed = true;
129 = '正在搜索请稍候...';
130 iscovery(); //开启搜索
131 var receiver;
132 receiver = ents('astReceiver', {
133 onReceive: function(context, intent) { //实现onReceiver回调函数
134 Class(intent); //通过intent实例引入intent类,方便以后的‘.’操作
135 (ion()); //获取action
136 if(ion() == "ERY_FINISHED") {
137 sterReceiver(receiver); //取消监听
138 ed = false;
139 = '搜索设备';
140 ("搜索结束")
141 } else {
142
143 BleDevice = celableExtra(_DEVICE);
144 //判断是否配对
145 if(dState() == _NONE) {
146 ("未配对蓝牙设备:" + e() + ' ' + ress());
147 //参数如果跟取得的mac地址一样就配对
148 if(address == ress()) {
149 if(Bond()) { //配对命令.createBond()
150 ("配对成功");
151 var li2 = Element('li'); //注册
152 ribute('id', ress()); //打印机mac地址
153 ribute('onclick', 'print(id)'); //注册click点击列表进行打印
154 ribute('class', 'mui-table-view-cell');
155 ext = e();
156 Child(li2);
157 }
158
159 } else {
160 if(e() != on) { //判断防止重复添加
161 var li1 = Element('li'); //注册
162 ribute('id', ress()); //打印机mac地址
163 ribute('onclick', 'searchDevices(id)'); //注册click点击列表进行配对
164 ribute('class', 'mui-table-view-cell');
165 on = e();
166 ext = on;
167 Child(li1);
168
169 }
170
171 }
172 } else {
173 if(e() != un) { //判断防止重复添加
174 ("已配对蓝牙设备:" + e() + ' ' + ress());
175 var li2 = Element('li'); //注册
176 ribute('id', ress()); //打印机mac地址
177 ribute('onclick', 'print(id)'); //注册click点击列表进行打印
178 ribute('class', 'mui-table-view-cell'); //注册click点击列表进行打印
179 un = e();
180 ext = un;
181 Child(li2);
182 }
183 }
184 }
185
186 }
187 });
188
189 ion(_FOUND);
190 ion(_DISCOVERY_STARTED);
191 ion(_DISCOVERY_FINISHED);
192 ion(_STATE_CHANGED);
193
194 erReceiver(receiver, filter); //注册监听
195 }
196
197 var device = null,
198 BAdapter = null,
199 BluetoothAdapter = null,
200 uuid = null,
201 main = null,
202 bluetoothSocket = null;
203
204 function print(mac_address) {
205 if(!mac_address) {
206 ('请选择蓝牙打印机');
207 return;
208 }
209 //alert(mac_address);
210 m("macaddress", mac_address);
211 alert('打印机已配对成功');
212 return;
213 }
214
215
216
2.打印页面,只有一个按钮(生成条码),见下图,代码如下
注:打印是用的斑马ZPL指令(如需学习 百度很多~)
代码如下:方法:printIndex(mol)
1
2 //address=""搜索蓝牙//address=设备mac地址,自动配对给出mac地址的设备
3 function searchDevices(address) {
4 //注册类
5 var main = eMainActivity();
6 var IntentFilter = Class('Filter');
7 var BluetoothAdapter = Class("othAdapter");
8 var BluetoothDevice = Class("othDevice");
9 var BAdapter = aultAdapter();
10 ("开始搜索设备");
11 var filter = new IntentFilter();
12 var bdevice = new BluetoothDevice();
13 var on = null;
14 var un = null;
15 var vlist1 = mentById('list1'); //注册容器用来显示未配对设备
16 TML = ''; //清空容器
17 var vlist2 = mentById('list2'); //注册容器用来显示未配对设备
18 TML = ''; //清空容器
19 var button1 = mentById('bt1');
20 ed = true;
21 = '正在搜索请稍候';
22 iscovery(); //开启搜索
23 var receiver;
24 receiver = ents('astReceiver', {
25 onReceive: function(context, intent) { //实现onReceiver回调函数
26 Class(intent); //通过intent实例引入intent类,方便以后的‘.’操作
27 (ion()); //获取action
28 if(ion() == "ERY_FINISHED") {
29 sterReceiver(receiver); //取消监听
30 ed = false;
31 = '搜索设备';
32 ("搜索结束")
33 } else {
34 BleDevice = celableExtra(_DEVICE);
35 //判断是否配对
36 if(dState() == _NONE) {
37 ("未配对蓝牙设备:" + e() + ' ' + ress());
38 //参数如果跟取得的mac地址一样就配对
39 if(address == ress()) {
40 if(Bond()) { //配对命令.createBond()
41 ("配对成功");
42 var li2 = Element('li'); //注册
43 ribute('id', ress()); //打印机mac地址
44 ribute('onclick', 'printIndex(id)'); //注册click点击列表进行打印
45 ext = e();
46 Child(li2);
47 }
48
49 } else {
50 if(e() != on) { //判断防止重复添加
51 var li1 = Element('li'); //注册
52 ribute('id', ress()); //打印机mac地址
53 ribute('onclick', 'searchDevices(id)'); //注册click点击列表进行配对
54 on = e();
55 ext = on;
56 Child(li1);
57
58 }
59
60 }
61 } else {
62 if(e() != un) { //判断防止重复添加
63 ("已配对蓝牙设备:" + e() + ' ' + ress());
64 var li2 = Element('li'); //注册
65 ribute('id', ress()); //打印机mac地址
66
67 ribute('onclick', 'printIndex(id)'); //注册click点击列表进行打印
68 un = e();
69 ext = un;
70 Child(li2);
71 }
72 }
73 }
74
75 }
76 });
77
78 ion(_FOUND);
79 ion(_DISCOVERY_STARTED);
80 ion(_DISCOVERY_FINISHED);
81 ion(_STATE_CHANGED);
82
83 erReceiver(receiver, filter); //注册监听
84 }
85
86 var device = null,
87 BAdapter = null,
88 BluetoothAdapter = null,
89 uuid = null,
90 main = null,
91 bluetoothSocket = null;
92
93 function printIndex(molList) {
94 var total = 1;
95 var btnArray = ['确定', '取消'];
96 ('请输入打印数量...', total, '', btnArray, function(e) {
97 if( == 0) {
98 total = parseFloat();
99 if(total == 0) {
100 alert("请输入大于0的数量!");
101 return false;
102 } else {
103 print(molList, total);
104 }
105 } else {
106 ("已取消");
107 }
108 });
109 }
110
111 function print(molList, total) {
112 iting("标签打印中,请稍等...");
113 var mac_address = m("macaddress");
114 if(!mac_address) {
115 ('请选择蓝牙打印机');
116 return;
117 }
118 m("macaddress", mac_address);
119 //alert(m("macaddress"));
120 main = eMainActivity();
121 BluetoothAdapter = Class("othAdapter");
122 UUID = Class("");
123 uuid = ring("00001101-0000-1000-8000-00805F9B34FB");
124 BAdapter = aultAdapter();
125 device = oteDevice(mac_address);
126 Class(device);
127 bluetoothSocket = InsecureRfcommSocketToServiceRecord(uuid);
128 Class(bluetoothSocket);
129
130 if(!ected()) {
131 ('检测到设备未连接,尝试连接....');
132 t();
133 }
134
135 ('设备已连接');
136
137 if(ected()) {
138 // alert(ected());
139 var outputStream = putStream();
140 Class(outputStream);
141 var wlbm = AL_(-10);
142 var wlms = AL_DESCRIPTION;
143 var ddh = _NUMBER;
144 var sl = _SUM;
145 var dw = _UNIT;
146
147 var string = "^XA^MNW^JUS^CI28" +
148 "^FO15,25" +
149 "^MD" +
150 "^A@N,20,20,E:" +
151 //"^A@N,20,20,E:" +
152 "^FD" + wlbm + "^FS" +
153 "^FO15,55" +
154 "^A@N,20,20,E:" +
155 "^FD名称:" + wlms + "^FS" +
156 "^FO15,85" +
157 "^A@N,20,20,E:" +
158 "^FD订单号:" + ddh + "^FS" +
159 "^FO15,115" +
160 "^A@N,20,20,E:" +
161 "^FD到货日期:" + _DATE + " 数量:" + sl + "^FS" +
162 "^FO15,145" +
163 "^A@N,20,20,E:" +
164 "^FD工厂:5020 单位:" + dw + "^FS" +
165 "^FO55,175^BY1.8" +
166 "^BCN,50,Y,N,N" +
167 "^FD" + wlbm + "^FS" +
168 "^PQ" + total + ",2,1,Y" +
169 "^XZ";
170
171 var bytes = (string, 'getBytes', 'UTF-8');
172 (bytes);
173 ();
174 aiting();
175
176 device = null //这里关键
177 (); //必须关闭蓝牙连接否则意外断开的话打印错误
178
179 }
180 }
181
182
补充下:本人已经离项目 没拿代码, 官网案例 请参考