修改App图标、名称、蓝牙加入断线重连

This commit is contained in:
liub
2025-09-16 11:37:06 +08:00
parent 52b3bd796c
commit 778594574b
14 changed files with 3589 additions and 3686 deletions

View File

@ -40,8 +40,8 @@ class BleHelper {
onDeviceFound: [], //发现新设备的事件
receivDataCallback: [] //接收到数据的事件
}
this.addReceiveCallback((a,b,c)=>{
recei.ReceiveData(a,b,c);
this.addReceiveCallback((a, b, c) => {
recei.ReceiveData(a, b, c);
}, "BleReceiveData");
this.dic = {
errRemarks: [{
@ -105,14 +105,17 @@ class BleHelper {
}
linkAllDevices() {
// console.log("模块启动时,自动连接已连接过的设备", this.data.LinkedList);
console.log("模块启动时,自动连接已连接过的设备", this.data.LinkedList);
if (this.data.LinkedList && this.data.LinkedList.length > 0) {
for (var i = 0; i < this.data.LinkedList.length; i++) {
let device = this.data.LinkedList[i];
// console.log("自动连接:" + device.deviceId);
this.LinkBlue(device.deviceId, device.writeServiceId, device.wirteCharactId, device
.notifyCharactId);
// console.log("自动连接绑定过的设备:" + device.deviceId);
if(device.macAddress && device.device && device.device.id){
this.LinkBlue(device.deviceId, device.writeServiceId, device.wirteCharactId, device
.notifyCharactId);
}
}
} else {
// console.log("无设备连接");
@ -335,8 +338,8 @@ class BleHelper {
}
uni.getBluetoothAdapterState({
success: (info) => {
this.data.available=info.available;
this.data.discovering=info.discovering;
this.data.available = info.available;
this.data.discovering = info.discovering;
resolve(info);
},
fail: (ex1) => {
@ -360,12 +363,12 @@ class BleHelper {
resolve(false);
return;
}
uni.openBluetoothAdapter({
success: (args) => {
// console.log("蓝牙初始化成功:" + JSON.stringify(args));
this.data.isOpenBlue = true;
this.data.available=true;
this.data.available = true;
resolve(true);
if (this.data.isSubscribe) { //整个App生命周期只订阅一次
@ -386,7 +389,7 @@ class BleHelper {
this.data.discovering = state.discovering;
if (this.data.available && this.data
.isOpenBlue) { //蓝牙状态再次可用,重连所有设备
this.linkAllDevices();
}
@ -437,9 +440,9 @@ class BleHelper {
uni.onBLECharacteristicValueChange((receive) => {
//订阅消息
console.log("收到订阅消息",receive);
let f=this.data.LinkedList.find((v) => {
return v.deviceId == receive.deviceId;
console.log("收到订阅消息", receive);
let f = this.data.LinkedList.find((v) => {
return v.deviceId == receive.deviceId;
})
let dataView = new DataView(receive.value);
@ -458,7 +461,7 @@ class BleHelper {
// 将每个字节转换为对应的字符
str += String.fromCharCode(uint8Array[i]);
}
let header = "mac address:";
if (str.indexOf(header) == 0) { //650以文本传输mac
@ -467,7 +470,7 @@ class BleHelper {
.deviceId) {
v.macAddress = str.replace(
header, "");
console.log("收到mac地址:", str)
console.log("收到mac地址:", str)
}
});
uni.setStorageSync(this.StorageKey, this.data
@ -475,9 +478,9 @@ class BleHelper {
}
if (bytes[0] == 0xFC) { //6155以0xFC开头代表mac地址
console.log("收到mac地址:", bytes)
console.log("收到mac地址:", bytes)
if (bytes.length >= 7) {
console.log("hexs=",hexs);
console.log("hexs=", hexs);
let mac = hexs.slice(1, 7).join(":")
.toUpperCase();
this.data.LinkedList.find((v) => {
@ -491,7 +494,7 @@ class BleHelper {
.data.LinkedList);
}
}
console.log("str=",str);
console.log("str=", str);
} catch (ex) {
console.log("将数据转文本失败", ex);
}
@ -503,18 +506,18 @@ class BleHelper {
str: str,
hexs: hexs
};
console.log("监听到特征值:" + JSON.stringify(recData));
console.log("监听到特征值:" + JSON.stringify(recData));
if (this.cfg.receivDataCallback) {
if (this.cfg.receivDataCallback.length > 0) {
let path = this.getCurrentPagePath();
this.cfg.receivDataCallback.forEach((rec) => {
if (rec.callback) {
rec.callback(recData, f, path);
}
}
})
}
} else {
@ -672,7 +675,8 @@ class BleHelper {
}
// console.log("c=", c);
let startSubScribe = (id, serviceId, characteristicId) => {
console.log("serviceId=",serviceId);
console.log("characteristicId=",characteristicId);
return new Promise((succ, err) => {
uni.notifyBLECharacteristicValueChange({
deviceId: id,
@ -681,7 +685,7 @@ class BleHelper {
state: state,
success: (res) => {
if (state) {
// console.log("订阅消息成功", res);
console.log("订阅消息成功", res);
} else {
console.log("取消订阅成功", res);
}
@ -701,24 +705,27 @@ class BleHelper {
});
}
let promies = new Array();
for (var i = 0; i < c.Characteristics.length; i++) {
let item = c.Characteristics[i];
let serviceId = item.serviceId;
let characteristicId = item.uuid;
if (item.properties.notify) {
promies.push(startSubScribe(deviceId, serviceId, characteristicId));
if(c.Characteristics && c.Characteristics.length){
for (var i = 0; i < c.Characteristics.length; i++) {
let item = c.Characteristics[i];
let serviceId = item.serviceId;
let characteristicId = item.uuid;
if (item.properties.notify) {
promies.push(startSubScribe(deviceId, serviceId, characteristicId));
}
}
}
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);
}
});
@ -732,7 +739,7 @@ class BleHelper {
}, 20);
}, 800);
});
}
@ -939,79 +946,90 @@ class BleHelper {
//连接设备
var linkDevice = () => {
return new Promise((resolve, reject) => {
if (fIndex > -1 && f?.Linked) {
console.log("当前已连接,跳过其他步骤")
resolve(false);
return;
}
if(!this.data.available){
reject(this.getError({code:10001}));
return;
}
console.log("正在连接" + deviceId);
uni.createBLEConnection({
deviceId: deviceId,
timeout: 30000,
success: (info) => {
console.log("新连接成功", this.data.LinkedList);
this.getLinkBlue().then((arr) => {
let cr = arr.devices.find(c => {
if (c.deviceId == deviceId) {
c.Linked = true;
return true;
}
return false;
});
if (fIndex > -1) {
this.data.LinkedList[fIndex].Linked = true;
} else {
this.data.LinkedList.push(cr);
}
uni.setStorageSync(this.StorageKey, this.data
.LinkedList);
let os = plus.os.name;
console.log("os=",os);
if (os == 'Android') {
uni.setBLEMTU({
deviceId: deviceId,
mtu: 512,
success: (mtu) => {
console.log("mtu设置成功",mtu);
resolve(true);
},
fail: () => {
console.log("mtu设置失败")
resolve(true);
},
finally:()=>{
}
});
}else{
resolve(true);
}
}).catch((ex) => {
reject(this.getError(ex));
});
},
fail: (ex) => {
ex = this.getError(ex)
console.log("蓝牙连接失败" + JSON.stringify(ex));
reject(ex);
}
});
});
}
// 添加重试次数限制
let retryCount = 0;
const maxRetries = 10; // 最大重试次数
const connect = () => {
return new Promise((resolve, reject) => {
if (fIndex > -1 && f?.Linked) {
console.log("当前已连接,跳过其他步骤");
resolve(false);
return;
}
if (!this.data.available) {
reject(this.getError({ code: 10001 }));
return;
}
console.log("正在连接" + deviceId);
uni.createBLEConnection({
deviceId: deviceId,
timeout: 30000,
success: (info) => {
console.log("新连接成功", this.data.LinkedList);
this.getLinkBlue().then((arr) => {
let cr = arr.devices.find(c => {
if (c.deviceId == deviceId) {
c.Linked = true;
return true;
}
return false;
});
if (fIndex > -1) {
this.data.LinkedList[fIndex].Linked = true;
} else {
this.data.LinkedList.push(cr);
}
uni.setStorageSync(this.StorageKey, this.data.LinkedList);
console.log("LinkedList=", this.data.LinkedList);
// 处理 MTU 设置
if (plus.os.name === 'Android') {
uni.setBLEMTU({
deviceId: deviceId,
mtu: 512,
success: (mtu) => {
console.log("mtu设置成功", mtu);
resolve(true);
},
fail: () => {
console.log("mtu设置失败");
resolve(true); // MTU设置失败不影响连接成功
}
});
} else {
resolve(true);
}
}).catch((ex) => {
reject(this.getError(ex));
});
},
fail: (ex) => {
ex = this.getError(ex);
console.log("蓝牙连接失败" + JSON.stringify(ex));
// 连接超时后自动重试
if (ex.code === 10012 && retryCount < maxRetries) {
retryCount++;
console.log(`重试连接 (${retryCount}/${maxRetries})`);
// 使用 setTimeout 避免递归调用栈溢出
setTimeout(() => {
connect().then(resolve).catch(reject);
}, 1000); // 延迟1秒后重试
} else {
reject(ex);
}
}
});
});
};
return connect();
};
@ -1021,19 +1039,32 @@ class BleHelper {
////console.log("2222222");
return linkDevice(deviceId);
}).then((res) => {
////console.log("11111111");
if (res) { //新连接
console.log("11111111");
if (fIndex == -1) {
console.log("开始获取服务", targetServiceId)
return this.getService(deviceId, targetServiceId, writeCharId,
notifyCharId); //获取服务
notifyCharId); //获取服务
} else {
if (!f.notifyState) {
this.subScribe(deviceId, true);
if(f.wirteCharactId && f.notifyCharactId){
if (!f.notifyState) {
console.log("开始订阅特征");
this.subScribe(deviceId, true);
}else{
console.log("不订阅消息");
}
return Promise.resolve(true);
}else{
console.log("开始获取服务", targetServiceId)
return this.getService(deviceId, targetServiceId, writeCharId,
notifyCharId);
}
return Promise.resolve(true);
}
} else { //已连接过,直接订阅消息
} else { //已连接过,直接订阅消息
console.log("11111111");
if (fIndex > -1 && f && !f.notifyState) {
this.subScribe(deviceId, true);
@ -1157,7 +1188,7 @@ class BleHelper {
})); //没有找到指定设备
return;
}
console.log("device=", device);
console.log("device=", device);
uni.writeBLECharacteristicValue({
deviceId: device.deviceId,
serviceId: device.writeServiceId,
@ -1252,7 +1283,7 @@ class BleHelper {
let a = pixels[i + 3];
if (type == 'bgr') {
result[index++] = ((b & 0xF8) << 8) | ((g & 0xFC) << 3) | (r >> 3);
result[index++] = ((b & 0xF8) << 8) | ((g & 0xFC) << 3) | (r >> 3);
} else {
result[index++] = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
}