ios - 锁定屏幕时扫描蓝牙设备失败(使用 ibeacon)

标签 ios swift bluetooth ibeacon

我做了一个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 更新 我发现第一个问题描述的比较准确,锁屏的时候,设备离手机近,可以扫描,但是距离不远(这个距离前台可以扫描)

所以我猜是不是锁屏时扫描距离不准确?

最佳答案

  1. 您是否在 Xcode 项目的功能页面中打开了蓝牙的后台模式?

https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html

  1. 你需要先了解CoreBluetooth的机制。特别是您需要了解 Ranging 和 Monitoring 之间的区别,以及它们在后台模式下的局限性。有些操作只能在前台模式下实现。

Detecting beacons via iBeacon Monitoring & Ranging vs CoreBluetooth scanForPeripheralsWithServices

  1. didEnterRegiondidExitRegion 不是实时的,退出和重新进入区域之间有缓冲时间。此外,如果您在调用监控函数之前已经在该区域中,则不会触发 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/

相关文章:

swift - 如果没有#import,Swift 是否有自己的简单方法来检测循环依赖?

swift - 有没有办法对函数调用进行排队?

java - J2SE 中的手机监控器

ios - 将 Bluetooth® 2.1 + EDR class 2 设备与 iOS 应用程序连接

android - 检测蓝牙 'call' 按钮按下

iOS7 Autolayout 和 UIImageview 过大

ios - Facebook 应用邀请中的“缺少应用链接 URL”

ios - 在 dealloc 之后检查回调中的对象

javascript - 如何检查 iOS gui 自动化中的元素属性?

swift - macOS - NSView 中的背景图像在辅助监视器上消失