【问题标题】:Scan Bluetooth device failed when I lock screen(use ibeacon)锁屏时扫描蓝牙设备失败(使用ibeacon)
【发布时间】:2017-05-04 01:22:08
【问题描述】:

我做了一个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 委托代码:

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

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

【问题讨论】:

  • 我建议您发布到目前为止的代码,以便人们可以更好地帮助您

标签: ios swift bluetooth ibeacon


【解决方案1】:
  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、打不开,在尝试打开之前,还是有同样的问题

您需要打开它,并进行一些设置才能实现后台扫描。

2、非常感谢,但是我发现扫描失败了,不是每次,偶尔会。这就是我不明白的地方。

3、是的,我的实验是先离开区域,然后进入区域,因为我有两个beacon设备在一起,UUID不同,关闭一个,然后我去扫描,然后发现有时候不扫描, 有时是可能的

我找不到您正在使用的蓝牙库的代码。好像是大陆开发者写的?可以发一下那个库的链接吗?

我的经验,CoreBluetooth 测距功能相当可靠。所以我想问题是你没有打开后台模式。在“功能”选项卡中打开Acts as a Bluetooth LE accessoryUses Bluetooth LE accessories

另外,我建议您阅读/收藏Radius Networks 的博客。他们的开发者博客值得一读。

http://developer.radiusnetworks.com/2014/11/13/extending-background-ranging-on-ios.html

【讨论】:

  • 1、打不开,在尝试打开之前,还是有同样的问题
  • 2、非常感谢,但是我发现扫描失败了,不是每次都是,偶尔会。这就是我不明白的地方。
  • 3、是的,我的实验是先离开区域,然后进入区域,因为我有两个beacon设备在一起,UUID不同,关闭一个,然后我去扫描,然后发现有时不扫描,有时可能
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多