微信小程序安卓14蓝牙连接需要打开微信附近设备权限提醒

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].deviceId
									console.log('找到设备')
									console.log(res.devices[i])
									if (uni.getSystemInfoSync().platform == 'ios') {
										_.deviceId = res.devices[i].deviceId
										uni.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.效果
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值