- iOS 13.0+
- Xcode 11.2+
- Core Location
Beacons make location-based products and services available to users by broadcasting information to your device. Ranging is the process of reading the characteristics of a beacon region, such as signal strength, advertising interval, and measured power.
This sample code project configures a device to act as a beacon, and configures a device to use ranging to find surrounding beacons. Use two iOS devices to run the sample, with one acting as a beacon, and the other ranging for the beacon.
Configure a Device to Act as a Beacon
Run the sample app on the first iOS device. Select the option to Configure a Beacon. The project hard-codes a default value for the UUID that can be changed in
Optionally, modify the major and minor value for the beacon, then select the Enabled switch on the configuration screen to start advertising.
Configure contains a view controller object that configures the iOS device running this app to act as a beacon. The
configure method sets up the region and starts advertising itself.
Configure a Device to Range for Beacons
Using a second iOS device, run the sample app and tap Range for Beacons to scan for beacons. Add a UUID to range for by tapping the Add button in the upper corner of the screen. The hard-coded UUID appears by default.
Range contains a view controller object that scans for nearby beacon regions that the user adds. As in any location-based service, first request authorization. Use a
CLLocation instance to request that authorization, set up the constraint based on the hard-coded UUID, and tell the instance to start monitoring.
When the device enters the specified region, the
location delegate method receives the region state and starts ranging beacons.
While one or more beacons are in range, the
location delegate method receives their characteristics in the passed array.