快乐的昕的电脑
2025-11-24 9ee31239066551faf93a6bacfa142a72a4672bc8
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
<template>
    <view class="mui-content">    
        <view class="mui-table-view ulcss">
            <view class="mui-table-view-cell acss" @tap="getPrintInfo" style="
    position: absolute;
    top: -11px;
    right: 21px;
    /* padding: 10px 20px; */
    cursor: pointer;
    float: right;
    background-color: #00A2E9;
    color: white;
    border: none;
    font-size: 29px;
    border-radius: 15px;
    margin-top: -23px;
    padding-left: 25px;
    padding-right: 25px;
    margin-top: 9px;
    margin-bottom: 20px;">
                <text>刷新</text>
            </view>
        </view>
            <view class="mui-input-row">
                <label>PDA地址:</label>
                <input v-model="pdaMac" type="text" class="mui-input-clear" placeholder="请输入PDA的MAC地址" />
            </view>
            <view class="mui-input-row">
                <label>打印地址:</label>
                <input v-model="printMac" type="text" class="mui-input-clear" placeholder="请输入打印机MAC地址" />
            </view>
        </form>
        <view class="mui-table-view ulcss">
            <view class="mui-table-view-cell acss" @tap="saveSettings" style="background-color: rgb(93, 204, 201);">
                <text>保存</text>
            </view>
        </view>
        <view class="mui-table-view ulcss">
            <view class="mui-table-view-cell acss" @tap="doPrint2" style="background-color: rgb(153, 204, 153);">
                <text>打印测试</text>
            </view>
        </view>
 
    </view>
</template>
 
