diff --git a/star-cloud/lock.js b/star-cloud/lock.js index 0181e8a..6618341 100644 --- a/star-cloud/lock.js +++ b/star-cloud/lock.js @@ -66,6 +66,9 @@ export async function selectLock(params) { */ export async function openDoor(params) { const {type} = params + // ⏱️ 记录开始时间 + const startTime = Date.now(); + log.info({ ...new Result( Result.Success.code, @@ -102,7 +105,8 @@ export async function openDoor(params) { // 确认设备连接正常 if (!params.connected) { - const searchResult = await searchAndConnectDevice(this.lockInfo.bluetooth.bluetoothDeviceName) + const searchResult = await searchAndConnectDevice(this.lockInfo.bluetooth.bluetoothDeviceName, true, this.lockInfo.bluetooth.bluetoothDeviceId) + log.info({ ...new Result( searchResult.code, @@ -266,8 +270,16 @@ export async function openDoor(params) { ), name: 'openDoor' }) + // 获取最终结果 + const finalResult = this.getWriteResult(this.openDoor, params); - return this.getWriteResult(this.openDoor, params) + // ⏱️ 计算耗时 + const endTime = Date.now(); + const duration = endTime - startTime; + + console.log(`开门耗时:${duration}`) + + return finalResult; } /** diff --git a/uni/basic.js b/uni/basic.js index f5a0187..cbb9f58 100644 --- a/uni/basic.js +++ b/uni/basic.js @@ -8,13 +8,18 @@ import {Result} from '../constant' */ // 查找设备并连接 -export function searchAndConnectDevice(name, reset = true) { +export function searchAndConnectDevice(name, reset = true, deviceId = null) { // 循环查找设备 let timer // 超时计时器 let timeoutTimer return new Promise(async resolve => { + // 如果提供了deviceId,则直接连接 + if (deviceId) { + const connectResult = await createBLEConnection(deviceId) + resolve(connectResult) + } const result = await startBluetoothDevicesDiscovery() if (result.code === Result.Success.code) { let searchFlag = false @@ -189,28 +194,16 @@ export function createBLEConnection(deviceId, reconnectNumber = 0) { res.data.serviceId, res.data.notifyCharacteristicId ) - // 使用同步方法获取系统信息 - const systemInfo = uni.getSystemInfoSync(); - - if (systemInfo.platform === 'android') { - // 如果是Android机型,则设置BLE MTU - uni.setBLEMTU({ - deviceId: deviceId, - mtu: 512, - fail: res => { - // console.log('mtu fail', res); - }, - success: res => { - // console.log('mtu success', res); - } - }); - } else { - // 对于非Android设备,可以在这里添加其他逻辑或者直接忽略 - // console.log('当前设备不是Android,跳过设置BLE MTU'); - } resolve(res) }, async fail(err) { + if (err.errno === 1500101) { + // 未初始化,初始化成功后在重连 + const result = await openBluetoothAdapter() + if (result.code === Result.Success.code) { + resolve(await createBLEConnection(deviceId, reconnectNumber + 1)) + } + } if (err.errno === 1509007) { const res = await getBLEDeviceServicesAndCharacteristics(deviceId) await notifyBLECharacteristicValueChange(