1.wx.onBluetoothDeviceFound去搜索附近的设备如果搜索不到一个设备则默认附近设备权限没打开(ps微信开放社区里面的 wx.getAppAuthorizeSetting接口里面的bluetoothAuthorized一样会返回“authorized”判断不了只要允许授权蓝牙,附近设备权限没授权依然显示授权)
2.讨巧的方法通过蓝牙搜索附近设备如果设备列表搜索出来是0则认为微信《附近设备》权限没打开
/*---------查找附近的蓝牙设备----------*/find() {let _ = this;console.log('蓝牙开始查找')console.log('蓝牙名字=' + _.deviceName)_.findList = []wx.startBluetoothDevicesDiscovery({services: [],success: function() {console.log('开始搜索蓝牙')wx.onBluetoothDeviceFound(function(res) {console.log(res, '搜索蓝牙相关设备')for (let i = 0; i < res.devices.length; i++) {// app.globalData.bleConfig.name 这里提前就获取了设备的name_.findList.push(res.devices[i])if (res.devices[i].name == _.deviceName) {var deviceId = res.devices[i].deviceIdconsole.log('找到设备')console.log(res.devices[i])if (uni.getSystemInfoSync().platform == 'ios') {_.deviceId = res.devices[i].deviceIduni.setStorageSync('currentDeviceId', _.deviceId)}console.log('找到设备')// ios搜索出目标设备后,将停止搜索,否则影响手机性能wx.stopBluetoothDevicesDiscovery({success: function() {_.toConnectEquip()}})}}})},fail: function(err) {console.log('没有找到设备')console.log(err)console.log('没有找到设备')wx.stopBluetoothDevicesDiscovery({success: function() {}})}})let timers = setTimeout(() => {wx.stopBluetoothDevicesDiscovery({success: function() {}})console.log('真的没有找到设备')console.log(_.findList.length)console.log('deviceId=' + _.deviceId)console.log('真的没有找到设备')if (_.findList.length == 0) {if (uni.getSystemInfoSync().platform === 'android') {_.modalShow = true;uni.openAppAuthorizeSetting({success(res) {console.log(res)}})}}uni.hideLoading()clearTimeout(timers)timers = null;}, 6000)},
3.如果没发现设备则让用户打开授权
/*安卓14打开发现附近的设备*/openSetting() {uni.openAppAuthorizeSetting({success(res) {console.log(res)}})},
4.效果