我做了一个ibeacon项目,现在发现两个问题: 首先,当手机锁屏时,我会扫描蓝牙设备(我确定在信标区域),有时扫描失败并返回空数组 第二,当我锁屏的时候,有时候didEnterRegion和didExitRegion已经停止了,当我点亮屏幕的时候,它们又继续了
现在每次锁屏都想扫描设备,怎么办?
MonitoringForRegions 代码:
let region = BRTBeaconRegion.init(proximityUUID: proxiID, identifier: proxiID.UUIDString)
region.notifyOnEntry = true
region.notifyOnExit = true
region.notifyEntryStateOnDisplay = true
BRTBeaconSDK.startMonitoringForRegions(region)
Appdelegate 委托(delegate)代码:
func beaconManager(manager:BRTBeaconManager,didEnterRegion region:BRTBeaconRegion){
if region.notifyOnEntry {
//PublicMethod().sendLocalNotification(BEACON_TIP_IN)
print("\(NSDate())-------enter--------")
}
}
func beaconManager(manager:BRTBeaconManager,didExitRegion region:BRTBeaconRegion){
if region.notifyOnExit {
//PublicMethod().sendLocalNotification(BEACON_TIP_OUT)
print("\(NSDate())-------exit--------")
}
}
func beaconManager(manager:BRTBeaconManager,didDetermineState state:CLRegionState,forRegion region:BRTBeaconRegion){
print("didDetermineState")
}
扫码:
BRTBeaconSDK .startRangingBeaconsInRegions(regionArray) { (beacons, region, error ) in
for beacon in beacons as! [BRTBeacon]{
print("beacons count:\(beacons.count) name :\(beacon.name) macaddress:\(beacon.macAddress) major:\(beacon.major) minor:\(beacon.minor) ")
}
}
14:25 更新 我发现第一个问题描述的比较准确,锁屏的时候,设备离手机近,可以扫描,但是距离不远(这个距离前台可以扫描)
所以我猜是不是锁屏时扫描距离不准确?
最佳答案
- 您是否在 Xcode 项目的功能页面中打开了蓝牙的后台模式?
- 你需要先了解CoreBluetooth的机制。特别是您需要了解 Ranging 和 Monitoring 之间的区别,以及它们在后台模式下的局限性。有些操作只能在前台模式下实现。
Detecting beacons via iBeacon Monitoring & Ranging vs CoreBluetooth scanForPeripheralsWithServices
didEnterRegion
和didExitRegion
不是实时的,退出和重新进入区域之间有缓冲时间。此外,如果您在调用监控函数之前已经在该区域中,则不会触发didEnterRegion
。
更新
1、Not open ,Before trying to open, or have the same problem
您需要开启它,并进行一些设置才能实现后台扫描。
2、Thank you very much, but I found the scan failed, not every time, occasionally.That's what I don't understand.
3、yes,My experiments are the first to leave the area, then enter the area, because I have two beacon equipment together, UUID is different, close one, then I went to scan, and then found sometimes does not scan, sometimes it is possible
我找不到您正在使用的蓝牙库的代码。好像是大陆开发者写的?你能发布那个图书馆的链接吗?
我的经验,CoreBluetooth 的测距功能非常可靠。所以我想问题是你没有打开后台模式。在“功能”选项卡中打开充当蓝牙 LE 配件
和使用蓝牙 LE 配件
。
此外,我建议您阅读/收藏 Radius Networks
的博客。他们的开发者博客值得一读。
http://developer.radiusnetworks.com/2014/11/13/extending-background-ranging-on-ios.html
关于ios - 锁定屏幕时扫描蓝牙设备失败(使用 ibeacon),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41215004/