diff --git a/components/kk-printer/index.vue b/components/kk-printer/index.vue index 06b1ef9..8a5529d 100644 --- a/components/kk-printer/index.vue +++ b/components/kk-printer/index.vue @@ -159,10 +159,14 @@ let that = this; wx.startBluetoothDevicesDiscovery({ //222 success(res3) { + console.log('搜索设备成功',res3) that.jiantingshebei() }, - fail(err) { - that.tui.toast('连接失败,请重启蓝牙或删除小程序重新进入') + fail(err) { + console.log('搜索设备失败',err) + if(err.errCode=='10001'){ + that.tui.toast('当前蓝牙不可用') + } wx.closeBLEConnection({ deviceId: uni.getStorageSync('deviceId'), success(res) { @@ -170,8 +174,9 @@ } }) uni.removeStorageSync('deviceId'); - that.isShowSearch = true - that.sousuo() + uni.removeStorageSync('serviceId'); + uni.removeStorageSync('characteristicId'); + that.isShowSearch = true setTimeout(res=>{ uni.hideLoading(); },2000) @@ -181,6 +186,7 @@ //停止搜索蓝牙设备 stopSearchBtnTap() { wx.stopBluetoothDevicesDiscovery() + wx.closeBLEConnection() }, /** * jiantingshebei方法,判断缓存中有没有deviceId等数据的时候, @@ -193,12 +199,15 @@ // that.huoqufuwu(uni.getStorageSync('deviceId')) that.lianejieshebei(uni.getStorageSync('deviceId')) } else { + that.devicesList = [] uni.hideLoading(); that.isShowSearch = true // 监听扫描到新设备事件 wx.onBluetoothDeviceFound((res4) => { //333 + console.log('监听扫描到新设备',res4) res4.devices.forEach((device) => { if (!device.name && !device.localName) { + return } that.devicesList.push(device) @@ -217,11 +226,13 @@ wx.createBLEConnection({ //555 deviceId: deviceId, // 搜索到设备的 deviceId success: () => { + console.log('链接设备成功') that.isShowSearch = false that.huoqufuwu(deviceId) }, - fail: (res) => { - that.tui.toast('连接失败,请重启蓝牙或删除小程序重新进入') + fail: (res) => { + console.log('链接设备失败',res) + // that.tui.toast('连接失败,请重启蓝牙或删除小程序重新进入') if (uni.getStorageSync('deviceId')) { that.huoqufuwu(uni.getStorageSync('deviceId')) } else { @@ -231,7 +242,9 @@ } }) - uni.removeStorageSync('deviceId') + uni.removeStorageSync('deviceId'); + uni.removeStorageSync('serviceId'); + uni.removeStorageSync('characteristicId'); that.sousuo() that.isShowSearch = true } @@ -247,14 +260,19 @@ wx.getBLEDeviceServices({ //666 deviceId: deviceId, // 搜索到设备的 deviceId success: (res5) => { + console.log('获取服务成功',res5) that.duxietezhengzhi(deviceId, res5) }, fail: (res) => { - that.tui.toast('连接失败,请重启蓝牙或删除小程序重新进入') + console.log('获取服务失败',res) + uni.removeStorageSync('deviceId'); + uni.removeStorageSync('serviceId'); + uni.removeStorageSync('characteristicId'); + // that.tui.toast('连接失败,请重启蓝牙或删除小程序重新进入') wx.closeBLEConnection({ deviceId: uni.getStorageSync('deviceId'), success(res1) { - + that.handlePrintTap() } }) that.isShowSearch = true @@ -271,7 +289,7 @@ deviceId: deviceId, // 搜索到设备的 deviceId serviceId: res5.services[0].uuid, // 上一步中找到的某个服务 success: (res6) => { - console.log('characteristics',res6.characteristics) + console.log('读写特征值成功',res6) for (let i = 0; i < res6.characteristics.length; i++) { let item = res6.characteristics[i] that.startNoticeBle(deviceId,res5.services[0].uuid,res6.characteristics[2].uuid) @@ -286,7 +304,8 @@ } }, fail: (res) => { - that.tui.toast('连接失败,请重启蓝牙或删除小程序重新进入') + console.log('读写特征值失败',res) + // that.tui.toast('连接失败,请重启蓝牙或删除小程序重新进入') wx.closeBLEConnection({ deviceId: uni.getStorageSync('deviceId'), success(res) {