diff --git a/components/kk-printer/index.vue b/components/kk-printer/index.vue index 3871259..2d357d0 100644 --- a/components/kk-printer/index.vue +++ b/components/kk-printer/index.vue @@ -15,7 +15,7 @@ --> - + 搜索设备 @@ -26,29 +26,21 @@ - + 无可搜索到的设备 - + 设备名称: {{item.name?item.name:'未命名'}} 信号强度: - ({{Math.max(0, item.RSSI + 100)}}%) + ({{item.RSSI}}) - @@ -117,38 +109,6 @@ require: true } }, - computed: { - mapFilterRSSI() { - return (0 - this.filterRSSI) - }, - filterDeviceList() { - let devices = this.devicesList; - let name = this.filterName; - let rssi = this.filterRSSI; - //按RSSI过滤 - let filterDevices1 = devices.filter((item) => { - return item.RSSI > rssi - }) - // 按名字过滤 - let filterDevices2 - if (name != '') { - filterDevices2 = filterDevices1.filter((item) => { - return (item.name.indexOf(name) >= 0 || item.deviceId.indexOf(name) >= 0) - }) - } else { - filterDevices2 = filterDevices1 - } - // 根据广播数据提取MAC地址 - for (let i = 0; i < filterDevices2.length; i++) { - if (filterDevices2[i].hasOwnProperty('advertisData')) { - if (filterDevices2[i].advertisData.byteLength == 8) { - filterDevices2[i].advMac = util.buf2hex(filterDevices2[i].advertisData.slice(2, 7)); - } - } - } - return filterDevices2 - } - }, mounted() { }, @@ -166,6 +126,10 @@ //点击打印按钮 handlePrintTap() { let that = this + uni.showLoading({ + mask:true, + title: '加载中...' + }); uni.getSystemInfo({ success (res) { that.deviceType = res.osName @@ -180,7 +144,9 @@ that.sousuo() }, fail: (res) => { - that.tui.toast('初始化失败', res) + that.tui.toast('初始化失败', res) + this.isShowSearch = true + uni.hideLoading(); } }) @@ -194,10 +160,16 @@ that.jiantingshebei() }, fail(err) { - that.tui.toast('获取设备失败', res) + that.tui.toast('获取设备失败', err) + this.isShowSearch = true + uni.hideLoading(); } }) }, + //停止搜索蓝牙设备 + stopSearchBtnTap(){ + wx.stopBluetoothDevicesDiscovery() + }, /** * jiantingshebei方法,判断缓存中有没有deviceId等数据的时候, * 有: 直接拿到缓存中的deviceId等,直接请求 wx.createBLEConnection @@ -206,17 +178,20 @@ jiantingshebei(){ let that = this; if(uni.getStorageSync('deviceId')){ + console.log("111") that.lianejieshebei(uni.getStorageSync('deviceId')) }else{ + console.log("222") + uni.hideLoading(); + this.isShowSearch = true // 监听扫描到新设备事件 wx.onBluetoothDeviceFound((res4) => { //333 console.log('监听新设备列表,拿到想要的就自动停止', res4) res4.devices.forEach((device) => { - if(device.name == 'QR380A-6824_Ble'){ //搜索到需要的设备 - // 找到要搜索的设备后,及时停止扫描 - wx.stopBluetoothDevicesDiscovery() //444 - that.lianejieshebei(device.deviceId); + if (!device.name && !device.localName) { + return } + that.devicesList.push(device) }) }) } @@ -233,6 +208,8 @@ }, fail: (res) => { that.tui.toast('连接设备失败', res) + this.isShowSearch = true + uni.hideLoading(); } }) }, @@ -247,6 +224,8 @@ }, fail: (res) => { that.tui.toast('获取服务失败', res) + this.isShowSearch = true + uni.hideLoading(); } }) }, @@ -258,51 +237,55 @@ serviceId:res5.services[0].uuid, // 上一步中找到的某个服务 success: (res6) => { console.log('读写特征值成功',res6) - that.xierushuju(deviceId,res5,res6) + for (let i =0; i { that.tui.toast('读写特征值失败', res) + this.isShowSearch = true + uni.hideLoading(); } }) }, - xierushuju(deviceId,res5,res6){ + xierushuju(){ let that = this; //写入数据 - for (let i =0; i { - var dataStr = '' - for(let m=0;m{ + var dataStr = '' + for(let m=0;m