import { closeBluetoothAdapter, createBLEConnection, getBluetoothDevices, offBluetoothAdapterStateChange, startBluetoothDevicesDiscovery, stopBluetoothDevicesDiscovery } from '../uni/basic' import { Result } from '../constant' import { bindLockRequest } from '../api' // 搜索设备 export async function searchDevice(callback) { const result = await startBluetoothDevicesDiscovery() if (result.code === Result.Success.code) { const timestamp = new Date().getTime() let queryFlag = false this.timer = setInterval(async () => { const queryResult = await getBluetoothDevices() if (queryResult.code === Result.Success.code) { const deviceList = queryResult.data if (queryFlag === false && deviceList.length > 0) { queryFlag = true } if (new Date().getTime() - timestamp > 10000 && !queryFlag) { if (this.timer) { clearInterval(this.timer) } callback(Result.NotAvailableWeChatNearbyDevicesEmpty) } const list = [] for (let i = 0; i < deviceList.length; i++) { if (deviceList[i]?.advertisServiceUUIDs) { const uuid = deviceList[i]?.advertisServiceUUIDs[0] if (uuid && uuid.slice(2, 8) === '758824' && uuid.slice(30, 32) === '00') { list.push(deviceList[i]) } if (uuid && uuid.slice(0, 2) === '75' && uuid.slice(4, 6) === '00') { list.push(deviceList[i]) } } } this.searchDeviceList = list callback( new Result(Result.Success.code, { list }) ) } else { callback(queryResult) } }, 1000) } else { callback(result) } } // 停止搜索 export async function stopSearchDevice() { console.log('停止搜索') if (this.timer) { clearInterval(this.timer) } return await stopBluetoothDevicesDiscovery() } /** * 绑定设备 * @param params * @param {Number} [params.uid] 用户ID * @param {String} params.name 设备名称 * @returns {Promise} */ export async function bindDevice(params) { const { name } = params // 设置执行账号 const result = await this.login(params.uid) if (result.code !== Result.Success.code) { return result } const device = this.searchDeviceList.find(item => item.name === name) const connectResult = await createBLEConnection(device.deviceId) if (connectResult.code === Result.Success.code) { this.updateLockInfo({ ...connectResult.data, bluetooth: { bluetoothDeviceId: device.deviceId, bluetoothDeviceName: device.name } }) const publicKeyResult = await this.getPublicKey() if (publicKeyResult.code !== Result.Success.code) { return publicKeyResult } const commKeyResult = await this.getCommKey() if (commKeyResult.code !== Result.Success.code) { return commKeyResult } const lockStatusResult = await this.getLockStatus() if (lockStatusResult.code !== Result.Success.code) { return lockStatusResult } const timestamp = Math.ceil(new Date().getTime() / 1000) const password = (Math.floor(Math.random() * 900000) + 100000).toString() const addUserResult = await this.addLockUser({ params: { name: this.lockInfo.bluetooth.bluetoothDeviceName, keyId: '0', authUid: this.accountInfo.uid.toString(), uid: this.accountInfo.uid.toString(), openMode: 1, keyType: 0, startDate: timestamp, expireDate: 0xffffffff, useCountLimit: 0xffff, isRound: 0, weekRound: 0, startHour: 0, startMin: 0, endHour: 0, endMin: 0, role: 0xff, password }, disconnect: true }) if (addUserResult.code !== Result.Success.code) { return addUserResult } offBluetoothAdapterStateChange() closeBluetoothAdapter() const params = { lockAlias: this.lockInfo.bluetooth.bluetoothDeviceName, lockInfo: { ...this.lockInfo.lockConfig, adminPwd: password }, bluetooth: this.lockInfo.bluetooth, lockUserNo: this.lockInfo.lockUserNo, pwdTimestamp: this.lockInfo.pwdTimestamp, featureValue: this.lockInfo.featureValue, featureSettingValue: this.lockInfo.featureSettingValue, featureSettingParams: this.lockInfo.featureSettingParams } const bindLockResult = await bindLockRequest(params) if (bindLockResult.code === Result.Success.code) { this.updateLockInfo({ lockId: bindLockResult.data.lockId, keyId: bindLockResult.data.keyId, adminPwd: password }) } return new Result( bindLockResult.code, { lock: this.lockInfo }, bindLockResult.message ) } return connectResult }