您的位置:首頁技術文章
文章詳情頁

如何在IOS中使用IBeacon

瀏覽:59日期:2022-09-16 17:16:52
什么是iBeacon?

iBeacon 是蘋果公司2013年9月發布的移動設備用OS(iOS7)上配備的新功能。其工作方式是,配備有低功耗藍牙(BLE)通信功能的設備使用BLE技術向周圍發送自己特有的 ID,接收到該 ID 的應用軟件會根據該 ID 采取一些行動。

從個人的角度看: iBeacon向四面八方不停地廣播信號,就像是往平靜的水面上扔了一塊石子,泛起層層漣漪(俗稱水波),波峰相當于 iBeacon 的RSSI(接受信號強度指示),越靠近中心點的地方波峰越高(RSSI 越大),這個波峰的大小(RSSI 的值)受到扔石子時用力大小(發射功率)和水質(周圍環境因子)的影響,離中心點越遠水波越趨向于平靜,超過了一定值,水波會消失于無形,也就是說 iBeacon 向外廣播的距離是有范圍的,超過了這個范圍,將接受不到 iBeacon 的信號。

從iOS開發者的角度看: iBeacon 在 CoreLocation 框架中抽象為CLBeacon類, 該類有6個屬性,分別是:

proximityUUID,是一個 NSUUID,用來標識公司。每個公司、組織使用的 iBeacon 應該擁有同樣的 proximityUUID。 major,主要值,用來識別一組相關聯的 beacon,例如在連鎖超市的場景中,每個分店的 beacon 應該擁有同樣的 major。 minor,次要值,則用來區分某個特定的 beacon。 proximity,遠近范圍的,一個枚舉值。

