收藏
回答

小程序调用低耗蓝牙相关API,断开释放连接资源和重连失败?

当小程序第一次成功搜索到并了连接蓝牙后,成功调用wx.closeBLEConnection()断开蓝牙释放资源后,再次重新搜索连接蓝牙一直返回错误代码,(经过测试部分概率下
手动开关蓝牙再次搜索连接可以成功),我这里初步判断可能是wx.closeBLEConnection()调用后没有彻底断开蓝牙连接,导致再次搜索时无法连接,所以想问:小程序官方
是否有小程序彻底清除已连接的蓝牙连接资源的API或者解决这个问题的方案。调用wx.startBluetoothDevicesDiscovery() 能否搜索到已连接或者未成功
断开的蓝牙设备信息进行回调断开操作。 微信版本8.0.2,微信开发者工具版本1.05.2102010


//搜索蓝牙
function DiscoverBLE(ms{
  return new Promise(function(resolve, reject{
    if (m_Deviceid == "") {
      DevicesDiscovery(resolve, ms);
    } else {
      wx.closeBLEConnection({
        deviceId: m_Deviceid,
        successfunction(res{
          console.log('DisconnectBLE', res)
          connectSuccess = '';
          wx.closeBluetoothAdapter();
          setTimeout(function() {
            DevicesDiscovery(resolve, ms);
          }, 2000);


        },
        fail(res) => {
          connectSuccess = '';
          wx.closeBluetoothAdapter();
          setTimeout(function() {
            DevicesDiscovery(resolve, ms);
          }, 2000);
        }
      })
      receive_apdu = ""
    }
  })
}
function ConnectDeviceid(s_Deviceid{
  m_Deviceid = s_Deviceid;
  connectSuccess = '';
  console.log('m_Deviceid:', m_Deviceid);
  wx.createBLEConnection({
    deviceId: m_Deviceid,
    success(res) => {
      console.log('createBLEConnection success:')
      console.log('   ', res)
      setTimeout(() => {
        getBLEDeviceServices();
      }, 4000)
      wx.onBLEConnectionStateChange(function(res{
        // 该方法回调中可以用于处理连接意外断开等异常情况
        console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`);
        if ((!res.connected) && (connectSuccess == 1)) {
          DisconnectBLE();
        }
      })
    },
    fail(res) => {
      console.log('createBLEConnection fail:')
      console.log('   ', res)
      connectSuccess = '0'//连接失败
      if (res.errCode === 10003) {
        console.log('res.errCode === 10003')
        wx.closeBLEConnection({
          deviceId: m_Deviceid,
          successfunction(res{
            console.log('DisconnectBLE success', res)
            connectSuccess = '0'//蓝牙连接
          },
          fail(res) => {
            console.log('DisconnectBLE fail', res)
            wx.closeBluetoothAdapter();
          }
        })
      }
      wx.showToast({
        title'连接失败',
        icon'错误',
        duration2000
      })
    }
  })
}
//检测连接配对
function CheckConnectBLE(mode{
  return new Promise(function(resolve, reject{
    var times = 0;
    var i = setInterval(function() {
      if (times >= 30) {
        resolve('0');
        clearInterval(i)
      } else {
        console.log("times=", times);
        if (connectSuccess == '1') {
          times++;
          if (mode == 1) {
            CheckByMode1();
            ReceiveCheckByMode1(2000, mode).then(function(_receive_apdu{
              if (_receive_apdu == '01') {
                times = 30;
                resolve('1');
              }
            });
          } else if (mode == 2) {
            SendAPDU('00');
            ReceiveAPDU(2000, mode).then(function(_receive_apdu{
              if (_receive_apdu == '01') {
                times = 30;
                resolve('1');
              }
            });
          }
        } else if (connectSuccess == '0') {
          times = 30;
          resolve('0');
        } else {
          console.log("wait connect...");
        }
      }
    }, 2000);
  });
}
回答关注问题邀请回答
收藏

1 个回答

  • 社区技术运营专员--阳光
    社区技术运营专员--阳光
    2021-04-07

    1. 应该不是假断开的问题

    2. startBluetoothDevicesDiscovery能否搜索到已连接或者未成功断开的蓝牙设备?不能

    3. 如果说的是Android手机的问题,建议试试最开始startBluetoothDevicesDiscovery的时候多传个参数refreshCache:false试试;

    2021-04-07
    有用
    回复 1
    • 猪猪🐷的饲养员
      猪猪🐷的饲养员
      2021-11-04
      这个参数,文档上面没有写,传了有用吗?
      2021-11-04
      回复
登录 后发表内容
问题标签