我正在使用下面的代码行来创建 CLBeaconRegion
,然后按以下方式调用 location manager
:
let beaconRegion:CLBeaconRegion = CLBeaconRegion(proximityUUID: beaconUUID as UUID,
identifier: beaconIdentifier)
/// save a beacon region for stopping after.
(beaconInfo as! BeaconInfo).beaconRegion = beaconRegion
/// start monitoring the specified region.
self.locationManager.startMonitoring(for: beaconRegion)
/// start calculating ranges for beacons in the specified region.
self.locationManager.startRangingBeacons(in: beaconRegion)
位置管理器委托(delegate)中的代码是:
func locationManager(_ manager: CLLocationManager,
didRangeBeacons beacons: [CLBeacon],
in region: CLBeaconRegion) {
/// it may be assumed no beacons that match the specified region are nearby.
if beacons.count == 0 {
print ("beacon.count = 0")
return
}
else
{
\\executed rest of the code
}
其中 beaconUUID 是我的 iBeacon 的 UUID,beaconIdentifier 是标识符字符串。问题是因为我的应用程序将检测通用 iBeacon,所以我可能不知道它们的 beaconIdentifier 字符串(不是 UUID,只是一个标识符)。有什么方法可以将其作为通用字符串或 nil 传递(以消除依赖性)?
编辑:添加了 CLLocationManager 代码
最佳答案
不,您需要知道您正在寻找的信标的 UUID。 iOS 无法扫描“所有信标”。
标识符字符串只是您分配给应用程序中的 CLBeaconRegion
的字符串。它与实际信标中配置的任何值无关。
所以
let beaconRegion:CLBeaconRegion = CLBeaconRegion(proximityUUID: beaconUUID as UUID, identifier: "someIdentifier")
和
let beaconRegion:CLBeaconRegion = CLBeaconRegion(proximityUUID: beaconUUID as UUID, identifier: "someOtherIdentifier")
都将扫描同一组 iBeacon;具有 beaconUUID
您可以在以后使用该标识符来识别信标区域,例如当进入该区域或您想停止监视某个区域时。请记住,信标区域在您的应用程序的执行过程中保持事件状态,因此您不能依赖对象引用。
关于ios - 如何在没有信标标识符的情况下找到 iBeacon?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42822388/