<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>
|