Search code examples
swiftbeacon

list all beacons nearby you


I am using below code to connect to my beacon.

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.
    }


}

This is working code.

However I want to list all nearby beacons.

Any idea how to do this?


The problem in current is I am telling which beacon to search in startScanning.

What I want to do is search all beacons which are there in range and display them.


Solution

  • You can change your region definition to not specify major or minor:

    let beaconRegion = CLBeaconRegion(proximityUUID: uuid,  identifier: "MyBeacon")
    

    This will then match all beacons with that UUID regardless of major and minor.

    If you want to match more UUIDs, you can construct up to 20 different regions, each with a different UUID and monitor and range on all of them. (Be sure to change your identifier parameter for each region, too.)

    However, there is a limit on the number of regions you can monitor (The 21st region registered for monitoring will have no effect.). There is no hard limit on the number of regions you can range, but once you get beyond 100 or so, your app performance will reduce significantly.

    Unfortunately, it is not possible on iOS to set up a region that matches all beacons regardless of UUID. This is an Apple security restriction by design, albeit a silly one IMO. Android, MacOS, Linux and Windows have no such restriction.