670增加去连接蓝牙,其它体验问题小调整
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
import receivTool from "@/utils/BleReceive.js"
|
||||
var recei = null;
|
||||
const serviceDic = [ //合作供应商的蓝牙主服务
|
||||
{
|
||||
{
|
||||
"serviceId": "0000FFE0-0000-1000-8000-00805F9B34FB",
|
||||
"writeId": "0000FFE1-0000-1000-8000-00805F9B34FB",
|
||||
"notifyId": "0000FFE2-0000-1000-8000-00805F9B34FB"
|
||||
@ -11,7 +11,7 @@ const serviceDic = [ //合作供应商的蓝牙主服务
|
||||
"writeId": "0000AE03-0000-1000-8000-00805F9B34FB",
|
||||
"notifyId": "0000AE02-0000-1000-8000-00805F9B34FB"
|
||||
}
|
||||
|
||||
|
||||
];
|
||||
|
||||
class BleHelper {
|
||||
@ -33,17 +33,19 @@ class BleHelper {
|
||||
}
|
||||
if (linkedDevices && linkedDevices.length && linkedDevices.length > 0) {
|
||||
console.log("111111", linkedDevices);
|
||||
linkedDevices.filter((v) => {
|
||||
linkedDevices=linkedDevices.filter((v) => {
|
||||
v.Linked = false;
|
||||
v.notifyState = false;
|
||||
|
||||
return v.device && v.device.id;
|
||||
});
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
this.OpenBlue().then(()=>{
|
||||
this.OpenBlue().then(() => {
|
||||
this.linkAllDevices();
|
||||
});
|
||||
|
||||
|
||||
}, 10);
|
||||
this.data = {
|
||||
isOpenBlue: false, //蓝牙模块是否开启
|
||||
@ -120,7 +122,7 @@ class BleHelper {
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
//从缓存中删除某个设备,bleId蓝牙id,deviceId数据库中的设备id
|
||||
DropDevice(bleId, deviceId) {
|
||||
let flag = false;
|
||||
for (var i = 0; i < this.data.LinkedList.length; i++) {
|
||||
@ -146,6 +148,7 @@ class BleHelper {
|
||||
|
||||
}
|
||||
|
||||
//更新缓存
|
||||
updateCache() {
|
||||
// console.log("this.StorageKey=", this.StorageKey)
|
||||
uni.setStorageSync(this.StorageKey, this.data.LinkedList);
|
||||
@ -155,17 +158,23 @@ class BleHelper {
|
||||
// console.log("模块启动时,自动连接已连接过的设备", this.data.LinkedList);
|
||||
|
||||
if (this.data.LinkedList && this.data.LinkedList.length > 0) {
|
||||
let flag=false;
|
||||
for (var i = 0; i < this.data.LinkedList.length; i++) {
|
||||
let device = this.data.LinkedList[i];
|
||||
// console.log("自动连接绑定过的设备:" + device.deviceId);
|
||||
if (device.macAddress && device.device && device.device.id) {
|
||||
this.LinkBlue(device.deviceId, device.writeServiceId, device.wirteCharactId, device
|
||||
.notifyCharactId);
|
||||
flag=true;
|
||||
}
|
||||
|
||||
}
|
||||
if(!flag){
|
||||
this.updateCache();
|
||||
}
|
||||
} else {
|
||||
// console.log("无设备连接");
|
||||
console.log("蓝牙未连接过任何设备");
|
||||
this.updateCache();
|
||||
}
|
||||
}
|
||||
|
||||
@ -387,7 +396,7 @@ class BleHelper {
|
||||
}
|
||||
uni.getBluetoothAdapterState({
|
||||
success: (info) => {
|
||||
console.log("蓝牙状态获取成功,",info)
|
||||
console.log("蓝牙状态获取成功,", info)
|
||||
this.data.available = info.available;
|
||||
this.data.discovering = info.discovering;
|
||||
resolve(info);
|
||||
@ -461,8 +470,8 @@ class BleHelper {
|
||||
|
||||
if (!res.connected) {
|
||||
|
||||
console.log("蓝牙连接已断开", res);
|
||||
let f=this.data.LinkedList.find((v) => {
|
||||
console.error("蓝牙适配器已不可用", res);
|
||||
let f = this.data.LinkedList.find((v) => {
|
||||
if (v.deviceId == res.deviceId) {
|
||||
v.Linked = false;
|
||||
v.notifyState = false;
|
||||
@ -471,15 +480,19 @@ class BleHelper {
|
||||
return false;
|
||||
});
|
||||
this.updateCache();
|
||||
console.log("尝试5次恢复连接");
|
||||
this.LinkBlue(res.deviceId,f.writeServiceId,f.wirteCharactId,f.notifyCharactId,5)
|
||||
if (f.device && f.device.id) {
|
||||
console.log("尝试5次恢复连接,", f.deviceId);
|
||||
this.LinkBlue(res.deviceId, f.writeServiceId, f
|
||||
.wirteCharactId, f.notifyCharactId, 5)
|
||||
}
|
||||
|
||||
} else {
|
||||
console.log("蓝牙连接已恢复,", res);
|
||||
console.log("蓝牙适配器已恢复,", res);
|
||||
}
|
||||
});
|
||||
|
||||
uni.onBluetoothDeviceFound((devices) => {
|
||||
console.log("发现新设备:" + JSON.stringify(devices));
|
||||
// console.log("发现新设备:" + JSON.stringify(devices));
|
||||
this.data.searchList = this.data.searchList.concat(
|
||||
devices);
|
||||
if (this.cfg.onDeviceFound) {
|
||||
@ -539,20 +552,20 @@ class BleHelper {
|
||||
});
|
||||
this.updateCache();
|
||||
}
|
||||
|
||||
|
||||
if (str.indexOf('imei:') == 0) {
|
||||
let imei = str.split(':')[1];
|
||||
|
||||
|
||||
|
||||
|
||||
this.data.LinkedList.find((v) => {
|
||||
if (v.deviceId == receive
|
||||
.deviceId) {
|
||||
v.imei=imei;
|
||||
v.imei = imei;
|
||||
}
|
||||
});
|
||||
this.updateCache();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (bytes[0] == 0xFC) { //6155以0xFC开头代表mac地址
|
||||
console.log("收到mac地址:", bytes)
|
||||
@ -572,7 +585,7 @@ class BleHelper {
|
||||
}
|
||||
console.log("str=", str);
|
||||
} catch (ex) {
|
||||
console.log("将数据转文本失败", ex);
|
||||
console.error("将数据转文本失败", ex);
|
||||
}
|
||||
let recData = {
|
||||
deviceId: receive.deviceId,
|
||||
@ -589,31 +602,32 @@ class BleHelper {
|
||||
if (this.cfg.receivDataCallback.length > 0) {
|
||||
|
||||
let path = this.getCurrentPagePath();
|
||||
console.log("有人订阅消息")
|
||||
// console.log("有人订阅消息")
|
||||
this.cfg.receivDataCallback.forEach((
|
||||
rec) => {
|
||||
console.log("有人订阅消息111", )
|
||||
// console.log("有人订阅消息111", )
|
||||
if (rec.callback) {
|
||||
try {
|
||||
rec.callback(recData, f,
|
||||
path, this.cfg
|
||||
.receivDataCallback
|
||||
);
|
||||
);
|
||||
} catch (err) {
|
||||
console.log("订阅消息出现异常",
|
||||
console.error(
|
||||
"订阅消息出现异常",
|
||||
err);
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
} else {
|
||||
console.log("无人订阅消息");
|
||||
// console.log("无人订阅消息");
|
||||
}
|
||||
} else {
|
||||
console.log("无人订阅receivDataCallback,不处理数据");
|
||||
// console.log("无人订阅receivDataCallback,不处理数据");
|
||||
}
|
||||
} catch (ex) {
|
||||
console.log("ex=", ex);
|
||||
console.error("处理订阅消息失败,ex=", ex);
|
||||
}
|
||||
|
||||
});
|
||||
@ -638,7 +652,7 @@ class BleHelper {
|
||||
console.log("res=", res)
|
||||
return init();
|
||||
}).then(resolve).catch((ex) => {
|
||||
console.log("22222");
|
||||
console.error("异常:", ex);
|
||||
reject(ex);
|
||||
});
|
||||
|
||||
@ -690,7 +704,7 @@ class BleHelper {
|
||||
let RunSearch = (serviceId) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.startBluetoothDevicesDiscovery({
|
||||
services:serviceId? [serviceId]:[],
|
||||
services: serviceId ? [serviceId] : [],
|
||||
allowDuplicatesKey: true,
|
||||
success: (res) => {
|
||||
//console.log('开始搜索蓝牙设备成功');
|
||||
@ -713,9 +727,9 @@ class BleHelper {
|
||||
// for (let i = 0; i < serviceIds.length; i++) {
|
||||
|
||||
// promises.push(RunSearch(serviceIds[i]));
|
||||
|
||||
|
||||
// }
|
||||
promises.push(RunSearch());
|
||||
promises.push(RunSearch());
|
||||
Promise.all(promises).then(resolve).catch(reject);
|
||||
|
||||
});
|
||||
@ -778,14 +792,29 @@ class BleHelper {
|
||||
|
||||
//订阅消息
|
||||
subScribe(deviceId, state) {
|
||||
console.log("开始订阅消息");
|
||||
console.log("开始订阅消息", state);
|
||||
return new Promise((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
|
||||
|
||||
let c = this.data.LinkedList.find((v) => {
|
||||
return v.deviceId == deviceId;
|
||||
});
|
||||
|
||||
if (!deviceId) {
|
||||
console.error('deviceId无效')
|
||||
reject({
|
||||
code: 404,
|
||||
msg: 'deviceId无效'
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (!c) {
|
||||
console.error("已找不到该设备");
|
||||
reject({
|
||||
code: 404,
|
||||
msg: '已找不到该设备'
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (state) {
|
||||
if (c.notifyState) {
|
||||
resolve();
|
||||
@ -794,8 +823,8 @@ class BleHelper {
|
||||
}
|
||||
|
||||
let startSubScribe = (id, serviceId, characteristicId) => {
|
||||
console.log("serviceId=", serviceId);
|
||||
console.log("characteristicId=", characteristicId);
|
||||
// console.log("serviceId=", serviceId);
|
||||
// console.log("characteristicId=", characteristicId);
|
||||
let p1 = new Promise((succ, err) => {
|
||||
|
||||
uni.notifyBLECharacteristicValueChange({
|
||||
@ -836,6 +865,7 @@ class BleHelper {
|
||||
});
|
||||
return new Promise((succ, err) => {
|
||||
Promise.race([p1, p2]).then(succ).catch(ex => {
|
||||
// console.error("异常了:",ex);
|
||||
if (ex.code == -1) {
|
||||
succ();
|
||||
return;
|
||||
@ -845,8 +875,16 @@ class BleHelper {
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
let serv = serviceDic.find(v => {
|
||||
return v.serviceId == c.writeServiceId;
|
||||
});
|
||||
let promies = new Array();
|
||||
if (c.Characteristics && c.Characteristics.length) {
|
||||
if (c.notifyServiceid && c.notifyCharactId) {
|
||||
promies.push(startSubScribe(c.deviceId, c.notifyServiceid, c.notifyCharactId));
|
||||
} else if (serv) {
|
||||
promies.push(startSubScribe(c.deviceId, serv.serviceId, serv.notifyId));
|
||||
} else if (c.Characteristics && c.Characteristics.length) {
|
||||
for (var i = 0; i < c.Characteristics.length; i++) {
|
||||
let item = c.Characteristics[i];
|
||||
let serviceId = item.serviceId;
|
||||
@ -854,36 +892,42 @@ class BleHelper {
|
||||
|
||||
if (item.properties.notify) {
|
||||
promies.push(startSubScribe(deviceId, serviceId, characteristicId));
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}else{
|
||||
promies.push(startSubScribe(c.deviceId, c.notifyServiceid, c.notifyCharactId));
|
||||
} else {
|
||||
console.log("c=", c);
|
||||
console.log("serv=", serv);
|
||||
reject({
|
||||
code: 403,
|
||||
msg: "出现错误,找不到任何通知特征"
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (promies.length > 0) {
|
||||
|
||||
|
||||
|
||||
Promise.allSettled(promies).then((results) => {
|
||||
|
||||
|
||||
results.forEach((result, index) => {
|
||||
if (result.status === "fulfilled") {
|
||||
// console.log(`操作${index + 1}成功:`, result.value);
|
||||
// console.log(`操作${index + 1}成功:`, result.value);
|
||||
} else {
|
||||
// console.log(`操作${index + 1}失败:`, result.reason
|
||||
// .message);
|
||||
// console.log(`操作${index + 1}失败:`, result.reason
|
||||
// .message);
|
||||
}
|
||||
});
|
||||
// console.log("订阅消息成功");
|
||||
resolve();
|
||||
}).catch((ex) => {
|
||||
console.log("ex=",ex);
|
||||
console.error("异常,ex=", ex);
|
||||
reject(ex);
|
||||
}).finally(() => {
|
||||
// console.log("finally")
|
||||
});
|
||||
} else {
|
||||
console.log("没有特征需要订阅");
|
||||
console.log("没有特征需要订阅");
|
||||
resolve();
|
||||
}
|
||||
|
||||
@ -933,7 +977,7 @@ class BleHelper {
|
||||
});
|
||||
|
||||
if (se) {
|
||||
console.log("合作供应商的",s)
|
||||
console.log("合作供应商的", s)
|
||||
this.data.LinkedList.find((v) => {
|
||||
if (v.deviceId == id) {
|
||||
v.writeServiceId = s.serviceId;
|
||||
@ -983,11 +1027,11 @@ class BleHelper {
|
||||
.then((res) => {
|
||||
console.log('设备连接成功,初始化完成', this.data
|
||||
.LinkedList);
|
||||
|
||||
|
||||
resolve();
|
||||
})
|
||||
.catch(error => {
|
||||
console.log("errrr", error);
|
||||
console.error("errrr=", error);
|
||||
reject(error);
|
||||
});
|
||||
}
|
||||
@ -1200,7 +1244,7 @@ class BleHelper {
|
||||
deviceId: deviceId,
|
||||
mtu: 512,
|
||||
success: (mtu) => {
|
||||
|
||||
|
||||
resolve(true);
|
||||
},
|
||||
fail: () => {
|
||||
@ -1288,7 +1332,7 @@ class BleHelper {
|
||||
resolve();
|
||||
}, 500);
|
||||
}).catch((ex) => {
|
||||
console.log("出现异常", ex);
|
||||
console.error("出现异常", ex);
|
||||
reject(ex);
|
||||
});
|
||||
});
|
||||
@ -1429,7 +1473,7 @@ class BleHelper {
|
||||
}
|
||||
|
||||
Promise.race([timeOut(ms), promise]).then(resolve).catch((ex) => {
|
||||
// console.log("ex=", ex);
|
||||
// console.error("ex=", ex);
|
||||
if (ex.code == -1) {
|
||||
resolve(ex);
|
||||
} else {
|
||||
@ -1469,7 +1513,7 @@ class BleHelper {
|
||||
//console.log("发送成功")
|
||||
resolve();
|
||||
}).catch((ex) => {
|
||||
//console.log("出现异常", ex);
|
||||
console.error("出现异常", ex);
|
||||
reject(this.getError(ex));
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user