我正在使用下面的代码连接到我的信标。
class ViewController: UIViewController, CLLocationManagerDelegate {
var locationManager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
print("viewDidLoad==ViewController")
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
print("didChangeAuthorization")
if status == .authorizedAlways {
if CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self) {
if CLLocationManager.isRangingAvailable() {
startScanning()
}
}
}
}
func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
print("didRangeBeacons===\(beacons.count)")
if beacons.count > 0 {
print("basss---\(beacons[0].proximityUUID)")
updateDistance(beacons[0].proximity)
} else {
updateDistance(.unknown)
}
}
func updateDistance(_ distance: CLProximity) {
print("updateDistance")
UIView.animate(withDuration: 0.8) {
switch distance {
case .unknown:
self.view.backgroundColor = UIColor.gray
case .far:
self.view.backgroundColor = UIColor.blue
case .near:
self.view.backgroundColor = UIColor.orange
case .immediate:
self.view.backgroundColor = UIColor.red
}
}
}
func startScanning() {
// 39316, 54420, 5268
let uuid = UUID(uuidString: "86477363-EAB1-4988-AA99-B5C1517008D9")!
let beaconRegion = CLBeaconRegion(proximityUUID: uuid, major: 1, minor: 52681, identifier: "MyBeacon")
locationManager.startMonitoring(for: beaconRegion)
locationManager.startRangingBeacons(in: beaconRegion)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
这是工作代码。
但是我想列出附近所有的信标。
有什么想法吗?
当前的问题是我告诉要在startScanning
中搜索哪个信标。
我要做的是搜索范围内的所有信标并显示它们。
答案 0 :(得分:0)
您可以将区域定义更改为不指定主要或次要:
let beaconRegion = CLBeaconRegion(proximityUUID: uuid, identifier: "MyBeacon")
然后,这将匹配所有带有该UUID的信标,而不论主要和次要。
如果要匹配更多UUID,则可以构造多达20个不同的区域,每个区域具有不同的UUID,并监视所有区域。 (请确保也更改每个区域的标识符参数。)
但是,可以监视的区域数量有限制(注册用于监视的第二个区域无效)。可以设置的区域数量没有严格限制,但是一旦超过100个左右,应用程序的性能就会大大降低。
不幸的是,无论UUID如何,在iOS上都无法设置与所有信标匹配的区域。尽管这是一个愚蠢的IMO,但这是Apple设计的安全限制。 Android,MacOS,Linux和Windows没有这种限制。