列出您附近的所有信标
list all beacons nearby you
我正在使用以下代码连接到我的信标。
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
中搜索哪个信标。
我想做的是搜索范围内的所有信标并显示它们。
您可以将区域定义更改为不指定主要或次要:
let beaconRegion = CLBeaconRegion(proximityUUID: uuid, identifier: "MyBeacon")
然后这将匹配所有具有该 UUID 的信标,无论主要还是次要。
如果你想匹配更多的UUID,你可以构建最多20个不同的区域,每个区域有不同的UUID,并在所有区域上进行监控和范围。 (一定要更改每个区域的标识符参数。)
但是,您可以监控的区域数量是有限制的(注册监控的第21个区域将无效。)。您可以范围的区域数量没有硬性限制,但一旦超过 100 个左右,您的应用性能将显着降低。
遗憾的是,无法在 iOS 上设置匹配所有信标的区域,而不管 UUID。这是 Apple 设计的安全限制,尽管是一个愚蠢的 IMO。 Android、MacOS、Linux和Windows没有这样的限制。
我正在使用以下代码连接到我的信标。
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
中搜索哪个信标。
我想做的是搜索范围内的所有信标并显示它们。
您可以将区域定义更改为不指定主要或次要:
let beaconRegion = CLBeaconRegion(proximityUUID: uuid, identifier: "MyBeacon")
然后这将匹配所有具有该 UUID 的信标,无论主要还是次要。
如果你想匹配更多的UUID,你可以构建最多20个不同的区域,每个区域有不同的UUID,并在所有区域上进行监控和范围。 (一定要更改每个区域的标识符参数。)
但是,您可以监控的区域数量是有限制的(注册监控的第21个区域将无效。)。您可以范围的区域数量没有硬性限制,但一旦超过 100 个左右,您的应用性能将显着降低。
遗憾的是,无法在 iOS 上设置匹配所有信标的区域,而不管 UUID。这是 Apple 设计的安全限制,尽管是一个愚蠢的 IMO。 Android、MacOS、Linux和Windows没有这样的限制。