蓝牙增加断开重连、换设备后可以去再次绑定

This commit is contained in:
liub
2025-09-19 12:01:06 +08:00
parent ff5e363ccc
commit 482a47bd4f
6 changed files with 394 additions and 186 deletions

View File

@ -181,7 +181,8 @@
visiblePrompt: false, visiblePrompt: false,
promptTitle: '设备名称', promptTitle: '设备名称',
modelValue: '', modelValue: '',
visibleClose: false visibleClose: false,
okCallback:null
}, },
BottomMenu: { BottomMenu: {
show: false, show: false,

View File

@ -200,7 +200,8 @@
visiblePrompt: false, visiblePrompt: false,
promptTitle: '设备名称', promptTitle: '设备名称',
modelValue: '', modelValue: '',
visibleClose: false visibleClose: false,
okCallback:null
}, },
BottomMenu: { BottomMenu: {
show: false, show: false,
@ -393,7 +394,7 @@
return className; return className;
}, },
bleValueNotify: function(receive, device, path) { //订阅消息 bleValueNotify: function(receive, device, path) { //订阅消息
console.log("收到设备的数据", data) console.log("收到设备的数据", receive)
let data = recei.ReceiveData(receive, device, pagePath); let data = recei.ReceiveData(receive, device, pagePath);
if (data) { if (data) {
@ -1144,9 +1145,12 @@
if (this.Status.Pop.clickEvt == 'SendUsr') { if (this.Status.Pop.clickEvt == 'SendUsr') {
} }
console.log("1111");
this.Status.Pop.showPop = false; this.Status.Pop.showPop = false;
if(this.Status.Pop.okCallback){
this.Status.Pop.okCallback();
}
}, },
showPop: function(option) { showPop: function(option) {

View File

@ -181,7 +181,8 @@
visiblePrompt: false, visiblePrompt: false,
promptTitle: '设备名称', promptTitle: '设备名称',
modelValue: '', modelValue: '',
visibleClose: false visibleClose: false,
okCallback:null
}, },
BottomMenu: { BottomMenu: {
show: false, show: false,

View File

@ -8,6 +8,9 @@
<view class="deviceName"> <view class="deviceName">
蓝牙名:{{device.name}} 蓝牙名:{{device.name}}
</view> </view>
<view class="deviceName">
状态:{{deviceStatu}}
</view>
<view class="deviceName"> <view class="deviceName">
设备名:{{device.deviceName}} 设备名:{{device.deviceName}}
</view> </view>
@ -40,12 +43,14 @@
var these = null; var these = null;
var eventChannel = null; var eventChannel = null;
var ble = null; var ble = null;
var timeInteval = null;
export default { export default {
data() { data() {
return { return {
Statu: { Statu: {
bound: null bound: null,
timeInteval:null,
isSearch:false
}, },
device: { device: {
"deviceId": "", "deviceId": "",
@ -57,7 +62,8 @@
], ],
"linkStatu": false, "linkStatu": false,
"macAddress": "" "macAddress": "",
"communicationMode":""
}, },
serverDevice:null serverDevice:null
} }
@ -72,15 +78,36 @@
} else { } else {
return "red"; return "red";
} }
},
deviceStatu:function(){
if(!this.device.name){
return "";
}
if(!this.device.macAddress){
return '等待设备上报Mac地址';
}
if(!this.device.deviceName ){
if(this.Statu.isSearch){
return "无效设备";
}else{
if(this.Statu.timeInteval===null){
return "等待查询设备"
}else{
return "正在查询设备";
}
}
}else{
return "找到有效设备"
}
return "";
} }
}, },
onBackPress() {
console.log("返回时断开蓝牙连接,取消订阅");
ble.disconnectDevice(these.device.deviceId);
ble.removeReceiveCallback(pagePath);
},
onUnload() { onUnload() {
console.log("返回时断开蓝牙连接,取消订阅"); console.log("返回时断开蓝牙连接,取消订阅");
ble.disconnectDevice(these.device.deviceId);
ble.removeReceiveCallback(pagePath); ble.removeReceiveCallback(pagePath);
}, },
onLoad(option) { onLoad(option) {
@ -96,6 +123,7 @@
if (f && f.macAddress) { if (f && f.macAddress) {
these.device.macAddress = f.macAddress; these.device.macAddress = f.macAddress;
console.log("222222"); console.log("222222");
these.Statu.isSearch=false;
these.initDevice(); these.initDevice();
} }
} }
@ -112,9 +140,9 @@
keys.forEach((v, index) => { keys.forEach((v, index) => {
these.device[v] = f[v]; these.device[v] = f[v];
}) })
console.log("LinkedList=", ble.data.LinkedList) // console.log("LinkedList=", ble.data.LinkedList)
console.log("f=", f); // console.log("f=", f);
console.log("获取到设备", these.device); // console.log("获取到设备", these.device);
if (f.macAddress) { if (f.macAddress) {
these.device.macAddress = f.macAddress; these.device.macAddress = f.macAddress;
@ -130,9 +158,10 @@
methods: { methods: {
initDevice: function() { initDevice: function() {
clearTimeout(timeInteval); clearTimeout(this.Statu.timeInteval);
timeInteval = setTimeout(() => { this.Statu.timeInteval = setTimeout(() => {
this.Statu.isSearch=true;
showLoading(these, { showLoading(these, {
text: '正在获取设备信息' text: '正在获取设备信息'
}); });
@ -144,6 +173,7 @@
deviceMac: these.device.macAddress deviceMac: these.device.macAddress
} }
}).then(res => { }).then(res => {
console.log("获取设备信息", res); console.log("获取设备信息", res);
if (res && res.code == 200) { if (res && res.code == 200) {
let data = res.data; let data = res.data;
@ -180,6 +210,8 @@
}).catch((ex) => { }).catch((ex) => {
console.log("获取设备出现异常:", ex); console.log("获取设备出现异常:", ex);
}).finally(() => { }).finally(() => {
this.Statu.timeInteval=null;
this.Statu.isSearch=false;
hideLoading(these); hideLoading(these);
}); });
@ -202,11 +234,16 @@
these.Statu.boundRemark = "设备上报Mac地址异常"; these.Statu.boundRemark = "设备上报Mac地址异常";
return; return;
} }
if(!this.device.deviceName){
these.Statu.bound = false;
these.Statu.boundRemark = "设备未入库";
return;
}
these.Statu.bound = null; these.Statu.bound = null;
these.Statu.boundRemark = ""; these.Statu.boundRemark = "";
showLoading(these, { showLoading(these, {
text: "连接中..." text: "绑定中..."
}) })
let promise = request({ let promise = request({
url: '/app/device/bind', url: '/app/device/bind',

View File

@ -10,7 +10,7 @@
<view class="imgContent center"> <view class="imgContent center">
<view class="img center"> <view class="img center">
<image src="/static/images/bluetooth.png" class="titleIco" mode="aspectFit"> <image src="/static/images/common/bluetooth.png" class="titleIco" mode="aspectFit">
</image> </image>
</view> </view>
@ -27,7 +27,7 @@
<view class="item " v-for="item, index in PairEquip" v-show="PairEquip.length>0"> <view class="item " v-for="item, index in PairEquip" v-show="PairEquip.length>0">
<view class="leftImg "> <view class="leftImg ">
<image src="/static/images/BLEAdd/device.png" class="titleIco" mode="aspectFit"> <image src="/static/images/common/bluetooth.png" class="titleIco" mode="heightFix">
</image> </image>
</view> </view>
<view class="centertxt "> <view class="centertxt ">
@ -49,7 +49,7 @@
<view class="item" v-on:click="Link(item,index)" v-for="item, index in EquipMents" <view class="item" v-on:click="Link(item,index)" v-for="item, index in EquipMents"
v-show="!item['linkStatu']"> v-show="!item['linkStatu']">
<view class="leftImg "> <view class="leftImg ">
<image src="/static/images/BLEAdd/device.png" class="titleIco" mode="aspectFit"> <image src="/static/images/common/bluetooth.png" class="titleIco" mode="heightFix">
</image> </image>
</view> </view>
<view class="centertxt "> <view class="centertxt ">
@ -82,7 +82,7 @@
</view> </view>
</BottomSlideMenuPlus> </BottomSlideMenuPlus>
<global-loading ref="loading" /> <global-loading ref="loading" />
</view> </view>
</template> </template>
@ -94,10 +94,10 @@
hideLoading, hideLoading,
updateLoading updateLoading
} from '@/utils/loading.js' } from '@/utils/loading.js'
const pagePath="pages/common/addBLE/addEquip"; const pagePath = "pages/common/addBLE/addEquip";
var ble = null; var ble = null;
var these = null; var these = null;
var eventChannel=null; var eventChannel = null;
export default { export default {
data() { data() {
return { return {
@ -130,9 +130,9 @@
}, },
PairEquip: [], //已配对设备 PairEquip: [], //已配对设备
EquipMents: [], //搜索出来的设备 EquipMents: [], //搜索出来的设备
device:null, device: null,
item:{ item: {
deviceId:'' deviceId: ''
} }
} }
}, },
@ -143,18 +143,15 @@
ble.StopSearch(); ble.StopSearch();
}, },
onBackPress: (e) => {
ble.StopSearch(); onUnload() {
ble.removeDeviceFound(pagePath);
ble.removeReceiveCallback(pagePath);
},
onUnload(){
ble.StopSearch(); ble.StopSearch();
ble.removeDeviceFound(pagePath); ble.removeDeviceFound(pagePath);
ble.removeReceiveCallback(pagePath); ble.removeReceiveCallback(pagePath);
}, },
onLoad() { onLoad() {
these = this; these = this;
this.EquipMents = [];
ble = bleTool.getBleTool(); ble = bleTool.getBleTool();
ble.addDeviceFound((arr) => { ble.addDeviceFound((arr) => {
@ -162,7 +159,7 @@
for (var i = 0; i < arr.length; i++) { for (var i = 0; i < arr.length; i++) {
arr[i].linkStatu = false; arr[i].linkStatu = false;
if(!arr[i].name){ if (!arr[i].name) {
continue; continue;
} }
let f = these.EquipMents.find(function(v) { let f = these.EquipMents.find(function(v) {
@ -176,31 +173,36 @@
} }
} }
console.log("equip=", these.EquipMents)
}, pagePath); }, pagePath);
// console.log("addEquip") // console.log("addEquip")
// ble.addReceiveCallback((receivData,f,path,arr) => { ble.addReceiveCallback((receivData, f, path, arr) => {
// console.log("收到数据了:", receivData);//数据格式:{bytes:[109,97],str:"",hexs:"FA 01"} if (f.macAddress) {
// if (this.item.deviceId == receivData.deviceId) { showLoading(these, {
// console.log("11111:",receivData); text: '正在验证设备'
});
setTimeout(() => {
these.DeviceVerdict(f.deviceId);
}, 0);
}
// } }, pagePath);
// });
eventChannel = this.getOpenerEventChannel(); eventChannel = this.getOpenerEventChannel();
eventChannel.on('detailData', function(rec) { eventChannel.on('detailData', function(rec) {
console.log("接收到父页面的参数:",rec); console.log("接收到父页面的参数:", rec);
these.device=rec.data; these.device = rec.data;
}); });
}, },
onShow: function() { onShow: function() {
this.EquipMents=[]; this.EquipMents = [];
this.PairEquip=[]; this.PairEquip = [];
ble.StartSearch().catch((ex) => { ble && ble.StartSearch().catch((ex) => {
if (ex.code == 10001) { if (ex.code == 10001) {
these.showOpenSetting(); these.showOpenSetting();
} }
@ -238,57 +240,121 @@
this.Status.BottomMenu.show = false; this.Status.BottomMenu.show = false;
ble.showBlueSetting(false); ble.showBlueSetting(false);
}, },
Link: function(item, index) { DeviceVerdict(deviceId) { //判断是否是目标设备
this.item.deviceId=item.deviceId; if (these.device) { //从设备详情过来的,回设备详情去
showLoading(this,{ let f = ble.data.LinkedList.find(v => {
text: "正在连接" if (v.deviceId == deviceId) {
}); v.device = these.device;
setTimeout(() => { return true;
let serviceid=null;
if(item.advertisServiceUUIDs.length>0){
serviceid=item.advertisServiceUUIDs[0];
}
ble.LinkBlue(item.deviceId,serviceid).then((res) => {
let c = these.PairEquip.find(function(v) {
return v.deviceId == item.deviceId;
});
if (!c) {
these.PairEquip.push(item);
} }
console.log("连接成功"); return false;
if(these.device){//从设备详情过来的,回设备详情去 });
ble.data.LinkedList.find(v=>{ if (f.macAddress) {
if(v.deviceId==item.deviceId){
v.device=these.device;
return true; if (f.macAddress != these.device.deviceMac) {
} ble.disconnectDevice(deviceId);
return false; updateLoading(these, {
}); text: "设备Mac地址错误,请重选设备连接"
ble.updateCache(); })
uni.navigateBack();
return; return;
} }
hideLoading(these);
ble.updateCache();
uni.navigateBack();
return true;
} else {
updateLoading(these, {
text: "等待设备上报Mac地址"
})
return undefined;
}
}
return false;
},
Link: function(item) {
this.item.deviceId = item.deviceId;
showLoading(this, {
text: "正在连接:第1次"
});
let index = 1;
let total = 5;
let linkCallback = (res) => {
let c = these.PairEquip.find(function(v) {
return v.deviceId == item.deviceId;
});
if (!c) {
these.PairEquip.push(item);
}
console.log("连接成功", these.device);
if (!these.device) {
console.log("跳转到绑定")
hideLoading(these);
uni.navigateTo({ uni.navigateTo({
url:"/pages/common/addBLE/LinkBle", url: "/pages/common/addBLE/LinkBle",
events:{ events: {
}, },
success(res) { success(res) {
res.eventChannel.emit('LinkItem', item); res.eventChannel.emit('LinkItem', item);
} }
}); });
return;
}
console.log("验证设备")
these.DeviceVerdict(item.deviceId);
}
let execLink = () => {
return new Promise((resolve, reject) => {
if (index > total) {
reject({
msg: "连接超时"
});
return;
}
ble.LinkBlue(item.deviceId).then((res) => {
console.log("连接成功");
resolve(res);
}).catch((ex) => {
if (index == total) {
console.log("连接了N次都没连上");
reject(ex);
return;
}
index++;
updateLoading(this, {
text: ex.msg + ",正在重试第" + index + "次"
})
execLink().then(resolve).catch(reject);
})
}).catch((ex) => {
console.log("ex=",ex)
uni.showModal({
content:"连接失败:"+ex.msg
});
}).finally(()=>{
hideLoading(this);
}); });
}, 0);
}
execLink().then((res) => {
linkCallback(res);
}).catch(ex => {
console.log("ex=", ex)
uni.showModal({
content: "连接失败:" + ex.msg
});
hideLoading(these);
});
} }
@ -463,8 +529,8 @@
} }
.list .item .leftImg { .list .item .leftImg {
width: 100rpx; width: 60rpx;
height: 70rpx; height: 60rpx;
} }
.list .item .centertxt { .list .item .centertxt {
@ -496,7 +562,7 @@
font-family: "PingFang SC"; font-family: "PingFang SC";
font-size: 26rpx; font-size: 26rpx;
font-weight: 400; font-weight: 400;
line-height: 50rpx; line-height: 36rpx;
text-align: left; text-align: left;
} }
@ -505,14 +571,14 @@
font-family: "PingFang SC"; font-family: "PingFang SC";
font-size: 24rpx; font-size: 24rpx;
font-weight: 400; font-weight: 400;
line-height: 30rpx; line-height: 36rpx;
text-align: left; text-align: left;
} }
.list .item .rightIco .img { .list .item .rightIco .img {
width: 50rpx; width: 45rpx;
height: 50rpx; height: 45rpx;
} }
.openBlue { .openBlue {

View File

@ -1,5 +1,18 @@
import receivTool from "@/utils/BleReceive.js" import receivTool from "@/utils/BleReceive.js"
var recei = null; var recei = null;
const serviceDic = [ //合作供应商的蓝牙主服务
{
"serviceId": "0000AE30-0000-1000-8000-00805F9B34FB",
"writeId": "0000AE03-0000-1000-8000-00805F9B34FB",
"notifyId": "0000AE02-0000-1000-8000-00805F9B34FB"
},
{
"serviceId": "0000FFE0-0000-1000-8000-00805F9B34FB",
"writeId": "0000FFE1-0000-1000-8000-00805F9B34FB",
"notifyId": "0000FFE2-0000-1000-8000-00805F9B34FB"
}
];
class BleHelper { class BleHelper {
constructor() { constructor() {
this.StorageKey = "linkedDevices"; this.StorageKey = "linkedDevices";
@ -130,7 +143,7 @@ class BleHelper {
} }
updateCache() { updateCache() {
console.log("this.StorageKey=", this.StorageKey) // console.log("this.StorageKey=", this.StorageKey)
uni.setStorageSync(this.StorageKey, this.data.LinkedList); uni.setStorageSync(this.StorageKey, this.data.LinkedList);
} }
@ -414,18 +427,18 @@ class BleHelper {
} }
uni.onBluetoothAdapterStateChange((state) => { uni.onBluetoothAdapterStateChange((state) => {
console.log('蓝牙状态发生变化:' + JSON.stringify(state)); // console.log('蓝牙状态发生变化:' + JSON.stringify(state));
if(this.data.available==state.available){ if (this.data.available !== state.available) {
return; this.data.available = state.available;
} this.data.discovering = state.discovering;
this.data.available = state.available; if (this.data.available && this.data
this.data.discovering = state.discovering; .isOpenBlue) { //蓝牙状态再次可用,重连所有设备
if (this.data.available && this.data this.linkAllDevices();
.isOpenBlue) { //蓝牙状态再次可用,重连所有设备
this.linkAllDevices();
}
} }
if (!state.available) { //蓝牙状态不可用了,将所有设备标记为断开连接 if (!state.available) { //蓝牙状态不可用了,将所有设备标记为断开连接
this.data.LinkedList.filter((v) => { this.data.LinkedList.filter((v) => {
v.Linked = false; v.Linked = false;
@ -442,7 +455,7 @@ class BleHelper {
if (!res.connected) { if (!res.connected) {
console.log("蓝牙连接已断开", res); console.log("蓝牙连接已断开", res);
this.data.LinkedList.find((v) => { let f=this.data.LinkedList.find((v) => {
if (v.deviceId == res.deviceId) { if (v.deviceId == res.deviceId) {
v.Linked = false; v.Linked = false;
v.notifyState = false; v.notifyState = false;
@ -451,13 +464,15 @@ class BleHelper {
return false; return false;
}); });
this.updateCache(); this.updateCache();
console.log("尝试5次恢复连接");
this.LinkBlue(res.deviceId,f.writeServiceId,f.wirteCharactId,f.notifyCharactId,5)
} else { } else {
// console.log("蓝牙连接已恢复,", res); console.log("蓝牙连接已恢复,", res);
} }
}); });
uni.onBluetoothDeviceFound((devices) => { uni.onBluetoothDeviceFound((devices) => {
// ////console.log("发现新设备:" + JSON.stringify(devices)); // console.log("发现新设备:" + JSON.stringify(devices));
this.data.searchList = this.data.searchList.concat( this.data.searchList = this.data.searchList.concat(
devices); devices);
if (this.cfg.onDeviceFound) { if (this.cfg.onDeviceFound) {
@ -556,14 +571,16 @@ class BleHelper {
console.log("有人订阅消息") console.log("有人订阅消息")
this.cfg.receivDataCallback.forEach(( this.cfg.receivDataCallback.forEach((
rec) => { rec) => {
console.log("有人订阅消息111",) console.log("有人订阅消息111", )
if (rec.callback) { if (rec.callback) {
try{ try {
rec.callback(recData, f, rec.callback(recData, f,
path, this.cfg path, this.cfg
.receivDataCallback); .receivDataCallback
}catch(err){ );
console.log("订阅消息出现异常",err); } catch (err) {
console.log("订阅消息出现异常",
err);
} }
} }
@ -643,22 +660,42 @@ class BleHelper {
var these = this; var these = this;
//开始搜索 //开始搜索
var Search = () => { var Search = () => {
console.log("Search........"); //只搜索合作供应商的服务id
return new Promise((resolve, reject) => { let serviceIds = serviceDic.map(v => {
uni.startBluetoothDevicesDiscovery({ return v.serviceId
services: [], });
allowDuplicatesKey: false,
success: (res) => {
//console.log('开始搜索蓝牙设备成功');
resolve(res);
}, //搜索一个服务id的设备循环调用
fail: (err) => { let RunSearch = (serviceId) => {
console.log(`搜索蓝牙设备失败:`, err); return new Promise((resolve, reject) => {
uni.startBluetoothDevicesDiscovery({
services: [serviceId],
allowDuplicatesKey: false,
success: (res) => {
//console.log('开始搜索蓝牙设备成功');
resolve(res);
reject(this.getError(err)); },
} fail: (err) => {
console.log(`搜索蓝牙设备失败:`, err);
reject(this.getError(err));
}
});
}); });
}
return new Promise((resolve, reject) => {
let promises = [];
for (let i = 0; i < serviceIds.length; i++) {
promises.push(RunSearch(serviceIds[i]));
}
Promise.all(promises).then(resolve).catch(reject);
}); });
@ -667,13 +704,15 @@ class BleHelper {
return this.OpenBlue().then((res) => { return this.OpenBlue().then((res) => {
console.log("蓝牙适配器状态", res) // console.log("蓝牙适配器状态", res)
return Search(); return Search();
}); });
} }
//停止搜索 //停止搜索
StopSearch() { StopSearch() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -717,21 +756,24 @@ class BleHelper {
//订阅消息 //订阅消息
subScribe(deviceId, state) { subScribe(deviceId, state) {
console.log("开始订阅消息");
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
setTimeout(() => { setTimeout(() => {
let c = this.data.LinkedList.find((v) => { let c = this.data.LinkedList.find((v) => {
return v.deviceId == deviceId; return v.deviceId == deviceId;
}); });
if (state) { if (state) {
if (c.notifyState) { if (c.notifyState) {
resolve(); resolve();
return; return;
} }
} }
// console.log("c=", c);
let startSubScribe = (id, serviceId, characteristicId) => { let startSubScribe = (id, serviceId, characteristicId) => {
// console.log("serviceId=", serviceId); console.log("serviceId=", serviceId);
// console.log("characteristicId=", characteristicId); console.log("characteristicId=", characteristicId);
let p1 = new Promise((succ, err) => { let p1 = new Promise((succ, err) => {
uni.notifyBLECharacteristicValueChange({ uni.notifyBLECharacteristicValueChange({
@ -790,33 +832,36 @@ class BleHelper {
if (item.properties.notify) { if (item.properties.notify) {
promies.push(startSubScribe(deviceId, serviceId, characteristicId)); promies.push(startSubScribe(deviceId, serviceId, characteristicId));
} }
} }
}else{
promies.push(startSubScribe(c.deviceId, c.notifyServiceid, c.notifyCharactId));
} }
if (promies.length > 0) { if (promies.length > 0) {
// console.log("234324324324");
Promise.allSettled(promies).then((results) => { Promise.allSettled(promies).then((results) => {
// console.log("11111");
results.forEach((result, index) => { results.forEach((result, index) => {
if (result.status === "fulfilled") { if (result.status === "fulfilled") {
// console.log(`操作${index + 1}成功:`, result.value); // console.log(`操作${index + 1}成功:`, result.value);
} else { } else {
// console.log(`操作${index + 1}失败:`, result.reason // console.log(`操作${index + 1}失败:`, result.reason
// .message); // .message);
} }
}); });
// console.log("订阅消息成功");
resolve(); resolve();
}).catch((ex) => { }).catch((ex) => {
// console.log("222222"); console.log("ex=",ex);
reject(ex); reject(ex);
}).finally(() => { }).finally(() => {
// console.log("finally") // console.log("finally")
}); });
} else { } else {
// console.log("33333"); console.log("没有特征需要订阅");
resolve(); resolve();
} }
@ -848,16 +893,44 @@ class BleHelper {
v.services = res.services; v.services = res.services;
} }
}); });
this.updateCache(); //this.updateCache();
var promises = []; var promises = [];
let s = null;
let se = res.services.find((v) => { let se = res.services.find((v) => {
return v.uuid.indexOf(targetServiceId) > -1;
s = serviceDic.find(k => {
return k.serviceId == v.uuid;
})
if (s) {
return true;
}
return false;
// return v.uuid.indexOf(targetServiceId) > -1;
}); });
if (se) { if (se) {
promises.push(this.getFeatrus(id, se.uuid, writeCharId, console.log("合作供应商的",s)
notifyCharId)); this.data.LinkedList.find((v) => {
if (v.deviceId == id) {
v.writeServiceId = s.serviceId;
v.wirteCharactId = s.writeId;
v.notifyServiceid = s.serviceId;
v.notifyCharactId = s.notifyId;
}
});
this.updateCache();
//合作供应商的设备,直接订阅消息,不走发现特征了
promises.push(this.subScribe(id, true));
// promises.push(this.getFeatrus(id, se.uuid, writeCharId,
// notifyCharId));
} else { } else {
console.log("预设的蓝牙服务和特征中找不到");
for (var i = 0; i < res.services.length; i++) { for (var i = 0; i < res.services.length; i++) {
let service = res.services[i]; let service = res.services[i];
promises.push(this.getFeatrus(id, service.uuid, promises.push(this.getFeatrus(id, service.uuid,
@ -876,13 +949,19 @@ class BleHelper {
Promise.all(promises) Promise.all(promises)
.then(results => { .then(results => {
if (!s) {
//非指定供应商的设备,走订阅消息
return this.subScribe(id, true);
} else {
//指定供应商的设备已经订阅过了
return Promise.resolve();
}
return this.subScribe(id, true);
}) })
.then((res) => { .then((res) => {
console.log('设备连接成功,初始化完成', res); console.log('设备连接成功,初始化完成', this.data
console.log("LinkedList=", this.data
.LinkedList); .LinkedList);
resolve(); resolve();
}) })
.catch(error => { .catch(error => {
@ -928,28 +1007,47 @@ class BleHelper {
deviceId: id, deviceId: id,
serviceId: serviceId, serviceId: serviceId,
success: (res) => { success: (res) => {
console.log("获取到特征:" + JSON.stringify(res)); console.log("获取到特征:" + JSON.stringify(res));
res.characteristics.forEach((v) => { // res.characteristics.forEach((v) => {
v.serviceId = serviceId; // v.serviceId = serviceId;
}); // });
//写特征 //写特征
let writeChar = res.characteristics.find(char => { let writeChar = res.characteristics.find(char => {
return char.uuid.indexOf(writeCharId) > -1 let fe = serviceDic.find(v => {
return v.serviceId == serviceId && v.writeId == char
.uuid;
})
// return char.uuid.indexOf(writeCharId) > -1
if (fe) {
return true;
}
return false;
}); });
if (!writeChar) { // if (!writeChar) {
writeChar = res.characteristics.find(char => { // writeChar = res.characteristics.find(char => {
return char.properties.write; // return char.properties.write;
}); // });
} // }
//通知特征 //通知特征
let notiChar = res.characteristics.find(char => { let notiChar = res.characteristics.find(char => {
return char.uuid.indexOf(notifyCharId) > -1; let fe = serviceDic.find(v => {
return v.serviceId == serviceId && v.notifyId ==
char.uuid;
})
// return char.uuid.indexOf(writeCharId) > -1
if (fe) {
return true;
}
return false;
// return char.uuid.indexOf(notifyCharId) > -1;
}); });
if (!notiChar) { // if (!notiChar) {
notiChar = res.characteristics.find(char => { // notiChar = res.characteristics.find(char => {
return char.properties.notify; // return char.properties.notify;
}); // });
} // }
this.data.LinkedList.find(function(v) { this.data.LinkedList.find(function(v) {
if (v.deviceId == id) { if (v.deviceId == id) {
if (!v.Characteristics) { if (!v.Characteristics) {
@ -1001,9 +1099,11 @@ class BleHelper {
} }
//连接某个设备 //连接某个设备
LinkBlue(deviceId, targetServiceId, writeCharId, notifyCharId) { LinkBlue(deviceId, targetServiceId, writeCharId, notifyCharId, maxRetries) {
if (maxRetries === undefined) {
maxRetries = 0; // 最大重试次数
}
if (!writeCharId) { if (!writeCharId) {
writeCharId = "xxxx"; // "FFE1"; writeCharId = "xxxx"; // "FFE1";
} }
@ -1030,7 +1130,7 @@ class BleHelper {
var linkDevice = () => { var linkDevice = () => {
// 添加重试次数限制 // 添加重试次数限制
let retryCount = 0; let retryCount = 0;
const maxRetries = 5; // 最大重试次数
const connect = () => { const connect = () => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -1050,7 +1150,7 @@ class BleHelper {
console.log("正在连接" + deviceId); console.log("正在连接" + deviceId);
uni.createBLEConnection({ uni.createBLEConnection({
deviceId: deviceId, deviceId: deviceId,
timeout: 30000, timeout: 3000,
success: (info) => { success: (info) => {
console.log("新连接成功", this.data.LinkedList); console.log("新连接成功", this.data.LinkedList);
this.getLinkBlue().then((arr) => { this.getLinkBlue().then((arr) => {
@ -1078,8 +1178,7 @@ class BleHelper {
deviceId: deviceId, deviceId: deviceId,
mtu: 512, mtu: 512,
success: (mtu) => { success: (mtu) => {
console.log("mtu设置成功",
mtu);
resolve(true); resolve(true);
}, },
fail: () => { fail: () => {
@ -1098,7 +1197,7 @@ class BleHelper {
}, },
fail: (ex) => { fail: (ex) => {
ex = this.getError(ex); ex = this.getError(ex);
console.log("蓝牙"+deviceId+"连接失败" + JSON.stringify(ex)); console.log("蓝牙" + deviceId + "连接失败" + JSON.stringify(ex));
// 连接超时后自动重试 // 连接超时后自动重试
if (ex.code === 10012 && retryCount < maxRetries) { if (ex.code === 10012 && retryCount < maxRetries) {