schangxiang@126.com
2025-07-23 ba449717184ae09590aaead8a7240103b26cec5e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import printerUtil from '@/components/print/printerutil.js'
const PrinterJobs = require('@/components/print/printerjobs.js')
import {
    getDate
} from '@/utils/dateTime.js'
export default {
    data() {
        return {
            list: [],
            deviceId: '',
            serviceId: '',
            characteristics: [],
            characteristicId: '',
            deviceslist: []
        }
    },
    onShow() {
        this.list = []
        this.deviceslist = []
 
    },
    methods: {
        //初始化蓝牙设备
        openBluetoothAdapter() {
            let _this = this
            uni.openBluetoothAdapter({
                success: (res) => { //已打开
                    uni.getBluetoothAdapterState({ //蓝牙的匹配状态
                        success: (res1) => {
                            console.log(res1, '本机设备的蓝牙已打开')
                            // 开始搜索蓝牙设备
 
                            _this.startBluetoothDeviceDiscovery()
                            uni.setStorageSync('blueShow', true)
                        },
                        fail(error) {
                            uni.showToast({
                                icon: 'none',
                                title: '查看手机蓝牙是否打开err'
                            });
                        }
                    });
 
                },
                fail: err => { //未打开 
                    uni.setStorageSync('blueShow', false)
                    uni.showToast({
                        icon: 'none',
                        title: '查看手机蓝牙是否打开'
                    });
                }
            })
        },
        // 开始搜索蓝牙设备
        startBluetoothDeviceDiscovery() {
            uni.startBluetoothDevicesDiscovery({
                success: (res) => {
                    // 发现外围设备
                    uni.showLoading({
                        title: '加载中',
                        mask: true
                    });
                    this.onBluetoothDeviceFound()
                },
                fail: err => {
                    console.log(err, '错误信息')
                }
            })
        },
        // 发现外围设备
        onBluetoothDeviceFound() {
            uni.onBluetoothDeviceFound((res) => {
                this.deviceslist.length > 0 ? uni.hideLoading() : ''
                if (this.deviceslist.indexOf(res.devices[0].deviceId) == -1) {
                    this.deviceslist.push(res.devices[0].deviceId)
                    if (res.devices[0].name != "") {
                        uni.hideLoading();
                        this.list.push({
                            name: res.devices[0].name,
                            deviceId: res.devices[0].deviceId
                        })
                    }
                }
            })
        },
        //获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。
        getBluetoothDevices() {
            console.log("获取蓝牙设备");
            uni.getBluetoothDevices({
                success: res => {
                    console.log('获取蓝牙设备成功:');
                    console.log(res.devices);
                }
            });
        },
        //选择设备连接吧deviceId传进来
        createBLEConnection(deviceId) {
            //data里面建立一个deviceId,存储起来
            let _this = this
            uni.showLoading({
                title: '连接蓝牙中...',
                mask: true
            });
            this.stopBluetoothDevicesDiscovery()
 
            this.deviceId = deviceId,
                //连接蓝牙
                uni.createBLEConnection({
                    // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
                    deviceId: deviceId,
                    success(res) {
                        uni.hideLoading();
                        uni.showToast({
                            icon: 'none',
                            title: '蓝牙连接成功'
                        });
                        uni.setStorageSync("deviceId", deviceId) //把已经连接的蓝牙设备信息放入缓存
 
                        setTimeout(() => {
                            _this.getBLEDeviceServices(deviceId)
                        }, 1000)
 
                    },
                    fail(res) {
                        uni.showToast({
                            icon: 'none',
                            title: '蓝牙连接失败'
                        });
                    }
                })
        },
        // 停止搜寻蓝牙设备
        stopBluetoothDevicesDiscovery() {
            uni.stopBluetoothDevicesDiscovery({
                success: e => {
                    this.loading = false
                    console.log('停止搜索蓝牙设备:' + e.errMsg);
                },
                fail: e => {
                    console.log('停止搜索蓝牙设备失败,错误码:' + e.errCode);
                }
            });
        },
        //获取蓝牙特征
        getBLEDeviceCharacteristics(deviceId, serviceId) {
            console.log("进入特征");
            setTimeout(() => {
                uni.getBLEDeviceCharacteristics({
                    // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
                    deviceId: deviceId,
                    // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
                    serviceId: serviceId,
                    success: (res) => {
                        console.log(res, '特征getBLEDeviceCharacteristics')
                        this.characteristics = res.characteristics
 
                        res.characteristics.forEach((item) => {
                            if (item.uuid.indexOf("FF02") != -1) {
                                this.characteristicId = item.uuid
                                uni.setStorageSync("characteristicId", item
                                    .uuid) //把已经连接的蓝牙设备标识放入缓存
                                uni.setStorageSync("serviceId",
                                    serviceId) //把已经连接的蓝牙设备ID放入缓存
                                //console.log('characteristicId:', item.uuid)
                                //利用传参的形势传给下面的notify,这里的uuid如果都需要用到,就不用做判断了,建议使用setTimeout进行间隔性的调用此方法
                                // this.notifyBLECharacteristicValueChange(item.uuid)
 
                                setTimeout(() => {
                                    uni.hideLoading();
                                    this.BottomShow = false
                                    this.DrawerModalL = false
                                }, 1000)
                            }
                        })
                    },
                    fail: (res) => {
                        console.log(res)
                    }
                })
            }, 1000)
        },
        // 启用 notify 功能
        notifyBLECharacteristicValueChange(characteristicId) {
            let _this = this
            console.log('deviceId' + _this.deviceId)
            console.log('serviceId' + _this.serviceId)
            console.log('characteristicId' + characteristicId)
            // _this.fanhui()
            uni.notifyBLECharacteristicValueChange({
                state: true, // 启用 notify 功能
                // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
                deviceId: this.deviceId,
                // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
                serviceId: this.serviceId,
                // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
                characteristicId: characteristicId,
                success: (res) => {
                    if (this.blueFlag == true) {
                        this.writeBLECharacteristicValue()
                    } else {
                        uni.showLoading({
                            title: '蓝牙服务获取成功,请打印',
                            mask: true,
                        });
                    }
 
                    setTimeout(() => {
                        uni.hideLoading();
                    }, 1000)
                    // _this.fanhui()
                    this.print = "打印"
                    this.BottomShow = false
 
                    console.log('notifyBLECharacteristicValueChange success', res.errMsg)
                },
                fail: (res) => {
                    uni.showLoading({
                        title: '蓝牙服务获取成功,请打印',
                        mask: true,
                    });
                    setTimeout(() => {
                        uni.hideLoading();
                    }, 1000)
                    _this.print = "打印"
                    _this.BottomShow = false
 
                    // _this.fanhui()
                    console.log('notifyBLECharacteristicValueChange fail', res.errMsg)
                }
            })
        },
        //获取蓝牙的所有服务
        getBLEDeviceServices(deviceId) {
            uni.showLoading({
                title: '正在获取蓝牙服务...',
                mask: true,
            });
            setTimeout(() => {
                uni.getBLEDeviceServices({
                    // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
                    deviceId: deviceId,
                    success: (res) => {
                        uni.showLoading({
                            title: '获取中,请稍后...',
                            mask: true,
                        });
                        res.services.forEach((item) => {
                            if (item.uuid.indexOf("FF00") != -1) {
                                this.serviceId = item.uuid;
                                //获取特征
                                this.getBLEDeviceCharacteristics(deviceId, item.uuid)
                            }
                        })
                    }
                })
            }, 1000)
        },
        //写入蓝牙
        writeBLECharacteristicValue() {
            this.printflag = false
            let deviceId = uni.getStorageSync('deviceId')
            let serviceId = uni.getStorageSync('serviceId')
            let characteristicId = uni.getStorageSync('characteristicId')
            const printerJobs = new PrinterJobs();
            console.log(this.arr)
            this.arr.forEach(item => {
                if(item.business_order_type==null){
                    printerJobs
                        .label_set_page(576, 400)
                        //边框
                        .drawLine(1, 0, 0, 570, 0)
                        .drawLine(1, 0, 0, 0, 360)
                        .drawLine(1, 570, 0, 570, 360)
                        .drawLine(1, 0, 360, 570, 360)
                    
                        //内部横线
                        .drawLine(1, 0, 94, 570, 94) //第一行
                        .drawLine(1, 0, 160, 570, 160) //第二行
                        .drawLine(1, 0, 227, 570, 227) //第三行
                        .drawLine(1, 0, 293, 400, 293) //第四行
                    
                        .drawLine(1, 120, 94, 120, 360) // 第一根竖线
                    
                        .drawLine(1, 400, 227, 400, 360) // 第二根竖线
                        // 1 原始 是否粗体\字体大小\是否旋转角度\起始横坐标\起始纵坐标\字符串
                    
                        .drawText(170, 20, "重庆青山工业有限责任公司", 26, 0, 1, false, false)
                        .drawText(240, 62, '物料标识签', 26, 0, 1, false, false)
                    
                        // 3
                        .drawText(10, 120, "物料编号", 24, 0, 0, false, false)
                        .drawText(130, 120, item.material_code, 24, 0, 0, false, false)
                    
                        // 4
                        .drawText(10, 185, "物料名称", 24, 0, 0, false, false)
                        .drawText(130, 185, item.material_name, 24, 0, 0, false, false)
                    
                        // 5
                        .drawText(10, 248, "物料批次", 24, 0, 0, false, false)
                        .drawText(130, 248, item.lots_sn, 24, 0, 0, false, false)
                    
                        // 7
                        .drawText(10, 312, "打印时间", 24, 0, 0, false, false)
                        .drawText(130, 312, item.material_actual_num, 24, 0, 0, false, false)
                        // 二维码
                        .drawQrCode(438, 232, item.material_code, 0, 4, 1)
                        .drawText(423, 340, item.material_name, 1, 0, 0, false, false)
                        .label_print(0, 1);
                }else{
                    printerJobs
                        .label_set_page(576, 400)
                        //边框
                        .drawLine(1, 0, 0, 570, 0)
                        .drawLine(1, 0, 0, 0, 360)
                        .drawLine(1, 570, 0, 570, 360)
                        .drawLine(1, 0, 360, 570, 360)
                    
                        //内部横线
                        .drawLine(1, 0, 52, 570, 52) //第一行
                        .drawLine(1, 0, 104, 570, 104) //第二行
                        .drawLine(1, 0, 156, 570, 156) //第三行
                        .drawLine(1, 0, 208, 400, 208) //第四行
                        .drawLine(1, 0, 260, 400, 260) //第五行
                        .drawLine(1, 0, 312, 400, 312) //第六行
                    
                        .drawLine(1, 120, 0, 120, 360) // 第一根竖线
                    
                        .drawLine(1, 220, 260, 220, 312) // 第二根竖线
                        .drawLine(1, 300, 260, 300, 312) // 第三根竖线
                    
                        .drawLine(1, 400, 156, 400, 312,) // 第四根竖线
                        // 1 原始 是否粗体\字体大小\是否旋转角度\起始横坐标\起始纵坐标\字符串
                    
                        .drawText(10, 20, "物料编号", 24, 0, 0, false, false)
                        .drawText(130, 20, item.material_code, 24, 0, 0, false, false)
                    
                        // 2
                        .drawText(10, 72, "物料名称", 24, 0, 0, false, false)
                        .drawText(130, 72, item.material_name, 24, 0, 0, false, false)
                    
                        // 3
                        .drawText(10, 124, "物料批次", 24, 0, 0, false, false)
                        .drawText(130, 124, item.lots_sn, 24, 0, 0, false, false)
                    
                        // 4
                        .drawText(10, 176, "单据号", 24, 0, 0, false, false)
                        .drawText(130, 176, item.business_order_no, 24, 0, 0, false, false)
                    
                        // 5
                        .drawText(10, 225, "单据类型", 24, 0, 0, false, false)
                        .drawText(130, 225, item.business_order_type, 24, 0, 0, false, false)
                    
                        // // 5-1
                        // .drawText(230, 225, "工序", 24, 0, 0, false, false)
                        // .drawText(310, 225, item.cellNo, 24, 0, 0, false, false)
                    
                        // 6
                        .drawText(10, 280, "分拣前", 24, 0, 0, false, false)
                        .drawText(130, 280, item.material_original_num, 24, 0, 0, false, false)
                    
                        // 6-1
                        .drawText(230, 280, "分拣后", 24, 0, 0, false, false)
                        .drawText(340, 280, item.material_actual_num, 24, 0, 0, false, false)
                    
                        // 7
                        .drawText(10, 332, "分拣数量", 24, 0, 0, false, false)
                        .drawText(130, 332, item.cTime, 24, 0, 0, false, false)
                        .drawText(440, 332, item.material_require_num, 24, 0, 0, false, false)
                    
                        // 二维码
                        .drawQrCode(426, 170, item.material_code, 0, 6, 1)
                        .drawText(434, 310, item.material_name, 1, 0, 0, false, false)
                        .label_print(0, 1);
                }
            
            })
            let buffer = printerJobs.buffer();
            // console.log('ArrayBuffer', 'length: ' + buffer.byteLength, ' hex: ' + ab2hex(buffer));
            // 1.并行调用多次会存在写失败的可能性
            // 2.建议每次写入不超过20字节
            // 分包处理,延时调用
            const maxChunk = 18;
            const delay =20;
            for (let i = 0, j = 0, length = buffer.byteLength; i < length; i += maxChunk, j++) {
                let subPackage = buffer.slice(i, i + maxChunk <= length ? (i + maxChunk) : length);
                // setTimeout(this._writeBLECharacteristicValue, j * delay, subPackage);
                setTimeout(() => {
                        this._writeBLECharacteristicValue(deviceId, serviceId, characteristicId, subPackage)
                    }, j *delay);
            }
 
        },
        _writeBLECharacteristicValue(deviceId, serviceId, characteristicId, buffer) {
            uni.writeBLECharacteristicValue({
                deviceId: deviceId,
                serviceId: serviceId,
                characteristicId: characteristicId,
                value: buffer,
                success(res) {
                    console.log('writeBLECharacteristicValue success', res.errMsg)
                },
                fail(res) {
                    console.log(JSON.stringify(res))
                    console.log(JSON.stringify(buffer))
                    uni.getStorageSync('failerr','try')
                }
            })
        },
        //长按事件
        longpress(item, index) {
            let _this = this
            if (uni.getStorageSync('deviceId')) {
                uni.showToast({
                    title: '蓝牙已连接!',
                    icon: 'none',
                    duration: 2000
                })
                return
            }
            uni.showModal({
                title: '提示',
                content: '是否连接蓝牙:' + item.name,
                showCancel: true,
                cancelColor: '#333333',
                success: (res => {
                    if (res.confirm) {
                        // _this.stopBluetoothDevicesDiscovery()
                        uni.setStorageSync('itemblue', item.name)
                        this.itemblue = item.name
                        _this.createBLEConnection(item.deviceId)
 
                    } else if (res.cancel) {
                        uni.showToast({
                            title: '已取消连接',
                            duration: 2000,
                            icon: 'none'
                        })
                    }
                })
            });
        },
        // 点击断开蓝牙连接
        tomy() {
            var _this = this
 
            uni.closeBluetoothAdapter({
                success(res) {
                    uni.removeStorageSync('deviceId')
                    uni.removeStorageSync("serviceId");
                    uni.removeStorageSync("characteristicId");
                    // _this.openBluetoothAdapter()
                }
            })
 
        },
        //返回
        fanhui() {
            uni.navigateTo({
                url: './printing'
            })
        },
    }
}