繁体   English   中英

设置监控后无法在iOS 10中检测iBeacon

[英]Cant detect iBeacon in iOS 10 after setting up monitoring

我一直在使用swift 3和Xcode 8.2.1在iOS 10上检测iBeacons。

我一直在关注教程,到目前为止,我已经能够在前台检测到信标。 但是,我需要在后台以及关闭应用程序时检测到信标。

我已经在info.plist设置了键NSLocationAlwaysUsageDescription ,并且还在应用程序的“ Background Modes添加了Location updates

我还向用户请求requestAlwaysAuthorization

一切正常,直到我在代码中添加以下语句: locationManager.startMonitoring(for: beaconRegion)

添加上述语句并运行代码后,我的应用程序会在前台检测到信标并向我显示一条消息。 但是,一旦我最小化应用程序并重新打开它,该应用程序似乎就找不到信标。 如果我注释掉该行,然后重新运行程序,则该应用程序会检测到前景中的信标以及最小化并再次打开该应用程序时。 我不明白我在做什么错。

这是我的ViewController代码:

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {
    var locationManager: CLLocationManager!

    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.pausesLocationUpdatesAutomatically = false
        locationManager.allowsBackgroundLocationUpdates = true
        locationManager.requestAlwaysAuthorization()
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if status == .authorizedAlways {
            if CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self) {
                if CLLocationManager.isRangingAvailable() {
                    startScanning()
                }
            }
        }
    }

    func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
        if beacons.count > 0 {
            NSLog("Found beacon")
        } else {
            NSLog("Beacon not found")
        }
    }

    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
        let beaconRegion = region as! CLBeaconRegion
        print("Did enter region: " + (beaconRegion.major?.stringValue)!)
    }

    func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
        let beaconRegion = region as! CLBeaconRegion
        print("Did exit region: " + (beaconRegion.major?.stringValue)!)
    }

    func startScanning() {
        let uuid = UUID(uuidString: "2f234454-cf6d-4a0f-adf2-f4911ba9ffa6")!
        let beaconRegion = CLBeaconRegion(proximityUUID: uuid, major: CLBeaconMajorValue(0), minor: CLBeaconMinorValue(1), identifier: "MyBeacon")

        beaconRegion.notifyEntryStateOnDisplay = true
        beaconRegion.notifyOnEntry = true
        beaconRegion.notifyOnExit = true

        //        locationManager.startMonitoring(for: beaconRegion)
        locationManager.startRangingBeacons(in: beaconRegion)
    }
}

这是我的日志:

Found beacon
Found beacon
Found beacon
// App minimised and reopened here
Found beacon
Found beacon
Found beacon
Found beacon
Found beacon
Found beacon
Beacon not found
Beacon not found
Beacon not found
Beacon not found
Beacon not found
Beacon not found

尝试添加监视委托回调didEnter(region: region)didExit(region: region) 不知道为什么会改变事情,有点奇怪的是代码开始监视但没有监视。

编辑:看到更新的代码后,我怀疑问题可能是测距开始的方式。 与其在didChangeAuthorization回调中启动它, viewDidLoadviewDidLoad启动它。 如果未获得授权,该代码将不会崩溃。 只是直到它才真正扫描。

与iBeacons的交互可以通过测距en监视来完成。 信标测距为您提供iBeacon的价值/数据,例如uuid,major和minors。 测距的问题在于,苹果在后台仅接受几个(约10秒)的测距时间。 监视可以在后台进行。 如果是这样,您应该检查是否可以在后台(打印/日志)进行扫描,这是问题所在。

ps:当您开始在区域内进行范围调整时,不会调用didEnter委托方法,因为您已经在该区域中。

暂无
暂无

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

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