fix: 优化连接设备时直接传递mac地址进行连接,避开不必要的搜索
This commit is contained in:
parent
9f79c6c712
commit
f1285cb2e6
@ -66,6 +66,9 @@ export async function selectLock(params) {
|
|||||||
*/
|
*/
|
||||||
export async function openDoor(params) {
|
export async function openDoor(params) {
|
||||||
const {type} = params
|
const {type} = params
|
||||||
|
// ⏱️ 记录开始时间
|
||||||
|
const startTime = Date.now();
|
||||||
|
|
||||||
log.info({
|
log.info({
|
||||||
...new Result(
|
...new Result(
|
||||||
Result.Success.code,
|
Result.Success.code,
|
||||||
@ -102,7 +105,8 @@ export async function openDoor(params) {
|
|||||||
|
|
||||||
// 确认设备连接正常
|
// 确认设备连接正常
|
||||||
if (!params.connected) {
|
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({
|
log.info({
|
||||||
...new Result(
|
...new Result(
|
||||||
searchResult.code,
|
searchResult.code,
|
||||||
@ -266,8 +270,16 @@ export async function openDoor(params) {
|
|||||||
),
|
),
|
||||||
name: 'openDoor'
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
33
uni/basic.js
33
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 timer
|
||||||
// 超时计时器
|
// 超时计时器
|
||||||
let timeoutTimer
|
let timeoutTimer
|
||||||
|
|
||||||
return new Promise(async resolve => {
|
return new Promise(async resolve => {
|
||||||
|
// 如果提供了deviceId,则直接连接
|
||||||
|
if (deviceId) {
|
||||||
|
const connectResult = await createBLEConnection(deviceId)
|
||||||
|
resolve(connectResult)
|
||||||
|
}
|
||||||
const result = await startBluetoothDevicesDiscovery()
|
const result = await startBluetoothDevicesDiscovery()
|
||||||
if (result.code === Result.Success.code) {
|
if (result.code === Result.Success.code) {
|
||||||
let searchFlag = false
|
let searchFlag = false
|
||||||
@ -189,28 +194,16 @@ export function createBLEConnection(deviceId, reconnectNumber = 0) {
|
|||||||
res.data.serviceId,
|
res.data.serviceId,
|
||||||
res.data.notifyCharacteristicId
|
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)
|
resolve(res)
|
||||||
},
|
},
|
||||||
async fail(err) {
|
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) {
|
if (err.errno === 1509007) {
|
||||||
const res = await getBLEDeviceServicesAndCharacteristics(deviceId)
|
const res = await getBLEDeviceServicesAndCharacteristics(deviceId)
|
||||||
await notifyBLECharacteristicValueChange(
|
await notifyBLECharacteristicValueChange(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user