【问题标题】:beacon detecting in foreground, background and kill (remove from background) app在前台、后台和杀死(从后台删除)应用程序中检测信标
【发布时间】:2019-01-28 07:44:13
【问题描述】:

我正在处理前台、后台和杀死(从后台删除)应用程序中的信标检测。我有kontakt beacon。我已经实现了位置管理器委托方法,但我无法检测前台和后台的信标,甚至没有调用方法 didEnterRegion 和 didExitRegion 。对于解决方案,我将位置管理器委托与 KTKDevicesManagerDelegate 合并。我无法在位置管理器中检测到信标,因此我尝试在位置管理器方法的 didRangeBeacons 中实现我开始 devicesManager.startDevicesDiscovery() 的 KTKDeviceManagerDelegate 那个时候在前台和后台检测信标,但是当我们杀死应用程序时我无法检测到信标。我已经在 Info.plist 中添加了 NSBluetoothPeripheralUsageDescription 、 NSLocationAlwaysUsageDescription 、 NSLocationWhenInUseUsageDescription 、 bluetooth-central 、 fetch 、 location 。 我在 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)

    }

}

【问题讨论】:

标签: swift ibeacon locationmanager kontakt.io


【解决方案1】:

您必须在NSLocationAlwaysUsageDescription 中添加并允许位置。如果您在didEnterRegion 方法执行时位于信标区域,并且在didExitRegion 方法执行时不在信标区域,则我们开始和停止设备发现。我们可以在前台、后台、锁定手机和应用程序的终止状态中检测信标。

如下所示修改您的方法。

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


        }


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

【讨论】:

  • 非常感谢。你节省了我很多时间,最后它正在工作。干得好
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多