<script>
    export default {
        props: {
            orderNo: String,
            orderId: Number,
            machineNo: String,
        },
        data() {
            return {
                pdaMac: '',
                printMac: 'DC:1D:30:91:06:52',
                bluetoothSocket: {}, // 蓝牙连接
                device: '',
                uuid: ''
            };
        },
        created() {
            this.getPrintInfo();
            this.getMac();
            this.init();
        },
        methods: {
            getPrintInfo() {
                var mac = "";
                if (plus.os.name == "Android") {
                    //获取手机MAC地址
                    var Context = plus.android.importClass("android.content.Context");
                    var WifiManager = plus.android.importClass("android.net.wifi.WifiManager");
                    var wifiManager = plus.android.runtimeMainActivity().getSystemService(Context.WIFI_SERVICE);
                    var WifiInfo = plus.android.importClass("android.net.wifi.WifiInfo");
                    var wifiInfo = wifiManager.getConnectionInfo();
                    mac = wifiInfo.getMacAddress();
 
                    //如果mac为“02:00:00:00:00:00”,则可能是安卓6.0以上版本,则使用另一种方法获取mac地址
                    if (mac == "02:00:00:00:00:00") {
                        mac = this.getMacNew();
                    }
                    //如果mac使用新方法依然是“02:00:00:00:00:00”则不进行保存
                    if (mac == "02:00:00:00:00:00") {
                        return;
                    }
                    uni.setStorageSync('pdaMac', mac);
                    console.log(mac);
 
                    this.$post({
                        url: "/DevMachine/GetDevMachineByPdaMac",
                        data: {
                            pdaMac: mac,
                        }
                    }).then(res => {
                        let devMachine = res.data.tbBillList;
                        this.machineNo = devMachine.machineNo;
                        // this.machineName = "当前机台:" + this.machineNo + "号注塑机";
 
                        uni.setStorageSync('machineNo', this.machineNo);
                        uni.setStorageSync('printMac', devMachine.printMac);
 
                        this.getWomdaaIsShow();
 
                    });
 
                }
            },
            getMac() {
                let pdaMac = uni.getStorageSync('pdaMac');
                let printMac = uni.getStorageSync('printMac');
                this.pdaMac = pdaMac;
                this.printMac = printMac;
                return pdaMac;
            },
            getMacNew() {
                var str = "";
                try {
                    if (plus.os.name == "Android") {
                        var NetworkInterface = plus.android.importClass("java.net.NetworkInterface");
                        var networkInterface = NetworkInterface.getByName("wlan0");
                        var bytes = networkInterface.getHardwareAddress();
 
                        //将byte[] 转换成 String
                        for (var i = 0; i < bytes.length; i++) {
                            var tmp = "";
                            var num = bytes[i];
                            if (num < 0) {
                                tmp = (255 + num + 1).toString(16);
                            } else {
                                tmp = num.toString(16);
                            }
                            if (tmp.length == 1) {
                                tmp = "0" + tmp;
                            }
                            str += (i == 0) ? (tmp) : (":" + tmp);
                        }
                    }
                } catch (err) {
                    str = "02:00:00:00:00:00";
                }
                return str;
            },
            saveSettings() {
                if (!this.pdaMac) {
                    uni.showToast({
                        title: 'PDA地址不能为空',
                        icon: 'none',
                    });
                    return;
                }
                if (!this.printMac) {
                    uni.showToast({
                        title: '打印机地址不能为空',
                        icon: 'none',
                    });
                    return;
                }
                uni.setStorageSync('pdaMac', this.pdaMac);
                uni.setStorageSync('printMac', this.printMac);
 
                uni.showToast({
                    title: '保存成功',
                    icon: 'success',
                    duration: 3000,
                });
 
                this.$post({
                    url: "/DevMachine/UpdateDevMachine",
                    data: {
                        machineNo: this.machineNo,
                        pdaMac: this.pdaMac,
                        printMac: this.printMac,
                    }
                }).then(res => {
 
                });
 
            },
 
 
            doPrint2() {
                var mac_address = this.printMac;
                console.log('打印开始')
                var device = null,
                    BAdapter = null,
                    BluetoothAdapter = null,
                    uuid = null,
                    main = null,
                    bluetoothSocket = null;
 
                if (!mac_address) {
                    this.$showMessage('请选择蓝牙打印机');
                    return false;
                }
                main = plus.android.runtimeMainActivity();
                BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
                let 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);
                    let printStr =
                        "! 0 200 200 230 1\n" +
                        "PW 848\n" +
                        "TONE 0\n" +
                        "SPEED 0\n" +
                        "GAP-SENSE\n" +
                        "NO-PACE\n" +
                        "POSTFEED 0\n" +
                        "LEFT\n" +
                        "T 65 1 20 2 物料编码:\n" +
                        "SETBOLD 2\n" +
                        "T 65 1 140 1 {0}\n" +
                        "SETBOLD 0\n" +
                        "T 65 1 300 1 {11}\n" +
                        "T 65 1 20 25 物料规格:{1}\n" +
                        "T 65 1 20 50 {2}\n" +
                        "T 65 0 20 75 供应商:\n" +
                        "T 65 0 20 100 有效期:\n" +
                        "T 65 0 20 125 检验员:\n" +
                        "T 65 0 20 150 校验日期:\n" +
                        "T 55 0 120 80 {3}\n" +
                        "T 55 0 120 105 {4}\n" +
                        "T 55 0 120 130 {6}\n" +
                        "T 55 0 140 155 {5}\n" +
                        "SETBOLD 2\n" +
                        "SETBOLD 0\n" +
                        "SETMAG 0 0\n" +
                        "T 65 1 440 75 数量:\n" +
                        "SETBOLD 2\n" +
                        "T 65 1 500 75 {9}\n" +
                        "SETBOLD 0\n" +
                        "B QR 600 2 M 2 U 4\n" +
                        "MA,{10}\n" +
                        "ENDQR\n" +
                        "B 128 1 0 50 320 100 {10}\n" +
                        "T 65 1 385 155 {10}\n" +
                        "FORM\n" +
                        "PRINT\n";
                    var bytes = plus.android.invoke(printStr, 'getBytes', 'gbk');
                    outputStream.write(bytes);
                    outputStream.flush();
                    device = null //这里关键
                    bluetoothSocket.close(); //必须关闭蓝牙连接否则意外断开的话打印错误
                    //this.$showMessage('OK');
                    console.log("打印机连接状态:111");
                    return true;
                } else {
                    //this.$showMessage('NG');
                    console.log("打印机连接状态:222");
                    return false;
                }
            },
 
            printTest() {
                console.log(this.pdaMac);
                console.log('打印地址' + this.printMac);
                // this.doPrint2(this.printMac);
                // return;
 
                try {
 
                    console.log(1);
 
                    console.log(this.bluetoothSocket);
 
                    if (!this.bluetoothSocket.isConnected()) {
                        // plus.ui.toast("重新连接");
                        this.bluetoothSocket.connect();
                    }
 
                    var outputStream = this.bluetoothSocket.getOutputStream();
                    plus.android.importClass(outputStream);
 
                    //var barcode = "102000460*N017051601*14*170519*002";
                    var pdaMacStr = "PDA地址:     " + this.pdaMac;
                    var printMacStr = "打印机地址:  " + this.printMac;
                    var titleName = "测试页";
 
                    var printCmd = "! 0 203 203 490 1\r\n" +
                        "POSTFEED 19.5\n\r" + //打印之后走纸距离指令
                        "PAGE - WIDTH 640\r\n" +
                        "BOX 10 200 610 350 2\r\n" +
                        "LINE 10 270 610 270 1\r\n" +
                        "LINE 160 200 160 350 1\r\n" + //竖线
                        "TEXT 24 0 150 50 " + titleName + "\r\n" +
                        "TEXT 24 0 25 210 " + pdaMacStr + "\r\n" +
                        "TEXT 24 0 25 310 " + printMacStr + "\r\n" +
 
                        "B QR 350 50 M 2 U 5\r\n" + //二维码
                        "MA,打印成功~ \r\n" +
                        "ENDQR\r\n" +
 
                        "FORM\r\n" +
                        "PRINT\r\n";
 
                    var arrayBuffer = plus.android.invoke(printCmd, 'getBytes', 'gbk'); //stringToByte(printCmd);
                    outputStream.write(arrayBuffer);
                    outputStream.flush();
                } catch (err) {
                    console.log(err);
                    uni.showToast({
                        title: err,
                        icon: 'success',
                    });
                }
            },
            init() {
                try {
                    var main = plus.android.runtimeMainActivity();
                    var BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
                    var UUID = plus.android.importClass("java.util.UUID");
 
                    this.uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
 
                    console.log(JSON.stringify(this.uuid, null, 2));
 
                    var BAdapter = BluetoothAdapter.getDefaultAdapter();
                    BAdapter.cancelDiscovery(); //停止扫描
 
                    // var addres_mac = plus.storage.getItem("DC:1D:30:3F:D2:50");//传入打印机mac地址
                    var address_mac = uni.getStorageSync('printMac') ? uni.getStorageSync('printMac') :
                        "DC:0D:30:91:06:52";
                    this.device = BAdapter.getRemoteDevice(address_mac);
 
                    plus.android.requestPermissions(['android.permission.BLUETOOTH_SCAN',
                        'android.permission.BLUETOOTH_CONNECT'
                    ]);
 
                    console.log(231);
 
                    plus.android.importClass(this.device);
 
                    this.bluetoothSocket = this.device.createInsecureRfcommSocketToServiceRecord(this.uuid);
 
                    console.log(231);
 
                    console.log(JSON.stringify(this.bluetoothSocket, null, 2));
 
                    plus.android.importClass(this.bluetoothSocket);
                } catch (err) {
                    console.log("蓝牙连接异常!" + err);
                    uni.showToast({
                        title: '蓝牙连接异常!' + err,
                        icon: 'success',
                    });
                }
            }
        },
    };
</script>
 
<style scoped>
    .mui-content {
        margin-top: 12px;
    }
 
    .mui-btn {
        width: 80px;
    }
 
    .ulcss {
        margin-top: 0.625rem;
    }
 
    .acss a {
        text-align: center;
        color: #fff;
        text-decoration: none;
        /* background-color: rgb(100,149,237); */
    }
 
    .title-right {
        float: right;
        line-height: 44px !important;
        font-size: 16px;
    }
</style>