首页更新,添加蓝牙电量显示
This commit is contained in:
@ -56,7 +56,7 @@ class BleHelper {
|
||||
isOpenBlue: false, //蓝牙模块是否开启
|
||||
available: false, //蓝牙状态是否可用
|
||||
discovering: false, //蓝牙是否正在搜索
|
||||
searchList: [], //已搜索到的设备列表,
|
||||
|
||||
isSubscribe: false, //是否开启了订阅
|
||||
LinkedList: linkedDevices, //已连接的设备列表
|
||||
platform: systemInfo.uniPlatform,
|
||||
@ -71,9 +71,10 @@ class BleHelper {
|
||||
stateRecoveryCallback: [], //蓝牙适配器恢复可用事件
|
||||
stateBreakCallback: [] //蓝牙适配器不可用事件
|
||||
}
|
||||
this.addReceiveCallback((receive, f, path, recArr) => {
|
||||
recei.ReceiveData(receive, f, path, recArr);
|
||||
}, "BleReceiveData");
|
||||
//蓝牙模块不再订阅,改到首页订阅
|
||||
// this.addReceiveCallback((receive, f, path, recArr) => {
|
||||
// recei.ReceiveData(receive, f, path, recArr);
|
||||
// }, "BleReceiveData");
|
||||
|
||||
setTimeout(() => {
|
||||
this.OpenBlue().then(() => {
|
||||
@ -229,8 +230,12 @@ class BleHelper {
|
||||
}
|
||||
if (key) {
|
||||
// console.log("key=" + key);
|
||||
let f = this.cfg[type].findIndex((v) => {
|
||||
return v.key == key;
|
||||
let f =-1;
|
||||
|
||||
this.cfg[type].find((v,index) => {
|
||||
if(v.key == key){
|
||||
f=index;
|
||||
}
|
||||
});
|
||||
if (f > -1) {
|
||||
// this.cfg[type][f].callback = callback;
|
||||
@ -253,8 +258,11 @@ class BleHelper {
|
||||
|
||||
if (key) {
|
||||
// console.log("key=" + key);
|
||||
let f = this.cfg[type].findIndex((v) => {
|
||||
return v.key == key;
|
||||
let f =-1;
|
||||
this.cfg[type].find((v,index) => {
|
||||
if(v.key == key){
|
||||
f=index;
|
||||
}
|
||||
});
|
||||
if (f > -1) {
|
||||
this.cfg[type].splice(f, 1);
|
||||
@ -268,8 +276,31 @@ class BleHelper {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//获取某个事件的所有订阅者,但不包含某些订阅者
|
||||
getCfgAllKeys(type,retains){
|
||||
if(!retains){
|
||||
retains=[];
|
||||
}
|
||||
if(!Array.isArray(retains)){
|
||||
retains=[retains];
|
||||
}
|
||||
let keys=null;
|
||||
for (let index = 0; index < this.cfg[type]; index++) {
|
||||
let ele = this.cfg[type];
|
||||
let f=retains.find(v=>{
|
||||
return v.toLowerCase()==ele.key.toLowerCase();
|
||||
});
|
||||
if(!f){
|
||||
if(!keys){
|
||||
keys=[ele.key];
|
||||
}else{
|
||||
keys.push(ele.key);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
//设置蓝牙恢复连接的回调
|
||||
addRecoveryCallback(callback, currKey) {
|
||||
@ -280,6 +311,17 @@ class BleHelper {
|
||||
removeRecoveryCallback(currKey) {
|
||||
this.removeCallback(currKey, 'recoveryCallback');
|
||||
}
|
||||
//移除所有蓝牙恢复连接的回调,但不包括currkey
|
||||
removeAllRecoveryCallback(currKey) {
|
||||
let keys=this.getCfgAllKeys('recoveryCallback',currKey);
|
||||
if(keys){
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
let key = keys[i];
|
||||
this.removeRecoveryCallback(key);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//设置蓝牙断开连接的回调
|
||||
addDisposeCallback(callback, currKey) {
|
||||
@ -290,6 +332,16 @@ class BleHelper {
|
||||
removeDisposeCallback(currKey) {
|
||||
this.removeCallback(currKey, 'bleDisposeCallback');
|
||||
}
|
||||
removeAllDisposeCallback(currKey) {
|
||||
let keys=this.getCfgAllKeys('bleDisposeCallback',currKey);
|
||||
if(keys){
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
let key = keys[i];
|
||||
this.removeDisposeCallback(key);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
//设置发现新设备的回调
|
||||
@ -301,6 +353,16 @@ class BleHelper {
|
||||
removeDeviceFound(currKey) {
|
||||
this.removeCallback(currKey, 'onDeviceFound');
|
||||
}
|
||||
removeAllDeviceFound(currKey) {
|
||||
let keys=this.getCfgAllKeys('onDeviceFound',currKey);
|
||||
if(keys){
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
let key = keys[i];
|
||||
this.removeDeviceFound(key);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//添加接收到数据的回调
|
||||
addReceiveCallback(callback, currKey) {
|
||||
@ -311,6 +373,16 @@ class BleHelper {
|
||||
removeReceiveCallback(currKey) {
|
||||
this.removeCallback(currKey, 'receivDataCallback');
|
||||
}
|
||||
removeAllReceiveCallback(currKey) {
|
||||
let keys=this.getCfgAllKeys('receivDataCallback',currKey);
|
||||
if(keys){
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
let key = keys[i];
|
||||
this.removeReceiveCallback(key);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//添加蓝牙不可用的回调
|
||||
addStateBreakCallback(callback, currKey) {
|
||||
@ -321,7 +393,16 @@ class BleHelper {
|
||||
removeStateBreakCallback(currKey) {
|
||||
this.removeCallback(currKey, 'stateBreakCallback');
|
||||
}
|
||||
|
||||
removeAllStateBreakCallback(currKey) {
|
||||
let keys=this.getCfgAllKeys('stateBreakCallback',currKey);
|
||||
if(keys){
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
let key = keys[i];
|
||||
this.removeStateBreakCallback(key);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//设置蓝牙适配器恢复可用的回调
|
||||
addStateRecoveryCallback(callback, currKey) {
|
||||
@ -332,6 +413,16 @@ class BleHelper {
|
||||
removeStateRecoveryCallback(currKey) {
|
||||
this.removeCallback(currKey, 'stateRecoveryCallback');
|
||||
}
|
||||
removeAllStateRecoveryCallback(currKey) {
|
||||
let keys=this.getCfgAllKeys('stateRecoveryCallback',currKey);
|
||||
if(keys){
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
let key = keys[i];
|
||||
this.removeStateRecoveryCallback(key);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//清除所有事件回调
|
||||
removeAllCallback(currKey) {
|
||||
@ -342,7 +433,15 @@ class BleHelper {
|
||||
this.removeStateRecoveryCallback(currKey);
|
||||
this.removeStateBreakCallback(currKey);
|
||||
}
|
||||
|
||||
//清除所有事件回调,不包含retain
|
||||
removeAllCallbackByRetain(retain) {
|
||||
this.removeAllDeviceFound(retain)
|
||||
this.removeAllDisposeCallback(retain);
|
||||
this.removeAllReceiveCallback(retain);
|
||||
this.removeAllRecoveryCallback(retain);
|
||||
this.removeAllStateRecoveryCallback(retain);
|
||||
this.removeAllStateBreakCallback(retain);
|
||||
}
|
||||
|
||||
getError(ex) {
|
||||
let code = ex.code;
|
||||
@ -784,21 +883,24 @@ class BleHelper {
|
||||
}
|
||||
|
||||
BleConnChange() {
|
||||
let stateTimeout=null;
|
||||
uni.onBLEConnectionStateChange((res) => {
|
||||
console.log("蓝牙连接状态变化了", res);
|
||||
|
||||
|
||||
// 检查状态是否真的发生了变化
|
||||
let ble = this.data.LinkedList.find(dev => {
|
||||
return res.deviceId === dev.deviceId;
|
||||
});
|
||||
if (ble) {
|
||||
if (ble.Linked === res.connected) {
|
||||
console.error("专业给UniApp填坑,蓝牙连接状态重复回调");
|
||||
return;
|
||||
}
|
||||
}
|
||||
console.log("蓝牙连接状态变化了", res);
|
||||
|
||||
|
||||
|
||||
setTimeout(() => {
|
||||
clearTimeout(stateTimeout);
|
||||
stateTimeout=setTimeout(() => {
|
||||
if (!res.connected) {
|
||||
console.error("蓝牙已断开", res);
|
||||
let f = this.data.LinkedList.find((
|
||||
@ -851,7 +953,7 @@ class BleHelper {
|
||||
console.log("蓝牙连接已恢复", res);
|
||||
}
|
||||
|
||||
}, 0);
|
||||
}, 50);
|
||||
});
|
||||
}
|
||||
|
||||
@ -881,8 +983,7 @@ class BleHelper {
|
||||
return;
|
||||
}
|
||||
res.devices = arr;
|
||||
this.data.searchList = this.data.searchList.concat(
|
||||
res);
|
||||
|
||||
if (this.cfg.onDeviceFound) {
|
||||
if (this.cfg.onDeviceFound.length > 0) {
|
||||
this.cfg.onDeviceFound.forEach((found) => {
|
||||
@ -907,7 +1008,7 @@ class BleHelper {
|
||||
this.data.available = false;
|
||||
this.data.discovering = false;
|
||||
this.data.isOpenBlue = false;
|
||||
this.data.searchList = [];
|
||||
|
||||
|
||||
return Promise.resolve();
|
||||
}
|
||||
@ -924,7 +1025,7 @@ class BleHelper {
|
||||
this.data.available = false;
|
||||
this.data.discovering = false;
|
||||
this.data.isOpenBlue = false;
|
||||
this.data.searchList = [];
|
||||
|
||||
resolve();
|
||||
},
|
||||
fail: (ex) => {
|
||||
@ -959,20 +1060,15 @@ class BleHelper {
|
||||
if (this.data.platform == 'web') {
|
||||
return Promise.resolve({});
|
||||
}
|
||||
this.data.searchList = [];
|
||||
|
||||
var these = this;
|
||||
//开始搜索
|
||||
var Search = () => {
|
||||
//只搜索合作供应商的服务id
|
||||
let serviceIds = serviceDic.map(v => {
|
||||
return v.serviceId
|
||||
});
|
||||
|
||||
//搜索一个服务id的设备,循环调用
|
||||
let RunSearch = (serviceId) => {
|
||||
let RunSearch = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.startBluetoothDevicesDiscovery({
|
||||
services: serviceId ? [serviceId] : [],
|
||||
services:[],
|
||||
allowDuplicatesKey: true,
|
||||
success: (res) => {
|
||||
console.log('开始搜索蓝牙设备成功');
|
||||
@ -992,14 +1088,8 @@ class BleHelper {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
let promises = [];
|
||||
// for (let i = 0; i < serviceIds.length; i++) {
|
||||
|
||||
// promises.push(RunSearch(serviceIds[i]));
|
||||
|
||||
// }
|
||||
promises.push(RunSearch());
|
||||
Promise.all(promises).then(resolve).catch(reject);
|
||||
|
||||
});
|
||||
|
||||
|
||||
@ -1278,7 +1368,6 @@ class BleHelper {
|
||||
|
||||
|
||||
} else
|
||||
|
||||
{
|
||||
console.error("预设的蓝牙服务和特征中找不到");
|
||||
for (var i = 0; i < res.services.length; i++) {
|
||||
@ -1297,7 +1386,7 @@ class BleHelper {
|
||||
});
|
||||
} else {
|
||||
|
||||
Promise.all(promises)
|
||||
Promise.allSettled(promises)
|
||||
.then(results => {
|
||||
console.log("results= ", results);
|
||||
if (!s) {
|
||||
@ -1970,5 +2059,6 @@ export default {
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
},
|
||||
constService:serviceDic
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user