kyy
2025-08-16 527d1e703b44c173d02eb165c7d6e2345ab436b4
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
<template>
    <view class="mui-content">    
            <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 class="mui-table-view ulcss">
            <view class="mui-table-view-cell acss" @tap="getSettings" style="background-color: rgb(153, 204, 153);">
                <text>刷新</text>
            </view>
        </view>
    </view>
</template>
 
<script>
    export default {
        data() {
            return {
                pdaMac: '',
                printMac:'',// '00:01:78:24:28:32',
            };
        },
        created() {
            this.getSettings();
        },
        methods: {
            //读取pda mac地址
            getSettings() {
                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") {
                        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";
                        }
                        mac = str;
                    }
                    //如果mac使用新方法依然是“02:00:00:00:00:00”则不进行保存
                    if (mac == "02:00:00:00:00:00") {
                        return;
                    }
                    uni.setStorageSync('pdaMac', mac);
                    console.log(mac);
                    this.pdaMac=mac;
                    this.printMac= uni.getStorageSync('printMac');
                }
            },
            //保存地址
            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);
                //先保存在缓存中,若要保存至数据库,在这里post
            },
            //打印测试
            doPrint2() {
                var mac_address =  uni.getStorageSync('printMac');
                console.log('打印开始:'+mac_address)
                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;
                }
            },
        },
    };
</script>
<style scoped>
    .mui-content {
        margin-top:80px;padding:10px;
    }
    .ulcss {
        margin-top: 0.625rem;padding:2px;
    }
    
</style>