|
@@ -2,9 +2,8 @@ import * as tools from "./tools"
|
|
import {inArray, isObject, logs, sleep} from "../util";
|
|
import {inArray, isObject, logs, sleep} from "../util";
|
|
import {showToast} from "../app";
|
|
import {showToast} from "../app";
|
|
import {checkBleAuth} from "../uniFunction";
|
|
import {checkBleAuth} from "../uniFunction";
|
|
-import {ab2hex, stringToArrayBuffer} from "./hex";
|
|
|
|
|
|
+import {ab2hex, orderSplit, stringToArrayBuffer} from "./hex";
|
|
import {isH5} from "@/utils/platform";
|
|
import {isH5} from "@/utils/platform";
|
|
-import Vue from "vue";
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
* 蓝牙工具类
|
|
* 蓝牙工具类
|
|
@@ -21,6 +20,10 @@ export class bleBase {
|
|
this.serviceId = null
|
|
this.serviceId = null
|
|
this.writeId = null // 写入id
|
|
this.writeId = null // 写入id
|
|
this.characteristicId = null// 接收id
|
|
this.characteristicId = null// 接收id
|
|
|
|
+ this.conSevInterval = 2000
|
|
|
|
+
|
|
|
|
+ this.sList = []
|
|
|
|
+ this.OSList = []
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -34,26 +37,37 @@ export class bleBase {
|
|
let option = {
|
|
let option = {
|
|
second: 5000,
|
|
second: 5000,
|
|
type: 1,
|
|
type: 1,
|
|
|
|
+ start: null,
|
|
fun: null,
|
|
fun: null,
|
|
|
|
+ fail: null,
|
|
}
|
|
}
|
|
option = Object.assign(option, op)
|
|
option = Object.assign(option, op)
|
|
return new Promise(async (resolve, reject) => {
|
|
return new Promise(async (resolve, reject) => {
|
|
logs('进入 searchBle')
|
|
logs('进入 searchBle')
|
|
try {
|
|
try {
|
|
await this.topClass.init()
|
|
await this.topClass.init()
|
|
- if (isH5) throw 'H5不支持蓝牙功能'
|
|
|
|
|
|
+ console.error('getList init')
|
|
await checkBleAuth() // 检查用户蓝牙授权是否通过
|
|
await checkBleAuth() // 检查用户蓝牙授权是否通过
|
|
|
|
+ if (isH5) throw 'H5不支持蓝牙功能'
|
|
if (showLoad) uni.showLoading({title: '搜索设备中。。。'})
|
|
if (showLoad) uni.showLoading({title: '搜索设备中。。。'})
|
|
await tools._startSearch(Object.assign({
|
|
await tools._startSearch(Object.assign({
|
|
- allowDuplicatesKey: true
|
|
|
|
|
|
+ allowDuplicatesKey: true,
|
|
|
|
+ // services: [],
|
|
|
|
+ // powerLevel: 'high',
|
|
}, data))
|
|
}, data))
|
|
|
|
+ if (option.type == 3 && option.start) option.start()
|
|
let list = await this.bluetoothDeviceFound(option.second, option.type, option.fun)
|
|
let list = await this.bluetoothDeviceFound(option.second, option.type, option.fun)
|
|
if (showLoad) uni.hideLoading()
|
|
if (showLoad) uni.hideLoading()
|
|
resolve(Object.values(list))
|
|
resolve(Object.values(list))
|
|
} catch (msg) {
|
|
} catch (msg) {
|
|
|
|
+ console.error('getList err', msg)
|
|
if (showLoad) uni.hideLoading()
|
|
if (showLoad) uni.hideLoading()
|
|
showToast(msg, 4000)
|
|
showToast(msg, 4000)
|
|
- resolve(new Object())
|
|
|
|
|
|
+ if (option.type == 3 && option.fail) {
|
|
|
|
+ option.fail(msg)
|
|
|
|
+ } else {
|
|
|
|
+ reject()
|
|
|
|
+ }
|
|
}
|
|
}
|
|
})
|
|
})
|
|
}
|
|
}
|
|
@@ -71,8 +85,17 @@ export class bleBase {
|
|
} else {
|
|
} else {
|
|
item.ok = true
|
|
item.ok = true
|
|
}
|
|
}
|
|
- if (item.ok && this.option.macSlice) item.mac = item.manufacturerData.slice(...this.option.macSlice)
|
|
|
|
- if (this.option.search && !inArray(item.mac, this.option.machine)) item.ok = !1
|
|
|
|
|
|
+ if (item.ok) {
|
|
|
|
+ if (this.option.macSlice) item.mac = item.manufacturerData.slice(...this.option.macSlice)
|
|
|
|
+ if (this.option.order) {
|
|
|
|
+ let res = orderSplit(item.manufacturerData, this.option.order)
|
|
|
|
+ for (const resKey in res) {
|
|
|
|
+ item[resKey] = res[resKey]
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (this.option.judge) item.ok = this.option.judge(item)
|
|
|
|
+ }
|
|
|
|
+ if (this.option.search && !inArray(item.mac, this.option.machine)) item.ok = false
|
|
return item
|
|
return item
|
|
}
|
|
}
|
|
|
|
|
|
@@ -88,14 +111,22 @@ export class bleBase {
|
|
bluetoothDeviceFound(second = 5000, type = 1, fun = null) {
|
|
bluetoothDeviceFound(second = 5000, type = 1, fun = null) {
|
|
let list = {}
|
|
let list = {}
|
|
return new Promise(async (resolve, reject) => {
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
+ console.log('bleDeviceFound run')
|
|
try {
|
|
try {
|
|
this.topClass.bleDeviceFound((res) => {
|
|
this.topClass.bleDeviceFound((res) => {
|
|
res.devices.forEach((item) => {
|
|
res.devices.forEach((item) => {
|
|
item.manufacturerData = ab2hex(item.advertisData)
|
|
item.manufacturerData = ab2hex(item.advertisData)
|
|
|
|
+ // console.log('res.devices.forEach', item)
|
|
|
|
+ if (item.localName != '') {
|
|
|
|
+ // console.log(`bleDeviceFound forEach localName:${item.localName} deviceId:${item.deviceId} manufacturerData:${item.manufacturerData} advertisData:${item.advertisData}`)
|
|
|
|
+ // console.log('res.devices.forEach', item)
|
|
|
|
+ }
|
|
item = this.filter(item)
|
|
item = this.filter(item)
|
|
|
|
+ // console.log('res.devices.forEach after', item)
|
|
if (item.ok) {
|
|
if (item.ok) {
|
|
list[item.deviceId] = item
|
|
list[item.deviceId] = item
|
|
if (type == 2) {
|
|
if (type == 2) {
|
|
|
|
+ // console.log('type2 stop')
|
|
tools._stopSearchBluetooth();
|
|
tools._stopSearchBluetooth();
|
|
throw '已找到设备'
|
|
throw '已找到设备'
|
|
} else if (type == 3 && fun) {
|
|
} else if (type == 3 && fun) {
|
|
@@ -109,11 +140,13 @@ export class bleBase {
|
|
})
|
|
})
|
|
if (second > 0) {
|
|
if (second > 0) {
|
|
await sleep(second)
|
|
await sleep(second)
|
|
|
|
+ // console.log('sleep stop')
|
|
throw '搜索结束'
|
|
throw '搜索结束'
|
|
}
|
|
}
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
|
+ // console.log('catch stop')
|
|
|
|
+ await tools._stopSearchBluetooth();
|
|
if (type != 3) {
|
|
if (type != 3) {
|
|
- await tools._stopSearchBluetooth();
|
|
|
|
} else if (type == 3 && fun) {
|
|
} else if (type == 3 && fun) {
|
|
fun({
|
|
fun({
|
|
run: false,
|
|
run: false,
|
|
@@ -133,6 +166,7 @@ export class bleBase {
|
|
|
|
|
|
stopSearch() {
|
|
stopSearch() {
|
|
/* #ifndef H5 */
|
|
/* #ifndef H5 */
|
|
|
|
+ // console.log('stopSearch stop')
|
|
tools._stopSearchBluetooth();
|
|
tools._stopSearchBluetooth();
|
|
/* #endif */
|
|
/* #endif */
|
|
}
|
|
}
|
|
@@ -152,36 +186,92 @@ export class bleBase {
|
|
await this.closeConnection() // 结束前一台
|
|
await this.closeConnection() // 结束前一台
|
|
await sleep(500)
|
|
await sleep(500)
|
|
}
|
|
}
|
|
|
|
+ await checkBleAuth() // 检查用户蓝牙授权是否通过
|
|
this.topClass.addConnect(deviceId, this.typeKey)
|
|
this.topClass.addConnect(deviceId, this.typeKey)
|
|
this.deviceId = deviceId
|
|
this.deviceId = deviceId
|
|
logs('connectBle --- ' + this.deviceId)
|
|
logs('connectBle --- ' + this.deviceId)
|
|
await tools._connectBlue(this.deviceId)
|
|
await tools._connectBlue(this.deviceId)
|
|
- await sleep(2000);
|
|
|
|
- await this.getBLEServices(this.option.serviceKey)
|
|
|
|
- await this.getCharacteristics(this.option.channelKey)
|
|
|
|
|
|
+ // await sleep(this.conSevInterval);
|
|
|
|
+ this.serviceId = await this.getBLEServices(this.option.serviceKey)
|
|
|
|
+ await sleep(500)
|
|
|
|
+ const main = await this.getCharacteristics(this.option.channelKey)
|
|
|
|
+ this.writeId = main.writeId // 写入id
|
|
|
|
+ this.characteristicId = main.characteristicId // 写入id
|
|
await tools._notifyBLECharacteristicValueChange(this.deviceId, this.serviceId, this.characteristicId)
|
|
await tools._notifyBLECharacteristicValueChange(this.deviceId, this.serviceId, this.characteristicId)
|
|
return Promise.resolve()
|
|
return Promise.resolve()
|
|
} catch (e) {
|
|
} catch (e) {
|
|
await this.closeConnection()
|
|
await this.closeConnection()
|
|
- console.log('connectionBle catch', e)
|
|
|
|
|
|
+ console.error('connectionBle catch', e)
|
|
return Promise.reject('连接失败')
|
|
return Promise.reject('连接失败')
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- async getBLEServices(serviceKey) {
|
|
|
|
- logs('开始蓝牙获取服务')
|
|
|
|
|
|
+ async getBLEServices_old(serviceKey) {
|
|
|
|
+ if (!this.deviceId) console.error('获取服务错误,没有deviceId')
|
|
let servicesList = await tools._getBLEServices(this.deviceId)
|
|
let servicesList = await tools._getBLEServices(this.deviceId)
|
|
|
|
+
|
|
if (!servicesList) throw '链接失败,蓝牙未找到服务号'
|
|
if (!servicesList) throw '链接失败,蓝牙未找到服务号'
|
|
logs('开始蓝牙获取服务', servicesList)
|
|
logs('开始蓝牙获取服务', servicesList)
|
|
|
|
+ this.sList = servicesList.services
|
|
|
|
+ let uuid = null
|
|
for (let i = 0; i < servicesList.services.length; i++) {
|
|
for (let i = 0; i < servicesList.services.length; i++) {
|
|
- if (servicesList.services[i].isPrimary && (servicesList.services[i].uuid.indexOf(serviceKey) != -1)) {
|
|
|
|
- this.serviceId = servicesList.services[i].uuid
|
|
|
|
- return;
|
|
|
|
|
|
+ if (servicesList.services[i].uuid.indexOf(serviceKey) != -1) {
|
|
|
|
+ // this.serviceId = servicesList.services[i].uuid
|
|
|
|
+ uuid = servicesList.services[i].uuid
|
|
|
|
+ return uuid;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ return uuid;
|
|
}
|
|
}
|
|
|
|
|
|
- async getCharacteristics(channelKey = '') {
|
|
|
|
|
|
+ async getBLEServices(serviceKey) {
|
|
|
|
+ if (!this.deviceId) console.error('获取服务错误,没有deviceId');
|
|
|
|
+
|
|
|
|
+ const timeout = new Promise((_, reject) => {
|
|
|
|
+ setTimeout(() => {
|
|
|
|
+ reject(new Error('超时,未能在5秒内获取到服务'));
|
|
|
|
+ }, 5000);
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ const getService = async () => {
|
|
|
|
+ let uuid = null;
|
|
|
|
+ for (let i = 0; i < 5; i++) {
|
|
|
|
+ try {
|
|
|
|
+ const servicesList = await tools._getBLEServices(this.deviceId);
|
|
|
|
+ if (servicesList) {
|
|
|
|
+ logs('getService', servicesList);
|
|
|
|
+ this.sList = servicesList.services;
|
|
|
|
+ for (let j = 0; j < servicesList.services.length; j++) {
|
|
|
|
+ if (servicesList.services[j].uuid.indexOf(serviceKey) !== -1) {
|
|
|
|
+ uuid = servicesList.services[j].uuid;
|
|
|
|
+ break; // 找到匹配的 uuid 后退出内层循环
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (uuid) break; // 找到匹配的 uuid 后退出外层循环
|
|
|
|
+ }
|
|
|
|
+ } catch (error) {
|
|
|
|
+ // 如果获取失败,继续下一次尝试
|
|
|
|
+ console.error('获取服务失败,重试中...');
|
|
|
|
+ }
|
|
|
|
+ await new Promise(resolve => setTimeout(resolve, 1000)); // 等待1秒后重试
|
|
|
|
+ }
|
|
|
|
+ if (uuid) {
|
|
|
|
+ return uuid;
|
|
|
|
+ } else {
|
|
|
|
+ throw new Error('未能在5秒内获取到服务');
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ const uuid = await Promise.race([getService(), timeout]);
|
|
|
|
+ return uuid;
|
|
|
|
+ } catch (error) {
|
|
|
|
+ throw error;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ async getCharacteristics(channelKey = '', obj = {}) {
|
|
logs('开始蓝牙通道信息')
|
|
logs('开始蓝牙通道信息')
|
|
// 获取通道信息(读、写、监听)
|
|
// 获取通道信息(读、写、监听)
|
|
let writeKey = ''
|
|
let writeKey = ''
|
|
@@ -192,19 +282,28 @@ export class bleBase {
|
|
} else {
|
|
} else {
|
|
writeKey = notifyKey = channelKey
|
|
writeKey = notifyKey = channelKey
|
|
}
|
|
}
|
|
- let {characteristics} = await tools._getCharacteristics(this.deviceId, this.serviceId)
|
|
|
|
|
|
+ let op = {
|
|
|
|
+ deviceId: this.deviceId,
|
|
|
|
+ serviceId: this.serviceId,
|
|
|
|
+ ...obj
|
|
|
|
+ }
|
|
|
|
+ let {characteristics} = await tools._getCharacteristics(op.deviceId, op.serviceId)
|
|
logs(`开始蓝牙通道信息 writeKey:${writeKey} notifyKey:${notifyKey} characteristics:${characteristics}`)
|
|
logs(`开始蓝牙通道信息 writeKey:${writeKey} notifyKey:${notifyKey} characteristics:${characteristics}`)
|
|
-
|
|
|
|
|
|
+ let d = {
|
|
|
|
+ writeId: null,
|
|
|
|
+ characteristicId: null,
|
|
|
|
+ }
|
|
for (let i = 0; i < characteristics.length; i++) {
|
|
for (let i = 0; i < characteristics.length; i++) {
|
|
let item = characteristics[i];
|
|
let item = characteristics[i];
|
|
if (item.uuid.search(writeKey) != -1) {
|
|
if (item.uuid.search(writeKey) != -1) {
|
|
- this.writeId = item.uuid // 写入id
|
|
|
|
|
|
+ d.writeId = item.uuid // 写入id
|
|
}
|
|
}
|
|
if (item.uuid.search(notifyKey) != -1) {
|
|
if (item.uuid.search(notifyKey) != -1) {
|
|
- this.characteristicId = item.uuid // 写入id
|
|
|
|
|
|
+ d.characteristicId = item.uuid // 写入id
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- logs(`开始蓝牙通道信息 writeId:${this.writeId} characteristicId:${this.characteristicId}`)
|
|
|
|
|
|
+ logs(`开始蓝牙通道信息 writeId:${d.writeId} characteristicId:${d.characteristicId}`)
|
|
|
|
+ return d
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -213,8 +312,8 @@ export class bleBase {
|
|
* @param deviceId
|
|
* @param deviceId
|
|
* @returns {Promise<void>}
|
|
* @returns {Promise<void>}
|
|
*/
|
|
*/
|
|
- async closeConnection(deviceId = '') {
|
|
|
|
- if (!deviceId) deviceId = this.deviceId
|
|
|
|
|
|
+ async closeConnection(deviceId = null) {
|
|
|
|
+ if (deviceId == null) deviceId = this.deviceId
|
|
// 断开已连接的连接
|
|
// 断开已连接的连接
|
|
try {
|
|
try {
|
|
if (this.deviceId) await tools._closeBLEConnection(deviceId);
|
|
if (this.deviceId) await tools._closeBLEConnection(deviceId);
|
|
@@ -224,22 +323,32 @@ export class bleBase {
|
|
this.serviceId = null
|
|
this.serviceId = null
|
|
this.writeId = null
|
|
this.writeId = null
|
|
this.characteristicId = null
|
|
this.characteristicId = null
|
|
|
|
+ this.sList = []
|
|
|
|
+ this.OSList = []
|
|
this.topClass.delConnect(deviceId)
|
|
this.topClass.delConnect(deviceId)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- async sentOrder(str, type = 1) {
|
|
|
|
|
|
+ async sentOrder(str, type = 1, obj = {}) {
|
|
let buffer = type != 0 ? stringToArrayBuffer(str, type) : str
|
|
let buffer = type != 0 ? stringToArrayBuffer(str, type) : str
|
|
logs("-- 发送数据: ", ab2hex(buffer, ' '))
|
|
logs("-- 发送数据: ", ab2hex(buffer, ' '))
|
|
|
|
+ let op = {
|
|
|
|
+ deviceId: this.deviceId,
|
|
|
|
+ serviceId: this.serviceId,
|
|
|
|
+ writeId: this.writeId,
|
|
|
|
+ ...obj
|
|
|
|
+ }
|
|
try {
|
|
try {
|
|
/* #ifndef H5 */
|
|
/* #ifndef H5 */
|
|
- if (this.deviceId) await tools._writeBLECharacteristicValue(this.deviceId, this.serviceId, this.writeId, buffer)
|
|
|
|
|
|
+ if (this.deviceId) await tools._writeBLECharacteristicValue(op.deviceId, op.serviceId, op.writeId, buffer)
|
|
/* #endif */
|
|
/* #endif */
|
|
|
|
+ if (op.suc) op.suc()
|
|
return true
|
|
return true
|
|
} catch (error) {
|
|
} catch (error) {
|
|
if (error.errCode == 10006) {
|
|
if (error.errCode == 10006) {
|
|
this.topClass.delConnect(this.deviceId, this.typeKey)
|
|
this.topClass.delConnect(this.deviceId, this.typeKey)
|
|
}
|
|
}
|
|
|
|
+ if (op.err) op.err()
|
|
console.error(error)
|
|
console.error(error)
|
|
logs(`发送出错 基本信息 deviceId:${this.deviceId}, serviceId:${this.serviceId}, writeId:${this.writeId}`)
|
|
logs(`发送出错 基本信息 deviceId:${this.deviceId}, serviceId:${this.serviceId}, writeId:${this.writeId}`)
|
|
logs(error)
|
|
logs(error)
|