|
|
@@ -4,9 +4,19 @@ var esc = require("@/pagesOrder/js/gprint/esc.js");
|
|
|
export default {
|
|
|
data() {
|
|
|
return {
|
|
|
+ nameBLE:'未连接',
|
|
|
+ devices: [],
|
|
|
+ currDev: null,
|
|
|
+ isAutoBle: false,
|
|
|
+ connId: "",
|
|
|
+ piaojuText: ['测试测试测试测试傻老大傻老大','测试测试测试测试傻老大傻老大','测试测试测试测试傻老大傻老大'],
|
|
|
+ };
|
|
|
+ },
|
|
|
+ onShow() {
|
|
|
+ if(uni.getStorageSync('BLE')){
|
|
|
+ this.nameBLE = uni.getStorageSync('BLE').name;
|
|
|
}
|
|
|
},
|
|
|
- onShow() {},
|
|
|
methods: {
|
|
|
//断开蓝牙链接
|
|
|
destroyed() {
|
|
|
@@ -20,6 +30,7 @@ export default {
|
|
|
//初始化蓝牙
|
|
|
searchBle() {
|
|
|
var that = this;
|
|
|
+ that.isAutoBle = false;
|
|
|
uni.openBluetoothAdapter({
|
|
|
success(res) {
|
|
|
console.log(res,"打开 蓝牙模块");
|
|
|
@@ -55,27 +66,56 @@ export default {
|
|
|
console.log(devices,'设备列表');
|
|
|
var re = JSON.parse(JSON.stringify(devices));
|
|
|
let name = re.devices[0].name?re.devices[0].name:'未知设备';
|
|
|
- let deviceId = re.devices[0].deviceId;
|
|
|
- that.devices.push({name: name,deviceId: deviceId,services: []});
|
|
|
- // that.autoLinkBle();
|
|
|
- // if (name != "未知设备" && name != "") {
|
|
|
- // let deviceId = re.devices[0].deviceId;
|
|
|
- // that.devices.push({name: name,deviceId: deviceId,services: []});
|
|
|
- // }
|
|
|
+ if (name != "未知设备" && name != "") {
|
|
|
+ let deviceId = re.devices[0].deviceId;
|
|
|
+ that.devices.push({name: name,deviceId: deviceId,services: []});
|
|
|
+ // that.$nextTick(()=>{
|
|
|
+ // that.$util.unique(that.devices);
|
|
|
+ // })
|
|
|
+
|
|
|
+ }
|
|
|
});
|
|
|
},
|
|
|
- //自动链接蓝牙
|
|
|
+ //自动链接蓝牙-初始化蓝牙模块
|
|
|
autoLinkBle(){
|
|
|
let self = this;
|
|
|
- // let deviceId = uni.getStorageSync('BLE').deviceId;
|
|
|
+ if(!uni.getStorageSync('BLE')){
|
|
|
+ uni.showModal({
|
|
|
+ title: '提示', content: '请先连接蓝牙',
|
|
|
+ confirmText: "去连接",
|
|
|
+ success: function (res) {
|
|
|
+ if (res.confirm) {
|
|
|
+ uni.navigateTo({url:'/pagesOrder/print'});
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ uni.showLoading({title: '打印中...'});
|
|
|
+ self.isAutoBle = true;
|
|
|
let deviceId = uni.getStorageSync('BLE');
|
|
|
- this.onConn(deviceId);
|
|
|
- // this.devices.forEach((ele)=>{
|
|
|
- // if(ele.deviceId==deviceId){
|
|
|
- // this.senBleLabel();
|
|
|
- // this.onConn(ele);
|
|
|
- // }
|
|
|
- // })
|
|
|
+ //初始化蓝牙模块
|
|
|
+ uni.openBluetoothAdapter({
|
|
|
+ success(res) {
|
|
|
+ self.autoLinkBleFun(deviceId);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+ //自动链接蓝牙
|
|
|
+ autoLinkBleFun(item) {
|
|
|
+ var that = this;
|
|
|
+ let deviceId = item.deviceId;
|
|
|
+ uni.createBLEConnection({
|
|
|
+ deviceId: deviceId,
|
|
|
+ complete(res) {
|
|
|
+ if (res.errMsg == "createBLEConnection:ok") {
|
|
|
+ uni.setStorageSync('BLE',item);
|
|
|
+ setTimeout(function() {
|
|
|
+ that.getBLEServices(deviceId);
|
|
|
+ }, 2000);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
},
|
|
|
stopFindBule() {
|
|
|
console.log("停止搜寻附近的蓝牙外围设备---------------");
|
|
|
@@ -93,7 +133,7 @@ export default {
|
|
|
deviceId: deviceId,
|
|
|
complete(res) {
|
|
|
if (res.errMsg == "createBLEConnection:ok") {
|
|
|
- uni.setStorageSync('BLE',item);
|
|
|
+ that.nameBLE = item.name;
|
|
|
console.log("连接蓝牙-[" + item.name + "]--成功");
|
|
|
that.$msg("连接蓝牙-[" + item.name + "]--成功");
|
|
|
that.connId = deviceId;
|
|
|
@@ -133,30 +173,23 @@ export default {
|
|
|
success(res) {
|
|
|
var re = JSON.parse(JSON.stringify(res));
|
|
|
|
|
|
- console.log(
|
|
|
- "deviceId = [" + deviceId + "] serviceId = [" + serviceId + "]"
|
|
|
- );
|
|
|
+ console.log("deviceId = [" + deviceId + "] serviceId = [" + serviceId + "]");
|
|
|
for (var c = 0; c < re.characteristics.length; c++) {
|
|
|
if (re.characteristics[c].properties.write == true) {
|
|
|
let uuid = re.characteristics[c].uuid;
|
|
|
- console.log(
|
|
|
- " deviceId = [" +
|
|
|
- deviceId +
|
|
|
- "] serviceId = [" +
|
|
|
- serviceId +
|
|
|
- "] characteristics=[" +
|
|
|
- uuid
|
|
|
- );
|
|
|
-
|
|
|
+ console.log(" deviceId = [" +deviceId +"] serviceId = [" +serviceId +"] characteristics=[" +uuid);
|
|
|
+ //是否是直接打印
|
|
|
+ if(that.isAutoBle){that.senBleLabel();return}
|
|
|
for (var index in that.devices) {
|
|
|
if (that.devices[index].deviceId == deviceId) {
|
|
|
- that.devices[index].services.push({
|
|
|
- serviceId: serviceId,
|
|
|
- characteristicId: uuid
|
|
|
- });
|
|
|
+ that.devices[index].services.push({serviceId: serviceId,characteristicId: uuid});
|
|
|
+ that.$nextTick(()=>{
|
|
|
+ uni.setStorageSync('BLE',that.devices[index]);
|
|
|
+ })
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
console.log(JSON.stringify(that.devices));
|
|
|
}
|
|
|
}
|
|
|
@@ -195,6 +228,7 @@ export default {
|
|
|
for (var j = 0; j < data.length; j++) {
|
|
|
dataView.setUint8(j, data[j]);
|
|
|
}
|
|
|
+ uni.hideLoading();
|
|
|
uni.writeBLECharacteristicValue({
|
|
|
deviceId,
|
|
|
serviceId,
|
|
|
@@ -209,7 +243,7 @@ export default {
|
|
|
realWriteData(sendloop, i);
|
|
|
},
|
|
|
senBleLabel() {
|
|
|
- let currDev = uni.getStorageSync('BLE').deviceId;
|
|
|
+ let currDev = uni.getStorageSync('BLE');
|
|
|
//标签模式
|
|
|
let deviceId = currDev.deviceId;
|
|
|
let serviceId = currDev.services[0].serviceId;
|
|
|
@@ -231,7 +265,7 @@ export default {
|
|
|
this.senBlData(deviceId, serviceId, characteristicId, command.getData());
|
|
|
},
|
|
|
billPrint() {
|
|
|
- let currDev = uni.getStorageSync('BLE').deviceId;
|
|
|
+ let currDev = uni.getStorageSync('BLE');
|
|
|
//票据模式
|
|
|
let deviceId = currDev.deviceId;
|
|
|
let serviceId = currDev.services[0].serviceId;
|