uniapp搜索附近蓝牙信标(iBeacon)

一、 iBeacon介绍

iBeacon是苹果在2013年WWDC上推出一项基于蓝牙4.0(Bluetooth LE | BLE | Bluetooth Smart)的精准微定位技术,在iPhone 4S后支持。当你的手持设备靠近一个Beacon基站时,设备就能够感应到Beacon信号,范围可以从几毫米到50米。因为是一种定位技术,苹果将iBeacon相关的接口放到了 CoreLocation.framework。Google在Android 4.3及后续版本支持了该功能,只要满足iBeacon技术标准即可。iBeacon底层的技术使用的使用BLE(Bluetooth Low Energy)。

二、技术文档

参考:lBeacon API

三、代码实现

//开始搜索蓝牙设备
startDiscoverBluetooth() {
	let that = this;
	//蓝牙初始化
	uni.openBluetoothAdapter({ 
		success(res) {
			uni.getLocation({
				success(res) {
					//开始搜索蓝牙设备
					uni.startBeaconDiscovery({
						success(res) {
							uni.onBeaconUpdate((res) => {
								console.log("设备信息=> ", res)
							})
						}, fail(res) {
							console.log('搜索蓝牙设备失败!', res);
						}
					})
				}, fail(res) {
					uni.showModal({
						title:'提示',
						content:'请打开位置,并下拉刷新!',
						confirmText:'好的',
						showCancel:false,
					})
				}
			})
		}, fail(res) {
			uni.showModal({
				title:'提示',
				content:'请打开蓝牙,并下拉刷新!',
				confirmText:'好的',
				showCancel:false,
			})
		}
	})
},

四、小结

目前只能在手机处于亮屏状态下(包括软件在后台运行)才能监测到周围的信标设备,熄屏状态下监测不到(目前还未解决),如有解决的,欢迎大神留言讨论!

你可能感兴趣的:(uniapp,uni-app,javascript,信号处理)