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

时间:2015-02-21 14:01:30

标签: ios swift ibeacon estimote

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

使用' 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 个答案:

答案 0 :(得分:2)

您正在尝试更改位置管理器回调中的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,并且您需要App registers for location updates中定义的Required background modes