From a5e7d70a2e597dd8e71b867572f405514f37870e Mon Sep 17 00:00:00 2001 From: wangfukang <15630117759@163.com> Date: Thu, 18 Jul 2024 17:13:56 +0800 Subject: [PATCH] 11 --- components/.DS_Store | Bin 8196 -> 8196 bytes components/kk-printer/index.vue | 129 ++++++++++++++++------ components/kk-printer/utils/bluetoolth.js | 11 +- components/phone-verify/phone-verify.vue | 1 - 4 files changed, 101 insertions(+), 40 deletions(-) diff --git a/components/.DS_Store b/components/.DS_Store index c028802ad6063eb2178fa0275d83f27fdfbf2926..bcf4a53d33b39d42987ea88100f3d7d7d4fb9f02 100644 GIT binary patch delta 76 zcmV-S0JHyuK!iY$PXQmXP`eKSACq7Z6tf@@r~(4P8Ym=oC6%2M$a&ByI iZf5`h00EP(6C9If6Gs6&v#t|01hWSi_5`!p6bA#VFBkd% delta 61 zcmV-D0K)%-K!iY$PXQjWP`eKS9+O}Y6tf@@r~(4Nl#^}}7?a`>Ym;#kU;$f`ZWA1{ T?h_dVk$@z#2N?DQv3Lgqeqj`k diff --git a/components/kk-printer/index.vue b/components/kk-printer/index.vue index 3d0f697..06b1ef9 100644 --- a/components/kk-printer/index.vue +++ b/components/kk-printer/index.vue @@ -120,7 +120,7 @@ let that = this uni.showLoading({ mask: true, - title: '加载中...' + title: '打印中...' }); uni.getSystemInfo({ success(res) { @@ -133,13 +133,26 @@ success: (res2) => { that.sousuo() }, - fail: (res) => { - that.tui.toast('初始化失败', res) - this.isShowSearch = true - uni.hideLoading(); + fail: (res) => { + console.log('初始化失败', res) + if(res.errCode == '10001'){ + console.log('蓝牙未打开', res) + // that.tui.toast('请打开蓝牙后,再进行连接') + uni.showToast({ + duration: 2000, + title: "请打开蓝牙后,再进行连接", + icon: 'none', + mask:true + }) + }else{ + that.tui.toast('连接失败,请重启蓝牙或删除小程序重新进入') + that.isShowSearch = true + } + setTimeout(res=>{ + uni.hideLoading(); + },2000) } }) - }, // 开始搜索附近的蓝牙外围设备 sousuo() { @@ -148,18 +161,20 @@ success(res3) { that.jiantingshebei() }, - fail(err) { - that.tui.toast('获取设备失败', err) + fail(err) { + that.tui.toast('连接失败,请重启蓝牙或删除小程序重新进入') wx.closeBLEConnection({ deviceId: uni.getStorageSync('deviceId'), success(res) { - + console.log(res) } }) uni.removeStorageSync('deviceId'); that.isShowSearch = true - that.sousuo() - uni.hideLoading(); + that.sousuo() + setTimeout(res=>{ + uni.hideLoading(); + },2000) } }) }, @@ -179,7 +194,7 @@ that.lianejieshebei(uni.getStorageSync('deviceId')) } else { uni.hideLoading(); - this.isShowSearch = true + that.isShowSearch = true // 监听扫描到新设备事件 wx.onBluetoothDeviceFound((res4) => { //333 res4.devices.forEach((device) => { @@ -193,7 +208,11 @@ }, lianejieshebei(deviceId) { - let that = this; + let that = this; + uni.showLoading({ + mask: true, + title: '加载中...' + }); // 连接设备 wx.createBLEConnection({ //555 deviceId: deviceId, // 搜索到设备的 deviceId @@ -201,8 +220,8 @@ that.isShowSearch = false that.huoqufuwu(deviceId) }, - fail: (res) => { - that.tui.toast('连接设备失败', res) + fail: (res) => { + that.tui.toast('连接失败,请重启蓝牙或删除小程序重新进入') if (uni.getStorageSync('deviceId')) { that.huoqufuwu(uni.getStorageSync('deviceId')) } else { @@ -216,8 +235,9 @@ that.sousuo() that.isShowSearch = true } - - uni.hideLoading(); + setTimeout(res=>{ + uni.hideLoading(); + },2000) } }) }, @@ -227,12 +247,10 @@ wx.getBLEDeviceServices({ //666 deviceId: deviceId, // 搜索到设备的 deviceId success: (res5) => { - that.duxietezhengzhi(deviceId, res5) }, fail: (res) => { - - that.tui.toast('获取服务失败', res) + that.tui.toast('连接失败,请重启蓝牙或删除小程序重新进入') wx.closeBLEConnection({ deviceId: uni.getStorageSync('deviceId'), success(res1) { @@ -240,7 +258,9 @@ } }) that.isShowSearch = true - uni.hideLoading(); + setTimeout(res=>{ + uni.hideLoading(); + },2000) } }) }, @@ -251,10 +271,11 @@ deviceId: deviceId, // 搜索到设备的 deviceId serviceId: res5.services[0].uuid, // 上一步中找到的某个服务 success: (res6) => { - + console.log('characteristics',res6.characteristics) for (let i = 0; i < res6.characteristics.length; i++) { - let item = res6.characteristics[i] - if (item.properties.write) { // 该特征值可写 + let item = res6.characteristics[i] + that.startNoticeBle(deviceId,res5.services[0].uuid,res6.characteristics[2].uuid) + if (item.properties.write) { // 该特征值可写 uni.setStorageSync('deviceId', deviceId); uni.setStorageSync('serviceId', res5.services[0].uuid); @@ -265,7 +286,7 @@ } }, fail: (res) => { - that.tui.toast('读写特征值失败', res) + that.tui.toast('连接失败,请重启蓝牙或删除小程序重新进入') wx.closeBLEConnection({ deviceId: uni.getStorageSync('deviceId'), success(res) { @@ -273,9 +294,55 @@ } }) that.isShowSearch = true - uni.hideLoading(); + setTimeout(res=>{ + uni.hideLoading(); + },2000) } }) + }, + // 开启蓝牙数据监听 + startNoticeBle(deviceId,serviceId,characteristicId) { + let _this = this + uni.notifyBLECharacteristicValueChange({ + state: true, // 启用 notify 功能 + deviceId: deviceId, + serviceId: serviceId, + characteristicId: characteristicId, + success(res) { + console.log("监听成功===", res.errMsg, 'deviceId===', deviceId) + _this.GetDataFromBle(); + }, + fail: function(err) { + console.log('开启监听失败', err) + } + }) + }, + // 设备返回的数据接收 + GetDataFromBle() { + var _this = this; + uni.onBLECharacteristicValueChange((res) => { + // 此时可以拿到蓝牙设备返回来的数据是一个ArrayBuffer类型数据,所以需要通过一个方法转换成字符串 + console.log('返回的十六进制', _this.ab2hex(res.value)) + _this.bleData = _this.ab2hex(res.value) + uni.hideLoading() + uni.closeBLEConnection({ + deviceId: uni.getStorageSync('deviceId'), + success(res) { + console.log('断开') + }, + fail(err) { + } + }) + }) + }, + ab2hex(buffer) { + const hexArr = Array.prototype.map.call( + new Uint8Array(buffer), + function(bit) { + return ('00' + bit.toString(16)).slice(-2) + } + ) + return hexArr.join('') }, xierushuju() { let that = this; @@ -312,12 +379,10 @@ serviceId: uni.getStorageSync('serviceId'), characteristicId: uni.getStorageSync('characteristicId'), value: buffer, - success(res) { - + success(res) { }, fail(res) { - wx.closeBLEConnection({ deviceId: uni.getStorageSync('deviceId'), success(res) { @@ -325,14 +390,14 @@ } }) if (uni.getStorageSync('deviceId')) { - that.xierushuju() } else { that.jiantingshebei() - } + } + uni.hideLoading(); } }) - uni.hideLoading(); + }, handleSearchClose() { this.isShowSearch = false diff --git a/components/kk-printer/utils/bluetoolth.js b/components/kk-printer/utils/bluetoolth.js index afcd456..5cc3fcd 100644 --- a/components/kk-printer/utils/bluetoolth.js +++ b/components/kk-printer/utils/bluetoolth.js @@ -197,15 +197,10 @@ export function getDeviceCharacteristics(deviceId, services, success, fail) { * @param {function} onChange 监听蓝牙设备传递数据回调函数 */ export function onGetBLECharacteristicValueChange(options, onChange = function() {}) { - - - - uniAsyncPromise('notifyBLECharacteristicValueChange', { state: true, ...options, }).then((res) => { - uni.onBLECharacteristicValueChange(onChange); }); } @@ -410,10 +405,12 @@ function Hex2Str(num) { * 8像素=1mm * printNum: 打印张数,默认为1 * rotation:页面整体旋转 1-90度 2-180度 3-270度 其他-不旋转 + * back: 页面回退 back单位像素 退纸1mm,给back 8 */ -export function CreatCPCLPage(width, height, printNum, rotation, offset = 0) { +export function CreatCPCLPage(width, height, printNum, rotation, back, offset = 0) { var strCmd = '! ' + offset + ' 200 200 ' + height + ' ' + printNum + '\n'; - strCmd += "PAGE-WIDTH " + width + '\n'; + strCmd += "PAGE-WIDTH " + width + '\n'; + strCmd += "BF "+ back + "\n"; if (rotation == 1) strCmd += "ZPROTATE90\n"; else if (rotation == 2) diff --git a/components/phone-verify/phone-verify.vue b/components/phone-verify/phone-verify.vue index af32e01..e0198d2 100644 --- a/components/phone-verify/phone-verify.vue +++ b/components/phone-verify/phone-verify.vue @@ -48,7 +48,6 @@ this.choose = true this.setTime() }else if(reg(e,k) && this.isChoose == true){ - console.log('通过了') // 调用API } },