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

iBeacon - can detect using startRangingBeacons but not for didEnterRegion

我需要检测设备何时进入或离开某个区域并根据此执行一些操作。

使用 '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 { [=10=].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!")
    }

}

您正在尝试更改位置管理器回调中的 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