kyy
2025-08-16 527d1e703b44c173d02eb165c7d6e2345ab436b4
加入缺少文件
已添加1个文件
205 ■■■■■ 文件已修改
PadClient2/pages/index/test/PrintInit.vue 205 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
PadClient2/pages/index/test/PrintInit.vue
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,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>