简体   繁体   English

Swift 2信标扫描在后台

[英]Swift 2 Beacon scan in background

I'm trying to run a beacon background detection in my app. 我正在尝试在我的应用程序中运行信标背景检测。 I created a dedicated class for this and launch it in my ViewController: 我为此创建了一个专用类,并在ViewController中启动它:

    override func viewDidLoad() {
      super.viewDidLoad()
      beaconSharedInstance
      ....
    }

and here is the code of my class: 这是我班上的代码:

let beaconSharedInstance = iBeacon();

class iBeacon: NSObject, UIApplicationDelegate, CLLocationManagerDelegate {

var locationManager: CLLocationManager!
var beaconRegion:CLBeaconRegion!

override init(){

    super.init()

    let uuidString = "FDA50693-A4E2-4FB1-AFCF-C6EB07640978"
    let beaconIdentifier = "uby-06B524"
    let beaconUUID:NSUUID = NSUUID(UUIDString: uuidString)!
    beaconRegion = CLBeaconRegion(proximityUUID: beaconUUID, identifier: beaconIdentifier)

    locationManager = CLLocationManager()
    locationManager.delegate = self

    if(CLLocationManager.authorizationStatus() != CLAuthorizationStatus.AuthorizedAlways){
        locationManager.requestAlwaysAuthorization()
    }

    locationManager.allowsBackgroundLocationUpdates = true
    locationManager!.startRangingBeaconsInRegion(beaconRegion)
}


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

    if(beacons.count > 0) {

        print("Discovered beacon \(beacons[0].rssi) dBM name: \(beacons[0].proximityUUID)")
    }


}

} }

this class is working perfectly in foreground, but as soon as my app is becoming inactive, I can see in the log files: 该类在前台运行良好,但是一旦我的应用程序变为非活动状态,就可以在日志文件中看到:

Ending background task

and the CLLocatioManager stops... 然后CLLocatioManager停止...

I set Location updates in the Background mode of my project as well as NSLocationAlwaysUsageDescription in my info.plist file but I still have the same behavior, my thread stops when app is inactive. 我在项目的背景模式下设置了位置更新,并在info.plist文件中设置了NSLocationAlwaysUsageDescription,但是我仍然有相同的行为,当应用程序处于非活动状态时,线程停止了。

I also tried to launch my Thread like this: 我也试图像这样启动我的线程:

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
    })

but in that case CLLocationManager never starts...any idea ? 但是在那种情况下,CLLocationManager永远不会启动...知道吗?

The background task needs to have a loop, otherwise it will simply exit. 后台任务需要循环,否则它将退出。 Try: 尝试:

dispatch_async(backgroundQueue, {
    print("Running beacon detection in the background queue")
    beaconSharedInstance
    while(true) {
      NSThread.sleepForTineInterval(1)
      print("running...")
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM