蓝牙小优化,App隐藏时断开设备连接
This commit is contained in:
@ -56,7 +56,7 @@ class BleHelper {
|
||||
isOpenBlue: false, //蓝牙模块是否开启
|
||||
available: false, //蓝牙状态是否可用
|
||||
discovering: false, //蓝牙是否正在搜索
|
||||
|
||||
|
||||
isSubscribe: false, //是否开启了订阅
|
||||
LinkedList: linkedDevices, //已连接的设备列表
|
||||
platform: systemInfo.uniPlatform,
|
||||
@ -139,7 +139,7 @@ class BleHelper {
|
||||
{
|
||||
key: '10013',
|
||||
remark: '连接 deviceId 为空或者是格式不正确'
|
||||
},
|
||||
},
|
||||
{
|
||||
key: '10016',
|
||||
remark: '定位服务已关闭'
|
||||
@ -234,11 +234,11 @@ class BleHelper {
|
||||
}
|
||||
if (key) {
|
||||
// console.log("key=" + key);
|
||||
let f =-1;
|
||||
|
||||
this.cfg[type].find((v,index) => {
|
||||
if(v.key == key){
|
||||
f=index;
|
||||
let f = -1;
|
||||
|
||||
this.cfg[type].find((v, index) => {
|
||||
if (v.key == key) {
|
||||
f = index;
|
||||
}
|
||||
});
|
||||
if (f > -1) {
|
||||
@ -262,10 +262,10 @@ class BleHelper {
|
||||
|
||||
if (key) {
|
||||
// console.log("key=" + key);
|
||||
let f =-1;
|
||||
this.cfg[type].find((v,index) => {
|
||||
if(v.key == key){
|
||||
f=index;
|
||||
let f = -1;
|
||||
this.cfg[type].find((v, index) => {
|
||||
if (v.key == key) {
|
||||
f = index;
|
||||
}
|
||||
});
|
||||
if (f > -1) {
|
||||
@ -281,26 +281,26 @@ class BleHelper {
|
||||
|
||||
|
||||
//获取某个事件的所有订阅者,但不包含某些订阅者
|
||||
getCfgAllKeys(type,retains){
|
||||
if(!retains){
|
||||
retains=[];
|
||||
getCfgAllKeys(type, retains) {
|
||||
if (!retains) {
|
||||
retains = [];
|
||||
}
|
||||
if(!Array.isArray(retains)){
|
||||
retains=[retains];
|
||||
if (!Array.isArray(retains)) {
|
||||
retains = [retains];
|
||||
}
|
||||
let keys=null;
|
||||
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();
|
||||
let f = retains.find(v => {
|
||||
return v.toLowerCase() == ele.key.toLowerCase();
|
||||
});
|
||||
if(!f){
|
||||
if(!keys){
|
||||
keys=[ele.key];
|
||||
}else{
|
||||
keys.push(ele.key);
|
||||
if (!f) {
|
||||
if (!keys) {
|
||||
keys = [ele.key];
|
||||
} else {
|
||||
keys.push(ele.key);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
return keys;
|
||||
@ -316,15 +316,15 @@ class BleHelper {
|
||||
this.removeCallback(currKey, 'recoveryCallback');
|
||||
}
|
||||
//移除所有蓝牙恢复连接的回调,但不包括currkey
|
||||
removeAllRecoveryCallback(currKey) {
|
||||
let keys=this.getCfgAllKeys('recoveryCallback',currKey);
|
||||
if(keys){
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
//设置蓝牙断开连接的回调
|
||||
@ -337,14 +337,14 @@ class BleHelper {
|
||||
this.removeCallback(currKey, 'bleDisposeCallback');
|
||||
}
|
||||
removeAllDisposeCallback(currKey) {
|
||||
let keys=this.getCfgAllKeys('bleDisposeCallback',currKey);
|
||||
if(keys){
|
||||
let keys = this.getCfgAllKeys('bleDisposeCallback', currKey);
|
||||
if (keys) {
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
let key = keys[i];
|
||||
this.removeDisposeCallback(key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -358,14 +358,14 @@ class BleHelper {
|
||||
this.removeCallback(currKey, 'onDeviceFound');
|
||||
}
|
||||
removeAllDeviceFound(currKey) {
|
||||
let keys=this.getCfgAllKeys('onDeviceFound',currKey);
|
||||
if(keys){
|
||||
let keys = this.getCfgAllKeys('onDeviceFound', currKey);
|
||||
if (keys) {
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
let key = keys[i];
|
||||
this.removeDeviceFound(key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
//添加接收到数据的回调
|
||||
@ -378,14 +378,14 @@ class BleHelper {
|
||||
this.removeCallback(currKey, 'receivDataCallback');
|
||||
}
|
||||
removeAllReceiveCallback(currKey) {
|
||||
let keys=this.getCfgAllKeys('receivDataCallback',currKey);
|
||||
if(keys){
|
||||
let keys = this.getCfgAllKeys('receivDataCallback', currKey);
|
||||
if (keys) {
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
let key = keys[i];
|
||||
this.removeReceiveCallback(key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
//添加蓝牙不可用的回调
|
||||
@ -398,14 +398,14 @@ class BleHelper {
|
||||
this.removeCallback(currKey, 'stateBreakCallback');
|
||||
}
|
||||
removeAllStateBreakCallback(currKey) {
|
||||
let keys=this.getCfgAllKeys('stateBreakCallback',currKey);
|
||||
if(keys){
|
||||
let keys = this.getCfgAllKeys('stateBreakCallback', currKey);
|
||||
if (keys) {
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
let key = keys[i];
|
||||
this.removeStateBreakCallback(key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
//设置蓝牙适配器恢复可用的回调
|
||||
@ -418,14 +418,14 @@ class BleHelper {
|
||||
this.removeCallback(currKey, 'stateRecoveryCallback');
|
||||
}
|
||||
removeAllStateRecoveryCallback(currKey) {
|
||||
let keys=this.getCfgAllKeys('stateRecoveryCallback',currKey);
|
||||
if(keys){
|
||||
let keys = this.getCfgAllKeys('stateRecoveryCallback', currKey);
|
||||
if (keys) {
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
let key = keys[i];
|
||||
this.removeStateRecoveryCallback(key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
//清除所有事件回调
|
||||
@ -448,10 +448,11 @@ class BleHelper {
|
||||
}
|
||||
|
||||
getError(ex) {
|
||||
let code = ex.code;
|
||||
ex.msg = code
|
||||
|
||||
ex.msg =ex.code+':'+ ex.errMsg;
|
||||
|
||||
let f = this.dic.errRemarks.find((v) => {
|
||||
return v.key == code;
|
||||
return v.key == ex.code;
|
||||
});
|
||||
if (f) {
|
||||
ex.msg = f.remark;
|
||||
@ -733,20 +734,26 @@ class BleHelper {
|
||||
let trimmedStr = str.trim();
|
||||
if (trimmedStr && (trimmedStr.startsWith('{') || trimmedStr.startsWith('['))) {
|
||||
let receivJson = JSON.parse(str);
|
||||
let key = "sta_address"; //HBY100以此方式上传mac地址
|
||||
if (key in receivJson) {
|
||||
|
||||
if ("sta_address" in receivJson || "sta_imei" in receivJson) {
|
||||
this.data.LinkedList.find((v) => {
|
||||
if (v.deviceId == receive
|
||||
.deviceId) {
|
||||
let macStr = receivJson[
|
||||
key];
|
||||
if (macStr.includes(':')) {
|
||||
v.macAddress = macStr;
|
||||
} else {
|
||||
v.macAddress = macStr
|
||||
.replace(/(.{2})/g,
|
||||
'$1:').slice(0,
|
||||
-1)
|
||||
let macStr = receivJson["sta_address"];
|
||||
if (macStr) {
|
||||
if (macStr.includes(':')) {
|
||||
v.macAddress = macStr;
|
||||
} else {
|
||||
v.macAddress = macStr
|
||||
.replace(/(.{2})/g,
|
||||
'$1:').slice(0,
|
||||
-1)
|
||||
}
|
||||
}
|
||||
|
||||
let sta_imei=receivJson["sta_imei"];
|
||||
if(sta_imei){
|
||||
v.imei=sta_imei;
|
||||
}
|
||||
|
||||
isUpdate = true;
|
||||
@ -754,6 +761,8 @@ class BleHelper {
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
} catch (convertException) {
|
||||
if (str && (str.trim().startsWith('{') || str.trim().startsWith('['))) {
|
||||
@ -779,23 +788,23 @@ class BleHelper {
|
||||
str: str,
|
||||
hexs: hexs
|
||||
};
|
||||
// console.log("监听到特征值:",recData);
|
||||
// console.log("监听到特征值:", recData);
|
||||
if (this.cfg.receivDataCallback) {
|
||||
|
||||
if (this.cfg.receivDataCallback.length > 0) {
|
||||
|
||||
// console.log("有人订阅消息")
|
||||
// console.log("有人订阅消息")
|
||||
this.cfg.receivDataCallback.forEach((
|
||||
rec) => {
|
||||
// console.log("有人订阅消息111", )
|
||||
// console.log("有人订阅消息111", )
|
||||
if (rec.callback) {
|
||||
try {
|
||||
// console.log("正在处理订阅消息",rec);
|
||||
// console.log("正在处理订阅消息",rec);
|
||||
rec.callback(recData, f,
|
||||
rec.key, this.cfg
|
||||
.receivDataCallback
|
||||
);
|
||||
// console.log("处理订阅消息完毕");
|
||||
// console.log("处理订阅消息完毕");
|
||||
} catch (err) {
|
||||
console.error(
|
||||
"订阅消息出现异常",
|
||||
@ -886,10 +895,10 @@ class BleHelper {
|
||||
}
|
||||
|
||||
BleConnChange() {
|
||||
let stateTimeout=null;
|
||||
let stateTimeout = null;
|
||||
uni.onBLEConnectionStateChange((res) => {
|
||||
|
||||
|
||||
|
||||
|
||||
// 检查状态是否真的发生了变化
|
||||
let ble = this.data.LinkedList.find(dev => {
|
||||
return res.deviceId === dev.deviceId;
|
||||
@ -903,7 +912,7 @@ class BleHelper {
|
||||
// console.log("蓝牙连接状态变化了", res);
|
||||
|
||||
clearTimeout(stateTimeout);
|
||||
stateTimeout=setTimeout(() => {
|
||||
stateTimeout = setTimeout(() => {
|
||||
if (!res.connected) {
|
||||
// console.error("蓝牙已断开", res);
|
||||
let f = this.data.LinkedList.find((
|
||||
@ -986,7 +995,7 @@ class BleHelper {
|
||||
return;
|
||||
}
|
||||
res.devices = arr;
|
||||
|
||||
|
||||
if (this.cfg.onDeviceFound) {
|
||||
if (this.cfg.onDeviceFound.length > 0) {
|
||||
this.cfg.onDeviceFound.forEach((found) => {
|
||||
@ -1011,7 +1020,7 @@ class BleHelper {
|
||||
this.data.available = false;
|
||||
this.data.discovering = false;
|
||||
this.data.isOpenBlue = false;
|
||||
|
||||
|
||||
|
||||
return Promise.resolve();
|
||||
}
|
||||
@ -1028,7 +1037,7 @@ class BleHelper {
|
||||
this.data.available = false;
|
||||
this.data.discovering = false;
|
||||
this.data.isOpenBlue = false;
|
||||
|
||||
|
||||
resolve();
|
||||
},
|
||||
fail: (ex) => {
|
||||
@ -1063,7 +1072,7 @@ class BleHelper {
|
||||
if (this.data.platform == 'web') {
|
||||
return Promise.resolve({});
|
||||
}
|
||||
|
||||
|
||||
var these = this;
|
||||
//开始搜索
|
||||
var Search = () => {
|
||||
@ -1071,7 +1080,7 @@ class BleHelper {
|
||||
let RunSearch = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.startBluetoothDevicesDiscovery({
|
||||
services:[],
|
||||
services: [],
|
||||
allowDuplicatesKey: true,
|
||||
success: (res) => {
|
||||
console.log('开始搜索蓝牙设备成功');
|
||||
@ -1227,7 +1236,7 @@ class BleHelper {
|
||||
// console.log("success SubScribe");
|
||||
succ(
|
||||
res
|
||||
); //见了鬼了,有时候执行了succ但promise永远pending了
|
||||
); //见了鬼了,有时候执行了succ但promise永远pending了
|
||||
},
|
||||
fail: (ex) => {
|
||||
|
||||
@ -1370,8 +1379,7 @@ class BleHelper {
|
||||
// notifyCharId));
|
||||
|
||||
|
||||
} else
|
||||
{
|
||||
} else {
|
||||
console.error("预设的蓝牙服务和特征中找不到");
|
||||
for (var i = 0; i < res.services.length; i++) {
|
||||
let service = res.services[i];
|
||||
@ -1604,23 +1612,23 @@ class BleHelper {
|
||||
timeout: 15000,
|
||||
success: (info) => {
|
||||
//释放连接锁
|
||||
|
||||
|
||||
delete this.data.connectingDevices[deviceId];
|
||||
console.log("新连接成功", this.data.LinkedList);
|
||||
|
||||
|
||||
// 处理 MTU 设置
|
||||
if (plus.os.name === 'Android') {
|
||||
this.setMtu(deviceId).catch(ex => {
|
||||
console.error("mtu设置失败=", ex);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
this.getLinkBlue().then((arr) => {
|
||||
let linkId=new Date().getTime();//本次连接的id
|
||||
let linkId = new Date().getTime(); //本次连接的id
|
||||
let cr = arr.devices.find(c => {
|
||||
if (c.deviceId == deviceId) {
|
||||
c.Linked = true;
|
||||
c.linkId=linkId
|
||||
c.linkId = linkId
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@ -1628,7 +1636,8 @@ class BleHelper {
|
||||
|
||||
if (fIndex > -1) {
|
||||
this.data.LinkedList[fIndex].Linked = true;
|
||||
this.data.LinkedList[fIndex].linkId=linkId;
|
||||
this.data.LinkedList[fIndex].linkId =
|
||||
linkId;
|
||||
} else {
|
||||
this.data.LinkedList.push(cr);
|
||||
}
|
||||
@ -1662,7 +1671,7 @@ class BleHelper {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
resolve(true);
|
||||
|
||||
}).catch((ex) => {
|
||||
@ -1955,17 +1964,17 @@ class BleHelper {
|
||||
return;
|
||||
}
|
||||
// console.log("device=", device);
|
||||
uni.writeBLECharacteristicValue({
|
||||
deviceId: device.deviceId,
|
||||
serviceId: device.writeServiceId,
|
||||
characteristicId: device.wirteCharactId,
|
||||
value: buffer,
|
||||
writeType: 'write',
|
||||
success: () => {
|
||||
console.log("✓ 蓝牙指令发送成功 - deviceId:", device
|
||||
.deviceId);
|
||||
succ();
|
||||
},
|
||||
uni.writeBLECharacteristicValue({
|
||||
deviceId: device.deviceId,
|
||||
serviceId: device.writeServiceId,
|
||||
characteristicId: device.wirteCharactId,
|
||||
value: buffer,
|
||||
writeType: 'write',
|
||||
success: () => {
|
||||
console.log("✓ 蓝牙指令发送成功 - deviceId:", device
|
||||
.deviceId);
|
||||
succ();
|
||||
},
|
||||
fail: (ex) => {
|
||||
ex = this.getError(ex);
|
||||
console.error("✗ 蓝牙指令发送失败 - deviceId:", device
|
||||
@ -2030,7 +2039,7 @@ class BleHelper {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -2048,5 +2057,5 @@ export default {
|
||||
|
||||
return instance;
|
||||
},
|
||||
constService:serviceDic
|
||||
}
|
||||
constService: serviceDic
|
||||
}
|
||||
@ -11,7 +11,8 @@ class BleReceive {
|
||||
'/pages/670/HBY670': this.Receive_670.bind(this),
|
||||
'/pages/4877/BJQ4877': this.Receive_4877.bind(this),
|
||||
'/pages/100/HBY100': this.Receive_100.bind(this),
|
||||
'/pages/102/HBY102': this.Receive_102.bind(this)
|
||||
'/pages/102/HBY102': this.Receive_102.bind(this),
|
||||
'/pages/6170/deviceControl/index':this.Receive_6170.bind(this)
|
||||
};
|
||||
}
|
||||
|
||||
@ -671,6 +672,43 @@ class BleReceive {
|
||||
}
|
||||
|
||||
|
||||
Receive_6170(receive, f, path, recArr) {
|
||||
let receiveData = {};
|
||||
|
||||
try {
|
||||
console.log("订阅消息者:", path);
|
||||
console.log("设备收到消息:", f);
|
||||
console.log("消息内容:", receive);
|
||||
receiveData = JSON.parse(receive.str);
|
||||
|
||||
let recCnt = recArr.find(v => {
|
||||
return v.key.replace(/\//g, "").toLowerCase() == f.device.detailPageUrl
|
||||
.replace(/\//g, "").toLowerCase();
|
||||
});
|
||||
if (!recCnt) {
|
||||
if (receiveData.sta_PowerPercent <= 20) {
|
||||
uni.showModal({
|
||||
title: "提示",
|
||||
content: "设备'" + f.device.deviceName + "'电量低",
|
||||
showCancel: false
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} catch (error) {
|
||||
receiveData = {};
|
||||
console.error("文本解析失败", error)
|
||||
}
|
||||
return receiveData;
|
||||
|
||||
}
|
||||
|
||||
Receive_102(receive, f, path, recArr) {
|
||||
let receiveData = {};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user