显示“找不到iBeacon”消息。

3

我的问题非常简单。我想在通过按钮按下后,在viewDidLoad中被调用后,通过调用startSearchingForSessions,如果iBeacon监测失败,显示错误消息,即“无法找到iBeacon”。

override func viewDidLoad() {
    super.viewDidLoad()

    self.locationManager = CLLocationManager()
    if self.locationManager.responds(to: #selector(CLLocationManager.requestWhenInUseAuthorization)) {
        self.locationManager.requestWhenInUseAuthorization()
    }
    self.locationManager.delegate = self
    self.locationManager.pausesLocationUpdatesAutomatically = false

    let uuid = UUID(uuidString: "869A6E2E-AE14-4CF5-8313-8D6976058A7A")
    self.beaconRegion = CLBeaconRegion(proximityUUID: uuid!, identifier: "com.dejordan.myapp"
    startSearchingForSessions()

}

func startSearchingForSessions() {

    // Start looking for the beacons with that UUID and Identifier.
    self.locationManager.startMonitoring(for: self.beaconRegion)
    self.locationManager.startRangingBeacons(in: self.beaconRegion)
    self.locationManager.startUpdatingLocation()

}

如此处理发现的信标:
// Required by the Location Manager.
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
    self.locationManager.startRangingBeacons(in: self.beaconRegion)
}

func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) {
    if state == CLRegionState.outside {
        print("Cannot Find Beacon")
    }
}

// Required by the Location Manager.
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
    self.locationManager.stopRangingBeacons(in: self.beaconRegion)
}

// This is called if any beacons are found.
func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {

    var result = Array<CLBeacon>()
    for beacon in beacons {
        result.append(beacon)
    }

    foundBeacons = result
    // If we found any, we need to see
    // what class they belong to based on information
    // from Parse.
    self.identifyFoundBeacons()

    // We can stop looking for beacons now.
    self.locationManager.stopMonitoring(for: self.beaconRegion)
    self.locationManager.stopRangingBeacons(in: self.beaconRegion)
    self.locationManager.stopUpdatingLocation()

}

我已经实现了委托错误方法,试图找出发生错误的位置,但是在导航iBeacon的大量文档中,我迄今没有取得任何成果。

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
    print("Location manager failed: \(error.localizedDescription)")
}

func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) {
    print("Failed monitoring region: \(error.localizedDescription)")
}

谢谢!


根据您的代码,func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) { print("Failed monitoring region: (error.localizedDescription)") } 可以正确检测信标监视器失败。 - vivek bhoraniya
你可以使用UIAlertController来展示信息。请确保调用展示UIAlertController的操作在UI线程(主线程)中执行:https://stackoverflow.com/documentation/ios/874/uialertcontroller#t=201706140716504655358 - Duyen-Hoa
你是否曾看到“requestWhenInUseAuthorization()”的对话框并确认已授予权限?另外,你必须打开手机的蓝牙和位置设置。 - davidgyoung
是的,我没有任何问题找到 iBeacon,但我无法捕捉到它“无法”找到 iBeacon 的情况。 - Dane Jordan
令我惊讶的是,原来didRangeBeacons就是我在寻找的方法,尽管它返回一个空数组,但我可以使用其大小来确定是否发现了任何信标。无论如何,感谢您的帮助! - Dane Jordan
2个回答

2
如果您只是想知道何时未检测到信标(而不是出现低级别错误无法查找信标),则可以使用以下委托方法:

如果你只想知道信标未被探测到的时间(与低级别的信标搜索失败相对),那么请使用以下委托方法:

public func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) {
  if state == CLRegionState.outside {
    print("Cannot find beacon")
  }
}

嗯...看起来它没有捕捉到。我之前发布了一个附加在.start函数上的按钮,在几秒钟后每次按下按钮都没有反应。 - Dane Jordan
你能否扩展你的问题,展示一下你设置 LocationManager 和 delegate 的完整代码?你是否请求了位置权限?你能显示一下那段代码吗? - davidgyoung

0
有趣的是,在委托方法中didRangeBeacons
func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion)

被传入一个空的[CLBeacon]数组,我可以使用beacons数组的大小来确定是否找到了任何信标。

虽然不是我预期的结果,但这解决了我的问题!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接