IBeacon introduction

IBeacon is a new feature in iOS7 that allows nearby handheld devices to detect a bluetooth signal emitted by an iBeacon transmitter. It uses Bluetooth Low Energy (BLE) technology based on Bluetooth 4.0 and is mainly used as an auxiliary indoor positioning function.

IBeacon principle

There are two roles in iBeacon: transmitter: generally a variety of hardware receiver: generally an intelligent terminal (mobile phone) transmitter broadcasts data packets at a certain interval through THE BLE advertising communication channel (usually two or three times per second), and receiver can receive them through the functions provided by the terminal to achieve information interaction. Each signal carries at least three Major information :UUID, Major and Minor, which constitute the unique identifier of an iBeacon.

When iOS devices receive iBeacon signals, they also have the following important information: RSSI: Signal strength Proximity: Distance between the transmitter and the receiver (not a numerical value, but an integer: unknown, Immediate, Near, Far) accuracy: Level of accuracy

In fact, the transmitter is the hardware to constantly broadcast signals around, and the signal is like water wave diffusion around, the closer to the center of the water wave is higher, that is, the RSSI signal is stronger, and once there is something blocked, the signal will weaken or even disappear, and once it exceeds a certain value, the signal will disappear, which shows iBeacon The range of broadcasting is limited.


After the transmitter, let’s talk about the receiver. The receiver provides two ways to receive iBeacon signals:

  • Monitoring: can be used to get notification when the device enters/exits a geographical region. Using this method, iBeacon can be detected in the background of the application, but only 20 regions can be detected at the same time, and the distance between the device and iBeacon cannot be estimated.
  • RangingIBeacon technology can be used to detect all iBeacons in a certain area and accurately estimate the distance between transmitter and receiver. This is represented by the following four proximity states:

The relevant API

Finally speaking of THE API, this is a bit of a pothole.

  • You need to turn on GPS and Bluetooth.
  • The API of iBeacon is in CoreLocation, but bluetooth must be turned on for iBeacon. In order to determine Bluetooth, CoreBluetooth framework is needed.
  • Monitoring and Ranging are two Monitoring methods that can be used together, but need to be separate from business requirements, and can be tricky to use together.
self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; // Follow the proxyif([self locationManager respondsToSelector: @ the selector (requestAlwaysAuthorization)]) {/ / request user authorization location permissions [self. The locationManager requestAlwaysAuthorization]; }Copy the code

The creation of CLBeaconRegion

CLBeaconRegion *region = [[CLBeaconRegion alloc]initWithProximityUUID:#UUID# identifier: #identifier#];
region.notifyOnExit = YES;
region.notifyOnEntry = YES;
region.notifyEntryStateOnDisplay = YES;
Copy the code

The following are two ways to monitor iBeacon:

/ / Monitoring / / starting test area [self. The locationManager startMonitoringForRegion: beaconRegion]; / / stop the detection area [self locationManager stopMonitoringForRegion: beaconRegion]; // delegate - (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region // Callback when the device enters this region - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region // callback when the device exits this region - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region // callback if an error occurs - (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(nullable CLRegion *)region withError:(NSError *)errorCopy the code
/ / held / / starting test area [self. The locationManager startRangingBeaconsInRegion: beaconRegion]; / / stop the detection area [self locationManager stopRangingBeaconsInRegion: beaconRegion]; - (void)locationManager:(CLLocationManager *)manager; // delegate // a callback function that detects iBeacons in the region didRangeBeacons:(NSArray<CLBeacon *> *)beaconsinRegion:(CLBeaconRegion *) Region // callback if an error occurs - (void)locationManager:(CLLocationManager *)manager rangingBeaconsDidFailForRegion:(CLBeaconRegion *)region withError:(NSError *)errorCopy the code

Precautions (Potholes)

  • Monitoring and Ranging are best not used together, as this log will appear for some reason:
[Client] {"msg":"Fence: onClientEventRegionState, invalid state"."regionState":"0"}
Copy the code

Google also failed to search why. Later, I turned off Monitoring and the log did not appear.

  • The API of iBeacon is in CoreLocation framework, but iBeacon must be on bluetooth, otherwise it will always fail, and Bluetooth will use CoreBluetooth to monitor the status (….).

reference

– if you are interested, please directly go here: developer.apple.com/ibeacon/Get… medium.com/@jerrywang0…