From 071e68557bcbc707ce59452f439d46f9ee481707 Mon Sep 17 00:00:00 2001 From: wangfukang <15630117759@163.com> Date: Fri, 4 Sep 2026 09:26:55 +0800 Subject: [PATCH] 1 --- App.vue | 15 +++- components/print/bluetooth.js | 147 +++++++++++++++++++++++--------- components/print/index.js | 22 +---- components/tab-bar/myCenter.vue | 8 +- pages/login/login.vue | 42 +++++++-- pages/myCenter/setPrint.vue | 108 ++++++++++++++--------- 6 files changed, 230 insertions(+), 112 deletions(-) diff --git a/App.vue b/App.vue index 86c60e0..4e468fd 100644 --- a/App.vue +++ b/App.vue @@ -23,6 +23,7 @@ genTestUserSig } from './debug/GenerateTestUserSig.js'; import printer from './components/print/index.js'; // app.js + import Bluetooth from './components/print/bluetooth.js'; // const aegis = new Aegis({ // id: 'iHWefAYqKznuxWjLnr', // 项目key // reportApiSpeed: true, // 接口测速 @@ -208,6 +209,7 @@ this.globalData.GaoDeKey_amapkey = '52de86da47be2ea547c37dd382025a0c' // #endif if(uni.getStorageSync('peisongyuan')) return; + this.autoConnectPrinter() if (!uni.getStorageSync('bluetoothDeviceId')) { uni.showModal({ title: '提示', @@ -230,6 +232,15 @@ }, }, methods: { + autoConnectPrinter() { + if (!uni.getStorageSync('bluetoothDeviceId')) return; + const bluetooth = new Bluetooth({ + autoOpen: false + }); + bluetooth.connectSavedDevice({ + silent: true + }).catch(() => {}); + }, processPrintQueue() { if (this.globalData.printData.length === 0) { this.globalData.isPrinting = false @@ -330,8 +341,8 @@ }, getRegistrationID() { //获取registerID jpushModule.getRegistrationID(result => { - let registerID = result.registerID - if (result && result.registerID) { + let registerID = result && (result.registerID || result.registrationID) + if (registerID) { this.globalData.registrationID = registerID uni.setStorageSync("registerID", registerID) } diff --git a/components/print/bluetooth.js b/components/print/bluetooth.js index dc719f7..3e6fd9c 100644 --- a/components/print/bluetooth.js +++ b/components/print/bluetooth.js @@ -1,6 +1,6 @@ class Bluetooth { - constructor() { + constructor(options = {}) { this.isOpenBle = false; this.deviceId = ""; this.serviceId = ""; @@ -8,7 +8,11 @@ class Bluetooth { this.notifyId = ""; this.num = 0; this.serviceList = []; - this.openBluetoothAdapter(); + if (options.autoOpen !== false) { + this.openBluetoothAdapter({ + silent: true + }).catch(() => {}); + } } showToast(title) { @@ -19,7 +23,8 @@ class Bluetooth { }); } - openBluetoothAdapter() { + openBluetoothAdapter(options = {}) { + const silent = options.silent === true; return new Promise((resolve, reject) => { uni.openBluetoothAdapter({ success: res => { @@ -28,7 +33,9 @@ class Bluetooth { resolve(res); }, fail: err => { - this.showToast('蓝牙状态读取失败,请检查是否打开蓝牙'); + if (!silent) { + this.showToast('蓝牙状态读取失败,请检查是否打开蓝牙'); + } reject(err); }, }); @@ -52,7 +59,7 @@ class Bluetooth { success: res => { resolve(res) }, - fail: res => { + fail: err => { self.showToast(`搜索设备失败` + JSON.stringify(err)); reject(err); } @@ -68,7 +75,7 @@ class Bluetooth { success: e => { uni.hideLoading(); }, - fail: e => { + fail: err => { uni.hideLoading(); self.showToast(`停止搜索蓝牙设备失败` + JSON.stringify(err)); } @@ -76,14 +83,71 @@ class Bluetooth { }); } - createBLEConnection(deviceId) { + saveDevice(deviceId, deviceName = '') { + if (!deviceId) return; + uni.setStorageSync('bluetoothDeviceId', deviceId); + if (deviceName) { + uni.setStorageSync('bluetoothDeviceName', deviceName); + } + } + + removeSavedDevice() { + uni.removeStorageSync('bluetoothDeviceId'); + uni.removeStorageSync('bluetoothDeviceName'); + uni.removeStorageSync('serviceId'); + uni.removeStorageSync('notifyId'); + uni.removeStorageSync('writeId'); + } + + resetConnectionInfo() { + this.serviceId = ""; + this.writeId = ""; + this.notifyId = ""; + this.num = 0; + this.serviceList = []; + } + + async connectDevice(deviceId, options = {}) { + if (!deviceId) { + return Promise.reject('设备ID不能为空'); + } + await this.openBluetoothAdapter(options); + this.deviceId = deviceId; + this.resetConnectionInfo(); + await this.createBLEConnection(deviceId, options); + this.serviceList = await this.getBLEDeviceServices(options); + await this.getBLEDeviceCharacteristics(options); + this.saveDevice(deviceId, options.deviceName); + return { + deviceId: this.deviceId, + serviceId: this.serviceId, + notifyId: this.notifyId, + writeId: this.writeId + }; + } + + connectSavedDevice(options = {}) { + const deviceId = uni.getStorageSync('bluetoothDeviceId'); + if (!deviceId) { + return Promise.resolve(null); + } + return this.connectDevice(deviceId, { + ...options, + deviceName: options.deviceName || uni.getStorageSync('bluetoothDeviceName') + }); + } + + createBLEConnection(deviceId, options = {}) { //设备deviceId let self = this; + const silent = options.silent === true; - uni.showLoading({ - mask: true, - title: '设别连接中,请稍候...' - }) + if (!silent) { + uni.showLoading({ + mask: true, + title: '设备连接中,请稍候...' + }) + } return new Promise((resolve, reject) => { uni.createBLEConnection({ deviceId, @@ -97,24 +161,29 @@ class Bluetooth { return true; } if (err.errCode == 10012) { - self.showToast(`蓝牙连接超时`); - } else { - self.showToast(`停止搜索蓝牙设备失败` + JSON.stringify(err)); + if (!silent) { + self.showToast(`蓝牙连接超时`); + } + } else if (!silent) { + self.showToast(`蓝牙连接失败` + JSON.stringify(err)); } reject(err); }, complete() { - uni.hideLoading(); + if (!silent) { + uni.hideLoading(); + } } }) }); } //获取蓝牙设备所有服务(service) - getBLEDeviceServices() { + getBLEDeviceServices(options = {}) { let _serviceList = []; let deviceId = this.deviceId; let self = this; + const silent = options.silent === true; return new Promise((resolve, reject) => { setTimeout(() => { uni.getBLEDeviceServices({ @@ -125,12 +194,16 @@ class Bluetooth { _serviceList.push(service); } } - uni.hideLoading(); + if (!silent) { + uni.hideLoading(); + } resolve(_serviceList) }, fail: err => { - uni.hideLoading(); - self.showToast(`获取设备Services` + JSON.stringify(err)); + if (!silent) { + uni.hideLoading(); + self.showToast(`获取设备Services` + JSON.stringify(err)); + } reject(err); }, }) @@ -139,10 +212,14 @@ class Bluetooth { } //获取蓝牙设备某个服务中所有特征值(characteristic) - getBLEDeviceCharacteristics() { + getBLEDeviceCharacteristics(options = {}) { let self = this; let deviceId = self.deviceId; let service = self.serviceList; + const silent = options.silent === true; + if (!service || !service[self.num]) { + return Promise.reject('找不到该读写的服务'); + } var uuid = service[self.num].uuid; return new Promise((resolve, reject) => { uni.getBLEDeviceCharacteristics({ @@ -167,10 +244,12 @@ class Bluetooth { reject('找不到该读写的特征值'); return false; } else { - self.getBLEDeviceCharacteristics() + self.getBLEDeviceCharacteristics(options).then(resolve).catch(reject); + return; } } else { self.serviceId = uuid; + uni.setStorageSync('serviceId', self.serviceId); } let result = { 'notifyId': self.notifyId, @@ -180,7 +259,9 @@ class Bluetooth { resolve(result) }, fail: err => { - self.showToast(`getBLEDeviceCharacteristics` + JSON.stringify(err)); + if (!silent) { + self.showToast(`getBLEDeviceCharacteristics` + JSON.stringify(err)); + } reject(err); } }) @@ -188,8 +269,8 @@ class Bluetooth { } //断开联链接 - closeBLEConnection() { - let deviceId = this.deviceId; + closeBLEConnection(deviceId = this.deviceId) { + if (!deviceId) return; uni.closeBLEConnection({ deviceId, success(res) { @@ -248,23 +329,7 @@ class Bluetooth { //若APP在之前已有搜索过某个蓝牙设备,并成功建立连接,可直接传入之前搜索获取的 deviceId 直接尝试连接该设备,无需进行搜索操作。 reconnect() { - (async () => { - try { - this.deviceId = this.deviceId || uni.getStorageSync("deviceId"); - this.serviceId = this.serviceId || uni.getStorageSync("serviceId"); - - let result1 = await this.createBLEConnection(); - - let result2 = await this.getBLEDeviceServices(); - - let result3 = await this.getBLEDeviceCharacteristics(); - - - } catch (err) { - - } - - })(); + return this.connectSavedDevice(); } } diff --git a/components/print/index.js b/components/print/index.js index 53ec13e..c19d33a 100644 --- a/components/print/index.js +++ b/components/print/index.js @@ -61,26 +61,8 @@ export default { async bindViewTap(deviceId, state, onDone) { var _this = this; try { - await bluetooth.createBLEConnection(deviceId).then((res) => { - bluetooth.deviceId = deviceId; - }).catch((e) => { - throw e; - }); - let server = []; - await bluetooth.getBLEDeviceServices().then((res) => { - bluetooth.notifyId = ''; - bluetooth.writeId = ''; - bluetooth.num = 0; - bluetooth.serviceList = res - }).catch((e) => { - throw e; - }); - let result3 = await bluetooth.getBLEDeviceCharacteristics().then(res => { - uni.setStorageSync('bluetoothDeviceId', deviceId); - this.pickUpOnce(onDone) - }).catch((e) => { - throw e; - }); + await bluetooth.connectDevice(deviceId); + this.pickUpOnce(onDone) } catch (e) { if (typeof onDone === 'function') onDone(); } diff --git a/components/tab-bar/myCenter.vue b/components/tab-bar/myCenter.vue index 5a61f92..3776459 100644 --- a/components/tab-bar/myCenter.vue +++ b/components/tab-bar/myCenter.vue @@ -148,9 +148,11 @@ }, getRegistrationID() { //获取registerID jpushModule.getRegistrationID(result => { - let registerID = result.registerID - - uni.setStorageSync("registerID", registerID) + let registerID = result && (result.registerID || result.registrationID) + if (registerID) { + getApp().globalData.registrationID = registerID + uni.setStorageSync("registerID", registerID) + } }) }, diff --git a/pages/login/login.vue b/pages/login/login.vue index ffc142a..5361757 100644 --- a/pages/login/login.vue +++ b/pages/login/login.vue @@ -68,6 +68,9 @@