1
0
forked from dyf/APP

处理100J设备蓝牙问题

This commit is contained in:
微微一笑
2026-03-19 14:36:17 +08:00
parent ac59e28281
commit 3a9de3078c
4 changed files with 102 additions and 43 deletions

View File

@ -607,9 +607,9 @@
console.log(this.activePermissions, 'this.activePermissions');
these.fetchDeviceDetail(data.data.deviceId)
}
// 尝试连接蓝牙:需先扫描获取 BLE deviceId不能直接用 MAC
// 尝试连接蓝牙:需先扫描获取 BLE deviceId不能直接用 MAC;延迟 500ms 确保蓝牙适配器就绪
if (data.data.deviceMac) {
these.tryConnect100JBle(data.data.deviceMac);
setTimeout(() => { these.tryConnect100JBle(data.data.deviceMac); }, 500);
}
});
this.createThrottledFunctions();
@ -991,21 +991,30 @@
deviceRecovry(res) {},
deviceDispose(res) {},
// 100J 蓝牙连接:先查缓存/尝试直连,失败则扫描createBLEConnection 需要扫描返回的 deviceId
// 100J 蓝牙连接:先查缓存/尝试直连,失败则扫描
// 注意Android 的 createBLEConnection 需要系统返回的 deviceId服务端 MAC(11:22:33:44:55:02) 与 deviceId(02:55:44:33:22:11) 字节序相反
tryConnect100JBle(deviceMac) {
const that = this;
const macNorm = (m) => (m || '').replace(/:/g, '').toUpperCase();
const targetMacNorm = macNorm(deviceMac);
const last6 = targetMacNorm.slice(-6);
// Android BLE deviceId 多为 MAC 字节反序,如 11:22:33:44:55:02 -> 02:55:44:33:22:11
const macToDeviceId = (mac) => {
const parts = (mac || '').split(':').filter(Boolean);
return parts.length === 6 ? parts.reverse().join(':') : mac;
};
// 1. 查缓存:之前连过且 mac 匹配
const cached = bleTool.data.LinkedList.find(v => {
const m = macNorm(v.macAddress);
return m === targetMacNorm || m.slice(-6) === last6;
});
const SVC = '0000AE30-0000-1000-8000-00805F9B34FB';
const WRITE = '0000AE03-0000-1000-8000-00805F9B34FB';
const NOTIFY = '0000AE02-0000-1000-8000-00805F9B34FB';
if (cached && cached.deviceId) {
console.log('[100J] 使用缓存设备连接', cached.deviceId);
bleTool.LinkBlue(cached.deviceId).then(() => {
bleTool.LinkBlue(cached.deviceId, SVC, WRITE, NOTIFY, 2).then(() => {
console.log('100J 蓝牙连接成功(缓存)');
that.bleStateRecovry({ deviceId: cached.deviceId });
}).catch(err => {
@ -1015,18 +1024,29 @@
return;
}
// 2. 无缓存:先尝试直连Android 上 deviceId 可能为 MAC
console.log('[100J] 尝试直连', deviceMac);
bleTool.LinkBlue(deviceMac).then(() => {
console.log('100J 蓝牙连接成功(直连)');
that.bleStateRecovry({ deviceId: deviceMac });
}).catch(err => {
console.log('100J 蓝牙直连失败,开始扫描', err);
// 2. 无缓存:先尝试直连Android 上 deviceId 为 MAC 反序(11:22:33:44:55:02->02:55:44:33:22:11)
const tryDirect = (id) => bleTool.LinkBlue(id, SVC, WRITE, NOTIFY, 2).then(() => {
console.log('100J 蓝牙连接成功(直连)', id);
that.bleStateRecovry({ deviceId: id });
});
const deviceIdReversed = macToDeviceId(deviceMac);
console.log('[100J] 尝试直连', deviceIdReversed, '(MAC反序)');
tryDirect(deviceIdReversed).catch(() => {
if (deviceIdReversed !== deviceMac) {
console.log('[100J] 反序直连失败,尝试原 MAC', deviceMac);
return tryDirect(deviceMac);
}
return Promise.reject();
}).catch(() => {
console.log('[100J] 蓝牙直连失败,开始扫描');
that.connect100JByScan(deviceMac, last6);
});
},
connect100JByScan(deviceMac, last6) {
const that = this;
const SVC = '0000AE30-0000-1000-8000-00805F9B34FB';
const WRITE = '0000AE03-0000-1000-8000-00805F9B34FB';
const NOTIFY = '0000AE02-0000-1000-8000-00805F9B34FB';
let resolved = false;
const timeout = 15000;
const timer = setTimeout(() => {
@ -1050,7 +1070,7 @@
bleTool.StopSearch();
bleTool.removeDeviceFound('HBY100J_SCAN');
console.log('[100J] 扫描到目标设备', match.name, match.deviceId);
bleTool.LinkBlue(match.deviceId).then(() => {
bleTool.LinkBlue(match.deviceId, SVC, WRITE, NOTIFY, 2).then(() => {
console.log('100J 蓝牙连接成功(扫描)');
that.bleStateRecovry({ deviceId: match.deviceId });
}).catch(err => {