iBeacon在背景中测距

时间:2017-07-08 21:58:57

标签: swift xcode bluetooth-lowenergy core-location ibeacon

我正在尝试将我的应用程序设置为只在后台开始响铃的方式,并在用户点击肩膀按钮或主页按钮时获取通知(并非所有时间)并且我不想使用后台模式。所以我用swift进行了编码,当我的应用程序在前台和后台工作10秒钟时它会工作,当用户点亮他的手机屏幕时,测距不会重新启动。首先,我开始了后台任务,然后在应用程序委托中,我试图开始测量直接范围内的信标。有人可以帮我解决这个问题吗?

#Background Task   

    class BackgroundTask1 : NSObject {

        private let application: UIApplication!
        private var identifier = UIBackgroundTaskInvalid


        init(application: UIApplication) {
            self.application = application
        }

        class func run(application: UIApplication, handler: (BackgroundTask1) -> ()) {

            let backgroundTask = BackgroundTask1(application: application)
            backgroundTask.begin()
            handler(backgroundTask)
        }

        func begin() {
            print("begin")
            self.identifier = application.beginBackgroundTask {
                self.end()
            }
        }

        func end() {
            if (identifier != UIBackgroundTaskInvalid) {
                application.endBackgroundTask(identifier)
            }
            identifier = UIBackgroundTaskInvalid
        }}

#AppDelegate
    func init_()
        {
            let uuidString = "43F2ACD1-5522-4E0D-9E3F-4A828EA12C24"
            let beaconRegionIdentifier = "Hello"
            let beaconUUID:UUID = UUID(uuidString:uuidString)!
            beaconRegion = CLBeaconRegion(proximityUUID: beaconUUID, identifier: beaconRegionIdentifier)

            beaconRegion.notifyEntryStateOnDisplay = true

            locationManager = CLLocationManager()


            if (CLLocationManager.authorizationStatus() != CLAuthorizationStatus.authorizedWhenInUse) {
                locationManager!.requestWhenInUseAuthorization()
            }
            locationManager!.delegate = self
            locationManager!.pausesLocationUpdatesAutomatically=false
        }

        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

            init_()

            locationManager?.startRangingBeacons(in: beaconRegion)

            return true
        }

        func applicationWillResignActive(_ application: UIApplication) {
            // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
            // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
        }

        var timer = Timer()
        func applicationDidEnterBackground(_ application: UIApplication) {
            // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
            // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
            BackgroundTask1.run(application:
            application) { (BackgroundTask1_) in

                DispatchQueue.global(qos: .default).async
                    {
                        DispatchQueue.main.async {

                            self.init_()
                            self.locationManager?.startRangingBeacons(in: self.beaconRegion)
                        }
                }

            }


        }

0 个答案:

没有答案