Swift2 doRangeBeacons从未打过电话

时间:2016-08-21 22:22:37

标签: swift ibeacon

我试图实现一个专用的类来扫描特定的iBeacon(在前台和后台)但是函数didRangeBeacons永远不会被调用。

我还在info.plist中设置了参数NSLocationAlwaysUsageDescription。

这是我开发的课程:

class iBeacon: NSObject, UIApplicationDelegate, CLLocationManagerDelegate {

var locationManager: CLLocationManager!
var beaconRegion:CLBeaconRegion!

override init(){

    super.init()
    print("init...")

    let uuidString = "B737D0E7-AF53-9B83-E5D2-922140A91234"
    let beaconIdentifier = "nbuit-06B908"
    let beaconUUID:NSUUID = NSUUID(UUIDString: uuidString)!
    beaconRegion = CLBeaconRegion(proximityUUID: beaconUUID,
                                                     identifier: beaconIdentifier)

    beaconRegion.notifyOnEntry = true
    locationManager = CLLocationManager()
    locationManager.delegate = self
    if(locationManager!.respondsToSelector("requestAlwaysAuthorization")) {
        locationManager!.requestAlwaysAuthorization()
    }

    locationManager!.pausesLocationUpdatesAutomatically = false

    locationManager!.startMonitoringForRegion(beaconRegion)
    locationManager!.startRangingBeaconsInRegion(beaconRegion)
    locationManager!.startUpdatingLocation()
}


func sendLocalNotificationWithMessage(message: String!) {
    let notification:UILocalNotification = UILocalNotification()
    notification.alertBody = message
    UIApplication.sharedApplication().scheduleLocalNotification(notification)
}

func locationManager(manager: CLLocationManager!, didStartMonitoringForRegion region: CLRegion!){
    locationManager.requestStateForRegion(region)
}

func locationManager(manager: CLLocationManager!, didDetermineState state: CLRegionState, forRegion region: CLRegion!) {
    if state == CLRegionState.Inside {
        locationManager.startRangingBeaconsInRegion(beaconRegion)
    }
    else {
        locationManager.stopRangingBeaconsInRegion(beaconRegion)
    }
}


func locationManager(manager: CLLocationManager,
                     didRangeBeacons beacons: [CLBeacon],
                                     inRegion region: CLBeaconRegion)  {
    print("didRangeBeacons");
    var message:String = ""

    if(beacons.count > 0) {
        let nearestBeacon:CLBeacon = beacons[0] as! CLBeacon

        switch nearestBeacon.proximity {
        case CLProximity.Far:
            message = "You are far away from the beacon"
        case CLProximity.Near:
            message = "You are near the beacon"
        case CLProximity.Immediate:
            message = "You are in the immediate proximity of the beacon"
        case CLProximity.Unknown:
            return
        }
    } else {
        message = "No beacons are nearby"
    }

    print("%@", message)
    sendLocalNotificationWithMessage(message)
}



func locationManager(manager: CLLocationManager!, didEnterRegion region: CLRegion!) {
    print("Beacon in range")
}


func locationManager(manager: CLLocationManager!, didExitRegion region: CLRegion!) {
    print("No beacons in range")
}


func locationManager(manager: CLLocationManager!, monitoringDidFailForRegion region: CLRegion!, withError error: NSError!) {
    print("Failed monitoring region: \(error.description)")
}

func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
    print("Location manager failed: \(error.description)")
}

}

我无法找到未检测到iBeacon的原因。我还检查了BLE扫描仪应用程序中的UUID和名称,它似乎是正确的。

如果您有任何想法可以帮助我,那就太棒了。

编辑:

我终于找到了从未启动过LocationManager的原因,我正在实例化我的类:

let qualityOfServiceClass = DISPATCH_QUEUE_PRIORITY_DEFAULT
    let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
    dispatch_async(backgroundQueue, {
        print("Running beacon detection in the background queue")
        beaconSharedInstance
    })

我希望让这个实例在后台运行,但只需调用我的实例就像这样

beaconSharedInstance

启动并正确扫描区域。但是一旦我的应用程序处于非活动状态,我就会有以下日志:

Ending background task

我的应用停止扫描。我设置了以下参数

locationManager.allowsBackgroundLocationUpdates = true

并设置了我的项目的后台模式功能。

任何想法为什么我的实例在应用程序处于后台时停止了?

0 个答案:

没有答案