From 1c9477301726107412ecabefc1433e2d309d7f0d Mon Sep 17 00:00:00 2001 From: wangfukang <15630117759@163.com> Date: Fri, 17 Nov 2023 09:51:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=93=E5=8D=B0=E6=9C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/kk-printer/index.vue | 101 ++++++++++++---------- components/kk-printer/utils/bluetoolth.js | 3 +- 2 files changed, 56 insertions(+), 48 deletions(-) diff --git a/components/kk-printer/index.vue b/components/kk-printer/index.vue index a517e5d..81d1219 100644 --- a/components/kk-printer/index.vue +++ b/components/kk-printer/index.vue @@ -32,7 +32,7 @@ - + 设备名称: {{item.name?item.name:'未命名'}} @@ -128,7 +128,6 @@ let filterDevices1 = devices.filter((item)=>{ return item.RSSI > rssi }) - console.log(filterDevices1) // 按名字过滤 let filterDevices2 if(name!=''){ @@ -190,7 +189,6 @@ onceLength:datalen } console.log("开始打印了") - console.log(opt) blesdk.sendDataToDevice(opt); this.isPrinting = false; //打印成功跳转页面 @@ -198,46 +196,51 @@ }) return - } - //若没有已连接设备,弹框搜索设备 - console.log(res,this.deviceId,this.serviceId,this.writeId,this.bufferData,this.onPrintSuccess,'res,this.deviceId,this.serviceId,this.writeId,this.bufferData,this.onPrintSuccess’') - if(res.devices.length == 0){ - console.log("没有设备连接") - this.iosNo = true - this.isShowSearch = true + } + if(uni.getStorageSync('deviceId') != '' && uni.getStorageSync('serviceId') != ''){ + console.log('第二次进入') + let datalen=20; + // #ifdef APP-PLUS + if (plus.os.name != 'Android') + { + datalen=180; + } + // #endif + + this.isPrinting = true; + this.$emit('onPrint'); + this.$nextTick(()=>{ + if(this.bufferData!=''){ + let buffer = gbk.strToGBKByte(this.bufferData) + + let opt = { + deviceId: uni.getStorageSync('deviceId'), + serviceId: uni.getStorageSync('serviceId'), + characteristicId: uni.getStorageSync('writeId'), + value:buffer, + lasterSuccess: this.onPrintSuccess, + onceLength:datalen + } + console.log("开始打印了") + blesdk.sendDataToDevice(opt); + this.isPrinting = false; + //打印成功跳转页面 + } + + }) }else{ - console.log("有设备连接") - let datalen=20; - // #ifdef APP-PLUS - if (plus.os.name != 'Android') - { - datalen=180; - } - // #endif - - this.isPrinting = true; - this.$emit('onPrint'); - this.$nextTick(()=>{ - if(this.bufferData!=''){ - let buffer = gbk.strToGBKByte(this.bufferData) - - let opt = { - deviceId: this.deviceId, - serviceId: this.serviceId, - characteristicId: this.writeId, - value:buffer, - lasterSuccess: this.onPrintSuccess, - onceLength:datalen - } - console.log("开始打印了") - console.log(opt) - blesdk.sendDataToDevice(opt); - this.isPrinting = false; - //打印成功跳转页面 - } - - }) - } + console.log('首次进入') + console.log("没有设备连接") + this.iosNo = true + this.isShowSearch = true + } + + // //若没有已连接设备,弹框搜索设备 + // if(res.devices.length == 0){ + + // }else{ + + // } }).catch((err)=>{ blesdk.catchToast(err); }) @@ -271,8 +274,7 @@ let deviceId = device.deviceId; let name = device.name; this.deviceId = deviceId; - console.log('deviceId',this.deviceId) - // uni.setStorageSync('k_curDeviceID',deviceId); + uni.setStorageSync('deviceId',deviceId); // uni.setStorageSync('k_curDeviceName',name);plus.bluetooth.onBluetoothDeviceFound uni.onBLEConnectionStateChange(function(res){ console.log('连接',res) @@ -298,7 +300,8 @@ }, onGetServicesSuccess(res){ console.log('获取服务',res) - this.services = res.serviceId; + this.services = res.serviceId; + blesdk.getDeviceCharacteristics(this.deviceId, this.services, this.onGetCharacterSuccess, this.onGetCharacterFail); }, onGetServicesFail(err){ @@ -308,7 +311,10 @@ console.log('获取特征值成功',res) this.serviceId = res.serviceId; this.writeId = res.writeId; - this.readId = res.readId; + this.readId = res.readId; + uni.setStorageSync('serviceId',res.serviceId) + uni.setStorageSync('writeId',res.writeId) + uni.setStorageSync('readId',res.readId) this.isShowSearch = false; }, onGetCharacterFail(err){ @@ -443,7 +449,8 @@ width:100%; border-bottom: 1upx solid #ebebeb; padding:10upx 0; - box-sizing: border-box; + box-sizing: border-box; + z-index: 500; &:nth-last-child(1){ border-bottom: none; } diff --git a/components/kk-printer/utils/bluetoolth.js b/components/kk-printer/utils/bluetoolth.js index 0f8884d..9175301 100644 --- a/components/kk-printer/utils/bluetoolth.js +++ b/components/kk-printer/utils/bluetoolth.js @@ -52,7 +52,8 @@ export function onfindBlueDevices(getDevices) { //监听寻找到新设备的事件 uni.onBluetoothDeviceFound((devices)=>{ //获取在蓝牙模块生效期间所有已发现的蓝牙设备 - uniAsyncPromise('getBluetoothDevices').then((res) => { + uniAsyncPromise('getBluetoothDevices').then((res) => { + console.log('res这里',res.devices) getDevices && getDevices(res.devices); }); });