What is iBeacon?
iBeacon
It is a new feature equipped on OS (iOS7) for mobile devices released by Apple in September 2013. It works by using devices equipped with Bluetooth Low Energy (BLE) communicationBLE
The technology sends its own unique ID to the surroundings, and the application software that receives the ID will take some action based on the ID.
From a personal perspective: iBeacon
Broadcasting signals in all directions is like throwing a stone onto a calm water surface, causing layers of ripples (commonly known as water waves), with the peaks equivalent to iBeaconRSSI
(Accept signal intensity indication), the closer to the center point, the higher the peak (the larger the RSSI), the size of this peak (the value of RSSI) is affected by the force (transmission power) and water quality (ambient environmental factor) when throwing stones. The farther away from the center point, the more the water wave tends to calm. If it exceeds a certain value, the water wave will disappear invisible. In other words, the distance broadcast by iBeacon is ranged. If it exceeds this range, it will not be able to accept the iBeacon signal.
From the perspective of iOS developers:iBeacon inCoreLocation
The abstraction in the framework isCLBeacon
Class, this class has 6 properties, namely:
-
proximityUUID
, is aNSUUID
, used to identify a company. Every company and organization uses the same iBeaconproximityUUID
。 -
major
, the main value is used to identify a set of associated beacons. For example, in the supermarket chain scenario, each branch's beacon should have the samemajor
。 -
minor
, secondary value, is used to distinguish a specific beacon. -
proximity
, far and near range, an enum value. -
accuracy
, distance from iBeacon. -
rssi
, the signal is slightly negative, the closer it is to 0, the stronger the signal is. When it is equal to 0, the signal strength cannot be obtained.
typedef NS_ENUM(NSInteger, CLProximity) { CLProximityUnknown,// Invalid CLProximityImmediate,// Within a few centimeters CLProximityNear,// Within a few meters CLProximityFar//It is more than 10 meters away, but it is far if it exceeds 10 meters in the test}
Tip:proximityUUID
,major
,minor
These three attributes make upiBeacon
unique identifier of .
Just enteriBeacon
The range of the app can be awakened (about 10 seconds), even if the program is killed. If necessary, you can use itUIApplication
Classic- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:(void (^)(void))handler;
Method, request more background execution time.
Uses of iBeacon: We can useiBeacon
You can perform indoor positioning (garage, shopping mall), smart check-in, and remind (when leaving an object, such as leaving home).
The difference between iBeacon and BLE
iBeacon in iOS is a micro-positioning technology based on geographic location, although it uses the help of mobile phone Bluetooth for reception.Majro
、Minor
, but they have no relationship in development engineering.
iBeacon
Use Apple to provideCoreLocation
library, however, used in BLE during developmentCoreBluetooth
library. It is very clear from the libraries provided above, especially if you want to use them after iOS 8.0iBeacon
, the user must click whether to allow itXXapp
Use geographic location. If you use the iOS App for the first timeiBeacon
It is impossible to receive this sentence without prompting itiBeacon
signal (unless under iOS 8.0). If it is BLE, the user needs to be prompted to turn on Bluetooth during the development process, and no other geographical location information is required.
The use of iBeacon in iOS
Permission request
existAdded in
NSLocationAlwaysAndWhenInUseUsageDescription
,NSLocationWhenInUseUsageDescription
,NSLocationAlwaysUsageDescription
, request geolocation permissions.
OpenBackground Modes
Related Codes
import <CoreLocation/>
。
initializationlocationManager
andbeaconRegion
。
- (CLLocationManager *)locationManager { if (!_locationManager) { _locationManager = [[CLLocationManager alloc] init]; _locationManager.delegate = self; } return _locationManager; } - (CLBeaconRegion *)beaconRegion { if (!_beaconRegion) { _beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:Beacon_Device_UUID] identifier:@"test"]; _beaconRegion.notifyEntryStateOnDisplay = YES; } return _beaconRegion; }
CLBeaconRegion
Class, provides 3 initialization methods:
// Listen to all Beacon devices under this UUID- (instancetype)initWithProximityUUID:(NSUUID *)proximityUUID identifier:(NSString *)identifier; // Listen to all Beacon devices under the UUID, major- (instancetype)initWithProximityUUID:(NSUUID *)proximityUUID major:(CLBeaconMajorValue)major identifier:(NSString *)identifier; // Listen to the only Beacon device- (instancetype)initWithProximityUUID:(NSUUID *)proximityUUID major:(CLBeaconMajorValue)major minor:(CLBeaconMinorValue)minor identifier:(NSString *)identifier;
Before we start monitoring, we need to useisMonitoringAvailableForClass
Determine whether the device supports it and whether it allows access to the geographical location.
BOOL availableMonitor = [CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]]; if (availableMonitor) { CLAuthorizationStatus authorizationStatus = [CLLocationManager authorizationStatus]; switch (authorizationStatus) { case kCLAuthorizationStatusNotDetermined: [ requestAlwaysAuthorization]; break; case kCLAuthorizationStatusRestricted: case kCLAuthorizationStatusDenied: NSLog(@"Restricted or rejected"); break; case kCLAuthorizationStatusAuthorizedAlways: case kCLAuthorizationStatusAuthorizedWhenInUse:{ [ startRangingBeaconsInRegion:]; [ startMonitoringForRegion:]; } break; } } else { NSLog(@"This device does not support CLBeaconRegion area detection"); }
Monitoring method
Two ways to detect areasMonitoring
orRanging
Way
Monitoring: Can be used to get notifications when a device enters/exits a geographic area. This method can detect iBeacon when running in the background of the application, but can only detect 20 region areas at the same time, and it cannot infer the distance between the device and the iBeacon.
// Start detection area[ startMonitoringForRegion:beaconRegion]; // Stop detection area[ stopMonitoringForRegion:beaconRegion]; // Monitoring successfully corresponds to the callback function- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region; // Callback when the device enters this area- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region; // Callback when the device exits the area- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region; // Callback when Monitoring error occurs- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(nullable CLRegion *)region withError:(NSError *)error;
Ranging: Can be used to detect all iBeacons in a certain area.
// Start detection area[ startRangingBeaconsInRegion:beaconRegion]; // Stop detection area[ stopRangingBeaconsInRegion:beaconRegion]; // Ranging successfully corresponds to the callback function- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray<CLBeacon *> *)beacons inRegion:(CLBeaconRegion *)region // Callback when Ranging error occurs- (void)locationManager:(CLLocationManager *)manager rangingBeaconsDidFailForRegion:(CLBeaconRegion *)region withError:(NSError *)error
After the process kills, enter the callback in the iBeacon area
// After the program is killed, enter the ibeacon area, or lock the screen/unlock it when the program is running. This function will be called back- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
Buy more background time
If necessary, you can use itUIApplication
Classic- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:(void (^)(void))handler;
Method, request more background execution time.
[Simulate iBeacon with iPhone]
Any iOS device that supports sharing data using Bluetooth Low Energy can be used asiBeacon
。
import <CoreBluetooth/>
and<CoreLocation/>
existterminal
Used inuuidgen
Command to generate a UUID063FA845-F091-4129-937D-2A189A86D844
。
Actually useBLE
It is very simple to simulate the sending of a beacon device.
Related Codes
initializationperipheralManager
= [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:nil];
Send a signal
NSUUID *proximityUUID = [[NSUUID alloc] initWithUUIDString:]; //Create beacon areaCLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:proximityUUID major: minor: identifier:@"test"]; NSDictionary *beaconPeripheraData = [beaconRegion peripheralDataWithMeasuredPower:nil]; if(beaconPeripheraData) { [ startAdvertising:beaconPeripheraData];;//Start broadcast}
Stop broadcasting
[ stopAdvertising];
Note
- Access to the location permission is required.
- The device needs to turn on Bluetooth.
- Use iOS devices to simulate beacon signals, and the Home cannot send signals after it goes out.
The above is the detailed content of how to use IBeacon in IOS. For more information about IBeacon in IOS, please follow my other related articles!