From f08105ddbd9acd515fc9d3ed1b72feabdadda81f Mon Sep 17 00:00:00 2001 From: wangfukang <15630117759@163.com> Date: Sun, 17 Mar 2024 10:36:51 +0800 Subject: [PATCH] 11 --- components/kk-printer/index.vue | 128 ++++++++------------------------ 1 file changed, 32 insertions(+), 96 deletions(-) diff --git a/components/kk-printer/index.vue b/components/kk-printer/index.vue index f429425..8613ba3 100644 --- a/components/kk-printer/index.vue +++ b/components/kk-printer/index.vue @@ -86,7 +86,8 @@ writeId: '', readId: '', iosNo: false, - deviceType: 'ios' + deviceType: 'ios', + connected:'' } }, props: { @@ -179,10 +180,38 @@ that.sousuo() }, fail: (res) => { - that.tui.toast('初始化失败', res) + console.log('openBluetoothAdapter fail', res) + if (res.errCode === 10001) { + wx.showModal({ + title: '错误', + content: '未找到蓝牙设备, 请打开蓝牙后重试。', + showCancel: false + }) + wx.onBluetoothAdapterStateChange((res) => { + console.log('onBluetoothAdapterStateChange', res) + if (res.available) { + // 取消监听,否则stopBluetoothDevicesDiscovery后仍会继续触发onBluetoothAdapterStateChange, + // 导致再次调用startBluetoothDevicesDiscovery + wx.onBluetoothAdapterStateChange(() => {}); + this.sousuo() + } + }) + } } }) - + wx.onBLEConnectionStateChange((res) => { + // 该方法回调中可以用于处理连接意外断开等异常情况 + console.log('onBLEConnectionStateChange', + `device ${res.deviceId} state has changed, connected: ${res.connected}`) + this.connected= res.connected + if (!res.connected) { + wx.showModal({ + title: '错误', + content: '蓝牙连接已断开', + showCancel: false + }) + } + }); }, // 开始搜索附近的蓝牙外围设备 sousuo() { @@ -342,99 +371,6 @@ console.log('writeBLECharacteristicValue fail', res) } }) - }, - - - - onGetDevice(res) { - this.devicesList = res; - }, - handleSearchClose() { - this.isShowSearch = false - }, - handleSRRIChange(e) { - this.filterRSSI = e.detail.value - }, - //开始搜索设备 - searchBtnTap() { - blesdk.startBluetoothDevicesDiscovery(); - this.isSearching = true; - blesdk.onfindBlueDevices(this.onGetDevice) - }, - //停止搜索设备 - stopSearchBtnTap() { - blesdk.stopBlueDevicesDiscovery(); - this.isSearching = false; - }, - //点击连接设备 - handleConnectDevice(device) { - - let deviceId = device.deviceId; - let name = device.name; - this.deviceId = deviceId; - uni.setStorageSync('deviceId', deviceId); - uni.onBLEConnectionStateChange(function(res) { - if (res.connected) { - - // plus.nativeUI.toast('设备'+ res.deviceId + '已连接',{ - // verticalAlign:'center' - // }) - } else { - // plus.nativeUI.toast('设备'+ res.deviceId + '已断开连接',{ - // verticalAlign:'center' - // }) - } - }) - blesdk.createBLEConnection(deviceId, this.onConnectSuccess, this.onConnectFail); - this.devicesList == [] - }, - onConnectSuccess(res) { - this.stopSearchBtnTap() - uni.setStorageSync('ok', true) - blesdk.getBLEDeviceServices(this.deviceId, this.onGetServicesSuccess, this.onGetServicesFail); - }, - onConnectFail(err) { - blesdk.closeBLEConnection(uni.getStorageSync('deviceId')); - this.isShowSearch = true - uni.removeStorageSync('deviceId') - uni.removeStorageSync('serviceId') - uni.removeStorageSync('writeId') - uni.removeStorageSync('readId') - // uni. - }, - onGetServicesSuccess(res) { - this.services = res.serviceId; - - blesdk.getDeviceCharacteristics(uni.getStorageSync('deviceId') ? uni.getStorageSync('deviceId') : this - .deviceId, uni.getStorageSync('services') ? uni.getStorageSync('services') : this.services, this - .onGetCharacterSuccess, this.onGetCharacterFail); - }, - onGetServicesFail(err) {}, - onGetCharacterSuccess(res) { - this.serviceId = res.serviceId; - this.writeId = res.writeId; - this.readId = res.readId; - uni.setStorageSync('serviceId', res.serviceId) - uni.setStorageSync('writeId', res.writeId) - uni.setStorageSync('readId', res.readId) - this.isShowSearch = false; - this.handlePrintTap() - }, - onGetCharacterFail(err) { - - }, - onPrintSuccess() { - console.log("打印完成22222") - this.isPrinting = false; - this.$u.route('/pages/toll/toll', { - roomcode: this.roomcode, - CustName: this.CustName, - id: this.roomid - }) - this.$emit('onPrintSuccess') - }, - onPrintFail() { - this.isPrinting = false; } } }