w***@foxmail.com
w***@foxmail.com
  • 发布:2019-12-25 19:31
  • 更新:2019-12-25 19:31
  • 阅读:2886

uni-app 蓝牙连接被自动断开问题

分类:uni-app

遍历 快速对两个蓝牙设备(deviceId,mac地址不同)进行连接操作,时间隔为1秒,蓝牙刚进行连接,监听蓝牙连接状态,直接监听到被自动断开,而且断开两次,有知道怎么回事的不?ios ,android 有问题, 小程序没问题。

···javascript
autoConnect(){
let _this=this;
this.bleList.forEach((item,index)=>{
setTimeout(function(){
console.log(' ==== start auto connect ====')
_this.createBLEConnection(this.deviceId,true);
}.bind(item),1000);
}
});
},
/**

  • 连接低功耗蓝牙设备。
  • @param {Object} deviceId 设备ID
  • @param {Object} isAutoConnect 是否自动连接
    */
    createBLEConnection(deviceId,isAutoConnect){
    uni.showLoading({title:'连接中...'});
    console.log('连接中设备ID=>',deviceId);
    uni.createBLEConnection({
    // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
    deviceId,
    timeout:1000,
    success:res=> {
    uni.showToast({title:"成功",icon:'success'});
    this.bleList[this.getDeviceIndex(deviceId)].isConnect=true;
    this.saveConnectRecord(deviceId,true);
    },
    fail:error=>{
    this.bleList[this.getDeviceIndex(deviceId)].isConnect=false;
    this.bleList[this.getDeviceIndex(deviceId)].connectNum+=1;
    if(error.errCode == 10003 && this.bleList[this.getDeviceIndex(deviceId)].connectNum<3){ //连接超时少于3次,自动重连
    this.createBLEConnection(deviceId,true);
    }else{
    uni.showModal({
    content:'连接失败,请重新连接!',
    confirmText:'重新连接',
    success:res=>{
    res.confirm && this.createBLEConnection(deviceId,false);
    }
    });
    }
    console.log("连接失败"+ JSON.stringify(error));
    },
    complete:cRes=>{
    uni.hideLoading();
    console.log('连接 completed',cRes);
    }
    });
    }
    ···
2019-12-25 19:31 负责人:无 分享
已邀请:

该问题目前已经被锁定, 无法添加新回复