简体   繁体   English

信标检测前景,背景和杀死(从背景中删除)应用程序

[英]beacon detecting in foreground, background and kill (remove from background) app

I am working on beacon detecting in foreground, background and kill (remove from background) app.I have kontakt beacon.I have implemented location Manager delegate method but I can't detect beacon in foreground and background not even called method didEnterRegion and didExitRegion .For solution I merge location manager delegate with KTKDevicesManagerDelegate. 我正在研究前景中的信标检测,背景和杀死(从后台删除)应用程序。我有kontakt信标。我已经实现了位置管理器委托方法但我无法检测前景和背景中的信标甚至没有调用方法didEnterRegiondidExitRegion 。对于解决方案,我将位置管理器委托与KTKDevicesManagerDelegate合并。 I can't detect beacon in location manager so I have tried implementing in didRangeBeacons of location manager method I start devicesManager.startDevicesDiscovery() of KTKDeviceManagerDelegate that time detect beacon in Foreground and background ,but I can't detect beacon when we kill app. 我无法在位置管理器中检测到信标,所以我尝试在位置管理器方法的didRangeBeacons中实现我启动devicesManager.startDevicesDiscovery()时间检测前景和背景中的信标,但是当我们杀死app时我无法检测到信标。 I have already added in Info.plist NSBluetoothPeripheralUsageDescription , NSLocationAlwaysUsageDescription , NSLocationWhenInUseUsageDescription , bluetooth-central , fetch , location. 我已经在Info.plist中添加了NSBluetoothPeripheralUsageDescription,NSLocationAlwaysUsageDescription,NSLocationWhenInUseUsageDescription,bluetooth-central,fetch,location。 I have added following on didFinishLaunchingWithOptions requestAlwaysAuthorization(),requestWhenInUseAuthorization() . 我在didFinishLaunchingWithOptions requestAlwaysAuthorization(),requestWhenInUseAuthorization()上添加了以下内容。

    //MARK:- Variable initializers
    var devicesManager: KTKDevicesManager!
    var locationManager = CLLocationManager()
    let region = CLBeaconRegion(proximityUUID: NSUUID(uuidString: "********")! as UUID, identifier: "detected")

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

        Kontakt.setAPIKey(“*******************”)

         self.locationManager.delegate = self
        devicesManager = KTKDevicesManager(delegate: self)
//When bluetooth Is on that time
        devicesManager.startDevicesDiscovery(withInterval: 3)

}

//MARK: - KTKDevicesManagerDelegate Method

extension AppDelegate: KTKDevicesManagerDelegate {

    func devicesManager(_ manager: KTKDevicesManager, didDiscover devices: [KTKNearbyDevice]) {

        for device in nearbyDevices {
            if let uniqueID = device.uniqueID {
                print("Detected a beacon \(uniqueID)")
            } else {
                print("Detected a beacon with an unknown unique ID")
                devicesManager.stopDevicesDiscovery()
            }
        }

    }

    func devicesManagerDidFail(toStartDiscovery manager: KTKDevicesManager, withError error: Error?) {

        print("Discovery did fail with error: \(error)")
        devicesManager.stopDevicesDiscovery()
      }

}

//MARK: - Location Manager Method

extension AppDelegate: CLLocationManagerDelegate {

    private func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        print("STATUS CHANGED: \(status.rawValue)")
        if status == .authorizedAlways  {
            print("YAY! Authorized!")
            locationManager.startMonitoring(for: region)

        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        print("update location")

        locationManager.startMonitoring(for: region)
        locationManager.startRangingBeacons(in: region)
   }



    func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) {

        print("The monitored regions are: \(manager.monitoredRegions)")
        locationManager.startMonitoring(for: region)
        locationManager.startRangingBeacons(in: region as! CLBeaconRegion)

    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("Location Manager failed with the following error: \(error)")
    }

    func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {

       //  print("beacons : \(beacons.count)")

        devicesManager.startDevicesDiscovery()
       //So called  didDiscover method of KTKDevicesManagerDelegate method

    }

    func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) {
        print("FAIL!")
    }

    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
        print("Region Entered")

        locationManager.startMonitoring(for: region)
        locationManager.startRangingBeacons(in: region as! CLBeaconRegion)

    }


    func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
        print("Region exited")

        locationManager.startMonitoring(for: region)
        locationManager.startRangingBeacons(in: region as! CLBeaconRegion)

    }

}

You must add and allow location in NSLocationAlwaysUsageDescription .If you are in beacon region that time didEnterRegion method execute and out of beacon region that time didExitRegion method executed, that time we start and stop device discovery. 您必须添加并允许在位置NSLocationAlwaysUsageDescription 。如果你在这段时间标区域didEnterRegion方法执行,并指出,时间标区域的didExitRegion方法执行,到时候我们开始和停止设备发现。 we can able to detect beacon in foreground , background , lock phone and in kill state of application. 我们能够在前景,背景,锁定电话和杀死应用状态中检测信标。

Modify your method as given below. 修改您的方法,如下所示。

    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
            print("Region Entered")
            devicesManager.startDevicesDiscovery()


        }


        func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
            print("Region exited")
            devicesManager.stopDevicesDiscovery()    
        }

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

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