100J还差语音文件蓝牙上传
This commit is contained in:
@ -243,7 +243,8 @@
|
||||
deviceUpdateVolume,
|
||||
deviceVoiceBroadcast,
|
||||
updateBleStatus,
|
||||
parseBleData
|
||||
parseBleData,
|
||||
fetchBlePowerStatus
|
||||
} from '@/api/100J/HBY100-J.js'
|
||||
import BleHelper from '@/utils/BleHelper.js';
|
||||
var bleTool = BleHelper.getBleTool();
|
||||
@ -604,17 +605,9 @@
|
||||
console.log(this.activePermissions, 'this.activePermissions');
|
||||
these.fetchDeviceDetail(data.data.deviceId)
|
||||
}
|
||||
// 尝试连接蓝牙
|
||||
// 尝试连接蓝牙:需先扫描获取 BLE deviceId,不能直接用 MAC
|
||||
if (data.data.deviceMac) {
|
||||
// 假设 deviceMac 是蓝牙的 deviceId
|
||||
bleTool.LinkBlue(data.data.deviceMac).then(() => {
|
||||
console.log("100J 蓝牙连接成功");
|
||||
this.bleStateRecovry({
|
||||
deviceId: data.data.deviceMac
|
||||
});
|
||||
}).catch(err => {
|
||||
console.log("100J 蓝牙连接失败,将使用4G", err);
|
||||
});
|
||||
these.tryConnect100JBle(data.data.deviceMac);
|
||||
}
|
||||
});
|
||||
this.createThrottledFunctions();
|
||||
@ -639,6 +632,8 @@
|
||||
bleTool.removeRecoveryCallback("HBY100J");
|
||||
bleTool.removeStateBreakCallback("HBY100J");
|
||||
bleTool.removeStateRecoveryCallback("HBY100J");
|
||||
bleTool.removeDeviceFound("HBY100J_SCAN");
|
||||
bleTool.StopSearch();
|
||||
},
|
||||
onShow() {
|
||||
this.Status.pageHide = false;
|
||||
@ -993,24 +988,124 @@
|
||||
|
||||
deviceRecovry(res) {},
|
||||
deviceDispose(res) {},
|
||||
// 100J 蓝牙连接:先查缓存/尝试直连,失败则扫描(createBLEConnection 需要扫描返回的 deviceId)
|
||||
tryConnect100JBle(deviceMac) {
|
||||
const that = this;
|
||||
const macNorm = (m) => (m || '').replace(/:/g, '').toUpperCase();
|
||||
const targetMacNorm = macNorm(deviceMac);
|
||||
const last6 = targetMacNorm.slice(-6);
|
||||
|
||||
// 1. 查缓存:之前连过且 mac 匹配
|
||||
const cached = bleTool.data.LinkedList.find(v => {
|
||||
const m = macNorm(v.macAddress);
|
||||
return m === targetMacNorm || m.slice(-6) === last6;
|
||||
});
|
||||
if (cached && cached.deviceId) {
|
||||
console.log('[100J] 使用缓存设备连接', cached.deviceId);
|
||||
bleTool.LinkBlue(cached.deviceId).then(() => {
|
||||
console.log('100J 蓝牙连接成功(缓存)');
|
||||
that.bleStateRecovry({ deviceId: cached.deviceId });
|
||||
}).catch(err => {
|
||||
console.log('100J 蓝牙连接失败(缓存),尝试扫描', err);
|
||||
that.connect100JByScan(deviceMac, last6);
|
||||
});
|
||||
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);
|
||||
that.connect100JByScan(deviceMac, last6);
|
||||
});
|
||||
},
|
||||
connect100JByScan(deviceMac, last6) {
|
||||
const that = this;
|
||||
let resolved = false;
|
||||
const timeout = 15000;
|
||||
const timer = setTimeout(() => {
|
||||
if (resolved) return;
|
||||
resolved = true;
|
||||
bleTool.StopSearch();
|
||||
bleTool.removeDeviceFound('HBY100J_SCAN');
|
||||
console.log('100J 蓝牙扫描超时,将使用4G');
|
||||
}, timeout);
|
||||
|
||||
bleTool.addDeviceFound((res) => {
|
||||
if (resolved) return;
|
||||
const devices = res.devices || [];
|
||||
const match = devices.find(d => {
|
||||
const name = (d.name || '').replace(/\r\n/g, '').trim();
|
||||
return name === 'HBY100J' || name.startsWith('LED-') && name.slice(-6) === last6;
|
||||
});
|
||||
if (match) {
|
||||
resolved = true;
|
||||
clearTimeout(timer);
|
||||
bleTool.StopSearch();
|
||||
bleTool.removeDeviceFound('HBY100J_SCAN');
|
||||
console.log('[100J] 扫描到目标设备', match.name, match.deviceId);
|
||||
bleTool.LinkBlue(match.deviceId).then(() => {
|
||||
console.log('100J 蓝牙连接成功(扫描)');
|
||||
that.bleStateRecovry({ deviceId: match.deviceId });
|
||||
}).catch(err => {
|
||||
console.log('100J 蓝牙连接失败,将使用4G', err);
|
||||
});
|
||||
}
|
||||
}, 'HBY100J_SCAN');
|
||||
|
||||
bleTool.StartSearch().then(() => {
|
||||
console.log('[100J] 开始扫描蓝牙,设备名 HBY100J 或 LED-' + last6);
|
||||
}).catch(err => {
|
||||
if (!resolved) {
|
||||
resolved = true;
|
||||
clearTimeout(timer);
|
||||
bleTool.removeDeviceFound('HBY100J_SCAN');
|
||||
console.log('100J 蓝牙扫描启动失败,将使用4G', err);
|
||||
}
|
||||
});
|
||||
},
|
||||
bleStateBreak() {
|
||||
updateBleStatus(false, '', this.deviceInfo.deviceId);
|
||||
},
|
||||
bleStateRecovry(res) {
|
||||
let bleDeviceId = res ? res.deviceId : '';
|
||||
// 蓝牙适配器恢复可用(关闭蓝牙后重新开启):无 deviceId,需主动重连
|
||||
if (!res || !res.deviceId) {
|
||||
const mac = (this.device && this.device.deviceMac) || (this.deviceInfo && this.deviceInfo.deviceMac);
|
||||
if (mac) {
|
||||
console.log('[100J] 蓝牙适配器已恢复,尝试重连', mac);
|
||||
this.tryConnect100JBle(mac);
|
||||
}
|
||||
return;
|
||||
}
|
||||
let bleDeviceId = res.deviceId;
|
||||
updateBleStatus(true, bleDeviceId, this.deviceInfo.deviceId);
|
||||
// 蓝牙连接成功后主动拉取电源状态(电量、续航时间)
|
||||
fetchBlePowerStatus().catch(() => {});
|
||||
},
|
||||
previewImg(img) {},
|
||||
bleValueNotify: function(receive, device, path, recArr) { //订阅消息
|
||||
// 注意:这里 receive.deviceId 是蓝牙的 MAC 地址,而 this.formData.deviceId 是 4G 的 ID
|
||||
// 所以这里需要修改判断逻辑,或者不判断直接解析
|
||||
// 解析蓝牙上报数据 (协议: FC=MAC主动上报, FB=指令响应)
|
||||
if (!receive.bytes || receive.bytes.length < 3) return;
|
||||
const parsedData = parseBleData(receive.bytes);
|
||||
if (!parsedData) return;
|
||||
|
||||
// 尝试解析蓝牙上报的数据
|
||||
if (receive.bytes) {
|
||||
const parsedData = parseBleData(receive.bytes);
|
||||
if (parsedData && parsedData.batteryPercentage !== undefined) {
|
||||
this.deviceInfo.batteryPercentage = parsedData.batteryPercentage;
|
||||
}
|
||||
// 5.1 连接后设备主动上报 MAC 地址 (FC + 6字节 + FF)
|
||||
if (parsedData.type === 'mac' && parsedData.macAddress) {
|
||||
this.formData.macAddress = parsedData.macAddress;
|
||||
this.device.deviceMac = parsedData.macAddress;
|
||||
this.deviceInfo.deviceMac = parsedData.macAddress;
|
||||
return;
|
||||
}
|
||||
|
||||
// 5.5 获取设备电源状态 (0x04)
|
||||
if (parsedData.batteryPercentage !== undefined) {
|
||||
this.deviceInfo.batteryPercentage = parsedData.batteryPercentage;
|
||||
}
|
||||
if (parsedData.batteryRemainingTime !== undefined) {
|
||||
this.deviceInfo.batteryRemainingTime = parsedData.batteryRemainingTime;
|
||||
}
|
||||
|
||||
if (this.deviceInfo.batteryPercentage <= 20) {
|
||||
|
||||
Reference in New Issue
Block a user