iBeacon - 可以使用 startRangingBeacons 进行检测,但不能用于 didEnterRegion

Posted

技术标签:

【中文标题】iBeacon - 可以使用 startRangingBeacons 进行检测,但不能用于 didEnterRegion【英文标题】:iBeacon - can detect using startRangingBeacons but not for didEnterRegion 【发布时间】:2015-02-21 14:01:30 【问题描述】:

我需要检测设备何时进入或退出某个区域并基于此执行一些操作。

使用“startRangingBeaconsInRegion”,我可以检测到最近的 iBeacon 并据此更改背景颜色,如果无法检测到 iBeacon,则更改为白色。

我无法让它在“didEnterRegion”或“didExitRegion”上触发。

我知道如果设备已经在该区域中,则 enterRegion 不会触发。我确保未检测到信标(白屏),然后检测到信标(彩色屏幕) - 但没有任何反应。

我尝试过使用 estimote SDK,但遇到了同样的问题。重新启动设备也没有帮助。

我的代码如下,有什么建议吗?

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate 

    let locationManager = CLLocationManager()
    let region = CLBeaconRegion(proximityUUID: NSUUID(UUIDString: "B9407F30-F5F8-466E-AFF9-25556B57FE6D"), identifier: "Estimotes")

    let colors = [
        3861: UIColor(red: 84/255, green: 77/255, blue: 160/255, alpha: 1),
        19152: UIColor(red: 142/255, green: 212/255, blue: 220/255, alpha: 1),
        40527: UIColor(red: 162/255, green: 213/255, blue: 181/255, alpha: 1)
    ]

    override func viewDidLoad() 
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        locationManager.delegate = self
        locationManager.pausesLocationUpdatesAutomatically = false
        region.notifyEntryStateOnDisplay  = true
        region.notifyOnEntry = true
        region.notifyOnExit = true

        // Request authorisation to track location
        if (CLLocationManager.authorizationStatus() != CLAuthorizationStatus.AuthorizedWhenInUse) 
            locationManager.requestWhenInUseAuthorization()
        


        if (CLLocationManager .isMonitoringAvailableForClass(CLBeaconRegion))
        
            println("OK")
        
        else 
            println("Problem")

        

        locationManager.startMonitoringForRegion(region)
        locationManager.startRangingBeaconsInRegion(region)
        locationManager.startUpdatingLocation()

    

    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    
    func locationManager(manager: CLLocationManager!, didRangeBeacons beacons: [AnyObject]!, inRegion region: CLBeaconRegion!) 

        let knownBeacons = beacons.filter  $0.proximity != CLProximity.Unknown 
        if (knownBeacons.count > 0) 
            let closestBeacon = knownBeacons[0] as CLBeacon
            self.view.backgroundColor = self.colors[closestBeacon.minor.integerValue]
            println(beacons)

        
        else 
            self.view.backgroundColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 1)
        
    
    func locationManager(manager: CLLocationManager!, didEnterRegion region: CLRegion!) 
        println("Region entered")
    

    func locationManager(manager: CLLocationManager!, didExitRegion region: CLRegion!) 
        println("Region exited")
    
    func locationManager(manager: CLLocationManager!, monitoringDidFailForRegion region: CLRegion!, withError error: NSError!) 
        println("FAIL!")
    


【问题讨论】:

【参考方案1】:

您正在尝试更改位置管理器回调中的 UI - 这是一个很大的禁忌,因为这些回调通常不在主线程上。将每个 UI 更改包装在一个调度块中到主线程,看看是否有任何改变。

啊,几天前我也遇到了同样的问题。你需要实现:

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) 
    println("STATUS CHANGED: \(status.rawValue)")
    if status == .AuthorizedAlways && !monitoring 
        println("YAY! Authorized!")
        locationManager.startMonitoringForRegion(beaconRegion)
        monitoring = true
    

只有在系统告知您有权限后,您才能开始监控。

另外,如果你想要后台通知,你必须在你的 Info.plist 中有这个字符串:NSLocationAlwaysUsageDescription,并且你需要在Required background modes 中定义的App registers for location updates

【讨论】:

谢谢!我将“NSLocationAlwaysUsageDescription”添加到 plist 中,它起作用了。我已经有了“NSLocationWhenInUseUsageDescription”——但这还不够……这很奇怪,因为这就是我所要求的。无论如何,它现在可以工作了,感谢您的帮助!

以上是关于iBeacon - 可以使用 startRangingBeacons 进行检测,但不能用于 didEnterRegion的主要内容,如果未能解决你的问题,请参考以下文章

我可以在 Android 中将 iBeacon 与附近消息一起使用吗?

ibeacon怎么使用

iOS ibeacon 使用详解

是否可以在 iBeacon 上写入数据

Android 开发中 iBeacon的使用

Android 开发中 iBeacon的使用