蓝牙按工厂要求修改,app隐藏时断开设备蓝牙,已配对设备显示实际连接的设备
# Conflicts: # App.vue
This commit is contained in:
@ -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 => {
|
||||
|
||||
@ -26,7 +26,7 @@
|
||||
|
||||
<view class="list" style="margin-bottom: 30rpx;">
|
||||
|
||||
<view class="item " v-for="item, index in PairEquip" v-show="PairEquip.length>0">
|
||||
<view class="item " @click.stop="disConnect(item,index)" v-for="item, index in PairEquip" v-show="PairEquip.length>0">
|
||||
<view class="leftImg ">
|
||||
<image src="/static/images/common/bluetooth.png" class="titleIco filterNone"
|
||||
mode="heightFix">
|
||||
@ -164,7 +164,8 @@
|
||||
|
||||
},
|
||||
search: '', //筛选
|
||||
PairEquip: [], //本次已配对设备
|
||||
PairEquip: [], //已配对设备
|
||||
tmpLink:[],//本次已配对
|
||||
EquipMents: [], //搜索出来的设备
|
||||
device: null,
|
||||
item: {
|
||||
@ -192,9 +193,9 @@
|
||||
ble.StopSearch();
|
||||
ble.removeAllCallback(pagePath);
|
||||
if (!this.device && !this.Status.navigateTO) {
|
||||
if (this.PairEquip && this.PairEquip.length && this.PairEquip.length > 0) {
|
||||
if (this.tmpLink && this.tmpLink.length && this.tmpLink.length > 0) {
|
||||
console.error("页面卸载时,断开所有连接")
|
||||
let f = this.PairEquip.forEach((v) => {
|
||||
let f = this.tmpLink.forEach((v) => {
|
||||
ble.disconnectDevice(v.deviceId).catch(ex => {
|
||||
console.error("无法断开设备连接", ex);
|
||||
});
|
||||
@ -531,8 +532,7 @@
|
||||
|
||||
time = setTimeout(() => {
|
||||
these.EquipMents = [];
|
||||
console.error("1111111111")
|
||||
these.PairEquip = [];
|
||||
|
||||
|
||||
ble.StartSearch().then(result => {
|
||||
// console.log("开始搜索成功", result);
|
||||
@ -683,7 +683,7 @@
|
||||
|
||||
text: "等待设备上报Mac地址," + these.Status.time + 's'
|
||||
});
|
||||
console.log("11111111", this.Status.time);
|
||||
|
||||
clearInterval(this.Status.intval);
|
||||
this.Status.intval = null;
|
||||
|
||||
@ -745,12 +745,12 @@
|
||||
return;
|
||||
}
|
||||
// console.log("验证设备")
|
||||
// these.DeviceVerdict(item.deviceId);
|
||||
these.DeviceVerdict(item.deviceId);
|
||||
}
|
||||
let execLink = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
|
||||
|
||||
if (index > total) {
|
||||
reject({
|
||||
msg: "连接超时"
|
||||
@ -758,6 +758,7 @@
|
||||
return;
|
||||
}
|
||||
ble.LinkBlue(item.deviceId).then((res) => {
|
||||
this.tmpLink=[item];
|
||||
console.log("连接成功");
|
||||
ble.StopSearch();
|
||||
resolve(res);
|
||||
@ -793,6 +794,22 @@
|
||||
|
||||
|
||||
|
||||
},
|
||||
disConnect:function(item,index){
|
||||
// #ifdef H5|WEB
|
||||
|
||||
this.PairEquip.splice(index,1);
|
||||
|
||||
// #endif
|
||||
// #ifdef APP|APP-PLUS
|
||||
if(ble){
|
||||
ble.disconnectDevice(item.deviceId).catch(ex=>{
|
||||
console.error("无法断开连接",ex);
|
||||
});
|
||||
}
|
||||
// #endif
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -932,7 +949,7 @@
|
||||
position: absolute;
|
||||
top: 240rpx;
|
||||
left: 0rpx;
|
||||
/* border: 1px solid #BBE600; */
|
||||
z-index: 101;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user