typedef NS_ENUM(NSInteger, CLProximity) {CLProximityUnknown,// 無效CLProximityImmediate,//在幾厘米內CLProximityNear,//在幾米內CLProximityFar//超過 10 米以外,不過在測試中超不過10米就是far}accuracy,與iBeacon的距離。 rssi,信號輕度為負值,越接近0信號越強,等于0時無法獲取信號強度。

Tip:proximityUUID,major,minor 這三個屬性組成 iBeacon 的唯一標識符。

只要進入iBeacon的范圍,就能喚醒 App(大約10秒鐘),即使在程序被殺掉的情況下。必要時,可以使用UIApplication類的- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:(void (^)(void))handler;方法,請求更多的后臺執行時間。

iBeacon的用途:我們可以用iBeacon可以進行室內定位(車庫,商場),智能打卡,提醒(離開某物體的時候,比如離開家)。

iBeacon 與 BLE 的區別

iOS 中 iBeacon 是基于地理位置的微定位技術,雖然借助手機藍牙進行接收Majro、Minor,但是他們在開發工程中沒有任何關系。

iBeacon使用蘋果提供CoreLocation庫,然而在 BLE 在開發過程中使用CoreBluetooth庫。從上面提供的庫來看就很清楚了,特別是在 iOS8.0 之后的時候如果想使用iBeacon,必須讓用戶點擊是否允許XXapp使用地理位置。如果在第一次使用 iOS App 掃描iBeacon的時候沒有提示這句話,是不可能接收到iBeacon的信號(除非iOS 8.0之下)。如果是 BLE 則的開發過程中之需要提示用戶打開藍牙,并不要求其他的地理位置任何信息。

iBeacon 在 iOS 中的運用權限請求

在info.plist中添加NSLocationAlwaysAndWhenInUseUsageDescription,NSLocationWhenInUseUsageDescription,NSLocationAlwaysUsageDescription,請求地理位置權限。

開啟Background Modes

相關代碼

import <CoreLocation/CoreLocation.h>。

初始化locationManager和beaconRegion。

- (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類,提供了3個初始化方法:

//監聽該UUID下的所有Beacon設備- (instancetype)initWithProximityUUID:(NSUUID *)proximityUUID identifier:(NSString *)identifier;//監聽該UUID,major下的所有Beacon設備- (instancetype)initWithProximityUUID:(NSUUID *)proximityUUID major:(CLBeaconMajorValue)major identifier:(NSString *)identifier;//監聽唯一的Beacon設備- (instancetype)initWithProximityUUID:(NSUUID *)proximityUUID major:(CLBeaconMajorValue)major minor:(CLBeaconMinorValue)minor identifier:(NSString *)identifier;

在開始監控之前,我們需要使用isMonitoringAvailableForClass判斷設備是否支持,是否允許訪問地理位置。

BOOL availableMonitor = [CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]];if (availableMonitor) { CLAuthorizationStatus authorizationStatus = [CLLocationManager authorizationStatus]; switch (authorizationStatus) {case kCLAuthorizationStatusNotDetermined: [self.locationManager requestAlwaysAuthorization];break;case kCLAuthorizationStatusRestricted:case kCLAuthorizationStatusDenied: NSLog(@'受限制或者拒絕');break;case kCLAuthorizationStatusAuthorizedAlways:case kCLAuthorizationStatusAuthorizedWhenInUse:{ [self.locationManager startRangingBeaconsInRegion:self.beaconRegion]; [self.locationManager startMonitoringForRegion:self.beaconRegion];}break; }} else { NSLog(@'該設備不支持 CLBeaconRegion 區域檢測');}監聽方式

可用兩種方式檢測區域Monitoring或Ranging方式

Monitoring:可以用來在設備進入/退出某個地理區域時獲得通知, 使用這種方法可以在應用程序的后臺運行時檢測 iBeacon,但是只能同時檢測 20 個 region 區域,并且不能夠推測設備與 iBeacon 的距離。

// 開始檢測區域[self.locationManager startMonitoringForRegion:beaconRegion]; // 停止檢測區域[self.locationManager stopMonitoringForRegion:beaconRegion]; // Monitoring成功對應回調函數- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region;// 設備進入該區域時的回調- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region;// 設備退出該區域時的回調- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region;// Monitoring有錯誤產生時的回調- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(nullable CLRegion *)region withError:(NSError *)error;

Ranging:可以用來檢測某區域內的所有 iBeacons。

// 開始檢測區域[self.locationManager startRangingBeaconsInRegion:beaconRegion];// 停止檢測區域[self.locationManager stopRangingBeaconsInRegion:beaconRegion];// Ranging成功對應回調函數- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray<CLBeacon *> *)beacons inRegion:(CLBeaconRegion *)region // Ranging有錯誤產生時的回調- (void)locationManager:(CLLocationManager *)manager rangingBeaconsDidFailForRegion:(CLBeaconRegion *)region withError:(NSError *)error進程 kill 之后,進入 iBeacon 區域的回調

// 當程序被殺掉之后,進入ibeacon區域,或者在程序運行時鎖屏/解鎖 會回調此函數- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region爭取更多的后臺時間

必要時,可以使用UIApplication類的- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:(void (^)(void))handler;方法,請求更多的后臺執行時間。

[用 iPhone 手機模擬 iBeacon]

任何支持使用藍牙低功耗共享數據的 iOS 設備都可以用作 iBeacon。

import <CoreBluetooth/CoreBluetooth.h>和<CoreLocation/CoreLocation.h>

在terminal中使用uuidgen命令,生成一個 UUID 063FA845-F091-4129-937D-2A189A86D844。

其實利用BLE來模擬 beacon 設備發送信號,很簡單。

相關代碼

初始化peripheralManager

self.peripheralManager= [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:nil];

發送信號

NSUUID *proximityUUID = [[NSUUID alloc] initWithUUIDString:self.UUIDTextField.text];//創建beacon區域CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:proximityUUID major:self.majorTextField.text.integerValue minor:self.minorTextField.text.integerValue identifier:@'test'];NSDictionary *beaconPeripheraData = [beaconRegion peripheralDataWithMeasuredPower:nil];if(beaconPeripheraData) { [self.peripheralManager startAdvertising:beaconPeripheraData];;//開始廣播}

停止廣播

[self.peripheralManager stopAdvertising];注意點 需要訪問地理位置權限。 設備需要開啟藍牙。 利用 iOS 設備模擬 beacon信號,Home 出去之后是不能發送信號的。

以上就是如何在IOS中使用IBeacon的詳細內容,更多關于IOS中IBeacon的資料請關注好吧啦網其它相關文章!

標簽: IOS
相關文章:
国产综合久久一区二区三区