uniapp请求本机蓝牙是否打开,检测本机距离ibeacon距离实例开发

//之前做这个百度哪哪都是坑,这个拒绝踩坑,用了记得点个赞,评论多交流
uni.openBluetoothAdapter({
				//这里是检测蓝牙是否打开的接口打开进入success否则进入fail
				success: () => {
					//蓝牙已经打开
					console.log('蓝牙打开了');
					// 要想检测手机距离IBeacon设备距离必须检测是否开手机定位
					uni.getSystemInfo({
						// 获取系统信息
						success(res) {
							let locationEnabled = res.locationEnabled; //判断手机定位服务是否开启
							// let locationAuthorized = res.locationAuthorized; //判断定位服务是否允许微信授权
							console.log('定位服务是否开启', locationEnabled);
							if (locationEnabled == false) {
								// GPS 未授权
								uni.showToast({
									title: '定位未打开',
									icon: 'none',
									duration: 2000
								});
								//定位未打开,再次在本页面打开定位重新检测还是会检测没打开定位所以我用的返回上一页
								uni.navigateBack({
									delta:1
								})
							} else {
								uni.startBeaconDiscovery({
									// 开始搜索ibeacon设备 格式是8-4-4-4-12
									uuids: ['D777E6AC-87FB-964C-91C5-789D59F38355'],
									success: function(res) {
										//搜索成功后进入这里
										console.log('搜索成功' + res);
										uni.onBeaconUpdate(function(res) {
											//监听设备更新事件
											console.log('监听设备更新', res);
											if (res && res.beacons && res.beacons.length > 0) {
												uni.getBeacons({
													//获取搜索到设备的参数
													success: res => {
														console.log(res);
														console.log('设备广播uuid' + res.beacons[0].uuid);
														console.log('设备主id' + res.beacons[0].major);
														console.log('设备次id' + res.beacons[0].minor);
														console.log('设备距离' + res.beacons[0].accuracy);
														// setTimeout();
														if (res.beacons[0].accuracy < 3) {
															uni.stopBeaconDiscovery({
																success: function() {
																	console.log('停止扫描设备!');
																}
															});
														} else {
															uni.showToast({
																title: '靠近一点',
																duration: 2000
															});
														}
														setTimeout(function() {
															uni.stopBeaconDiscovery({
																success: function() {
																	console.log('停止扫描设备!');
																}
															});
															uni.showToast({
																title: '靠近一点重新感应',
																icon: 'error',
																duration: 3000
															});
														}, 10 * 1000);
													}
												});
											}
										});
									}
								});
							}
						}
					});
				},
				fail: res => {
					if (res.errCode == 10001) {
						uni.showToast({
							title: '蓝牙未打开',
							icon: 'none',
							duration: 2000
						});
					} else {
						uni.showToast({
							title: res.errMsg,
							icon: 'none',
							duration: 2000
						});
					}
				}
			});

你可能感兴趣的:(javascript,前端,开发语言)