小程序IOS搜索蓝牙设备有时候搜索不到
发布于 5 年前 作者 baixia 15415 次浏览 来自 问答
  • 在IOS设别上调用wx.startBluetoothDevicesDiscovery时,如果以前没连接过这个蓝牙设备,就会出现很难搜索到这个蓝牙设备的情况,我退出小程序再进去搜索的时候就能很快的搜索到,问题就出现在第一个搜索新设备的时候
  • 预期表现
  • 复现路径
  • 提供一个最简复现 Demo

// 1、获取本机蓝牙适配器状态

wx.getBluetoothAdapterState({

success(res) {

let available = res.available;

if (available == false) {

wx.showModal({

title: ‘提示’,

content: ‘手机蓝牙不可用,请重启后再试’,

showCancel: false,

success(res) {

}

})

return;

}

// 2、发现周边设备

wx.startBluetoothDevicesDiscovery({

services: [],

success: function (res) {

// console.log(‘开始搜索周边设备’)

wx.showLoading({

title: ‘正在搜索中’,

});

// 3、获取周边设备

wx.getBluetoothDevices({

success: function (res) {

for (let i = 0; i < res.devices.length; i++) {

if (lockNum.indexOf(‘CASZX’) >= 0) {

var name = res.devices[i].name;

if (name == lockNum) {

_this.setData({

deviceId: res.devices[i].deviceId

});

wx.hideLoading()

_this.stopBluetoothDevicesDiscovery();

_this.openLock(flg)

break

}

}

else if (app.isMac(lockNum)) {

var advertisData = res.devices[i].advertisData;

var mac = _this.ab2hex(advertisData);

if (mac == lockNum) {

_this.setData({

deviceId: res.devices[i].deviceId

});

wx.hideLoading()

_this.stopBluetoothDevicesDiscovery();

_this.openLock(flg)

break

}

}

}

},

})

//4 、监听发现设备

wx.onBluetoothDeviceFound(function (devices) {

for (let i = 0; i < devices.devices.length; i++) {

if (lockNum.indexOf(‘CASZX’) >= 0) {

var name = devices.devices[i].name;

if (name == lockNum) {

_this.setData({

deviceId: devices.devices[i].deviceId

});

wx.hideLoading()

_this.stopBluetoothDevicesDiscovery();

_this.openLock(flg)

break

}

}

else if (app.isMac(lockNum)) {

var advertisData = devices.devices[i].advertisData;

var mac = _this.ab2hex(advertisData);

if (mac == lockNum) {

_this.setData({

deviceId: devices.devices[i].deviceId

});

wx.hideLoading()

_this.stopBluetoothDevicesDiscovery();

_this.openLock(flg)

break

}

}

}

})

},

complete: function (res) {

}

})

},

fail: function (res) {

console.log(res)

}

})

2 回复

你好,麻烦提供出现问题的具体机型、微信版本号、系统版本号,以及能复现问题的代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)

你好,请问你解决了这个问题吗

回到顶部