iBeacon開發筆記
2015.10.19
airlocate
=========
airlocate顯示如何使用這個監控范圍clbeaconregions。
代碼還提供了一個例子,你如何能校準和配置iOS設備作為信標corebluetooth。
您可以配置一個iOS設備作為信標如下:
1)獲得兩個iOS設備配備藍牙LE。一個將是目標設備,一個將是一個遠程(校準)設備。
2)負載和啟動這個應用程序在這兩個設備上。
3)通過選擇配置和打開啟用的開關,將目標設備轉為信標。
4)取校準裝置,并將一米距離的目標設備移動。
5)在校準裝置上通過選擇校準校準過程。
6)從表格視圖中選擇目標設備。
7)校準過程將開始。你應該在這一過程中,在這個過程中,從一邊到另一邊的校準裝置的波。
8)當校準過程完成后,它會顯示一個校準的rssI值在屏幕上。
9)在目標設備上,返回到配置屏幕并輸入該值在測量功率下。
注:校準過程是可選的,但建議將微調范圍為您的環境。
您可以配置一個iOS設備沒有校準它不指定測量功率信標。
如果未指定測量功率,CoreLocation默認為預定值。
一旦你設置你的目標設備作為一個燈塔,你可以使用這個應用程序演示燈塔范圍和監測。
要演示范圍,選擇遠程設備。alrangingviewcontroller范圍一套clbeaconregions。
要演示監控,選擇遠程設備監控。almonitoringviewcontroller允許您配置一個clbeaconregion監測。
版權所有(2013)蘋果公司保留所有權利。
零.寫在前面
關于測試:建議下載Estimote的app,作為基站,得到它的UUID,majon,minor參數。
關于設備:iBeacon 使用 Bluetooth LE 技術,所以你必須要有一個內置有低功耗藍牙的 iOS 設備以便與 iBeacon 協同工作。目前這個列表里包含如下一些設備:
測試結果:rssi信號輕度大概到-90,有效距離大概為30m。
用途:藍牙BLE,定位,智能家居等。自己還做了個上班打卡的 app,只有進入有效范圍內才能打卡成功。
如果你不熟悉%20iBeacon,你可能也不熟悉術語%20UUID
、主要值(major%20value)
%20和%20次要值(minor%20value)
。
一個%20iBeacon%20除了是一個低功耗藍牙設備之外什么也不是,它們以特定結構發布信息。這些特定的東西超出本教程的范圍,但要明白的一件重要事情是%20iOS%20之所以能夠監控這些%20iBeacon%20就是基于%20UUID
、主要值
%20和%20次要值
。
UUDID%20是%20Universally%20Unique%20Identifier(通用唯一標識符)的縮寫,它實際上是一個隨機字符串;B558CBDA-4472-4211-A350-FF1196FFE8C8
%20就是一個例子。在%20iBeacon%20的討論范圍里,一個%20UUID%20通常用于表示你的頂層標識。作為開發者如果你生成一個%20UUID%20并將其分配給你的%20iBeacon%20設備,那么當一個設備檢測到你的%20iBeacon%20時,它就知道它是在和哪個%20iBeacon%20通信。
主要值與次要值在%20UUID%20之上提供了稍多的粒度。這些值只是%2016%20位無符號整數,能夠標識每個單獨的%20iBeacon%20,甚至是具有同樣%20UUID%20的哪些。
舉個例子,如果你有多間百貨公司,那么你所有的%20iBeacon%20發射器都可有同一個%20UUID%20,但每個店都有它自己的主要值,而里面的每個部門就會有它自己的次要值。你的應用能夠對一個位于你在邁阿密、佛羅里達店的鞋類部們里的%20iBeacon%20做出響應。
開始監聽你的Ibeacon。
在iOS8里面蘋果改變了地位的開啟方式(iBeacon的使用是基于藍牙和定位的),首先要在工程里的info.plist增加字段NSLocationAlwaysUsageDescription(這個是允許一直在后臺運行的)
可能你會有些奇怪 iBeacon 會與 Core Location 相關,畢竟它是藍牙設備,但考慮到 iBeacon 提供微定位信息對應 GPS 提供宏定位信息,也就不奇怪了。在將一個 iOS 設備當作一個iBeacon 而編程時,你就要利用 Core Bluetooth 框架,而在監控 iBeacon 時,你只需同 Core Location 打交道。
當程序運行起來你會發現,設備左下角有你的程序 icon 圖標
.h文件
#import<UIKit/UIKit.h>
#import<CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,CLLocationManagerDelegate>
@PRoperty (nonatomic, strong) NSArray *beaconArr;//存放掃描到的iBeacon
@property (strong, nonatomic) CLBeaconRegion *beacon1;//被掃描的iBeacon
@property (strong, nonatomic) CLLocationManager * locationmanager;
@end
.m文件
#define BEACONUUID @"12334566-7173-4889-9579-954995439125"http://iBeacon的uuid可以換成自己設備的uuid
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 568)];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
self.beaconArr = [[NSArray alloc] init];
self.locationmanager = [[CLLocationManager alloc] init];//初始化
self.locationmanager.delegate = self;
self.beacon1 = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:BEACONUUID] identifier:@"media"];//初始化監測的iBeacon信息
[self.locationmanager requestAlwaysAuthorization];//設置location是一直允許
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
if (status == kCLAuthorizationStatusAuthorizedAlways) {
[self.locationmanager startMonitoringForRegion:self.beacon1];//開始MonitoringiBeacon
}
}
{
//發現有iBeacon進入監測范圍
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{
[self.locationmanager startRangingBeaconsInRegion:self.beacon1];//開始RegionBeacons
}
//找的iBeacon后掃描它的信息
- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region{
//如果存在不是我們要監測的iBeacon那就停止掃描他
if (![[region.proximityUUID UUIDString] isEqualToString:BEACONUUID]){
[self.locationmanager stopMonitoringForRegion:region];
[self.locationmanager stopRangingBeaconsInRegion:region];
}
//打印所有iBeacon的信息
for (CLBeacon* beacon in beacons) {
NSLog(@"rssi is :%ld",beacon.rssi);
NSLog(@"beacon.proximity %ld",beacon.proximity);
......
}
self.beaconArr = beacons;
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.beaconArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ident = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ident];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ident];
}
CLBeacon *beacon = [self.beaconArr objectAtIndex:indexPath.row];
cell.textLabel.text = [beacon.proximityUUID UUIDString];
NSString *str;
switch (beacon.proximity) {
case CLProximityNear:
str = @"近";
break;
case CLProximityImmediate:
str = @"超近";
break;
case CLProximityFar:
str = @"遠";
break;
case CLProximityUnknown:
str = @"不見了";
break;
default:
break;
}
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ %ld %@ %@",str,beacon.rssi,beacon.major,beacon.minor];
return cell;
}
//一些錯誤處理,因為你正在同非常具體的硬件特性打交道,你需要知道任何原因導致的監控和測距失敗
- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error { NSLog(@"Failed monitoring region: %@", error);}- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"Location manager failed: %@", error);}
uuid唯一標識此類iBeacon。
proximity遠近范圍的,有Near(在幾米內),Immediate(在幾厘米內),Far(超過 10 米以外,不過在測試中超不過10米就是far),Unknown(無效)
major和minor組合后區分同一類型下的iBeacon。
accuracy和iBeacon的距離
rssi信號輕度為負值,越接近0信號越強,等于0時無法獲取信號強度
三.通知
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region { if ([region isKindOfClass:[CLBeaconRegion class]]) { UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.alertBody = @"Are you forgetting something?"; notification.soundName = @"Default"; [[UIapplication sharedApplication] presentLocalNotificationNow:notification]; }}
你的位置管理器將在你離開某個區域時調用上面的方法,這就是這個應用有用的時刻。你不需要在你接近你的電腦包時被告知,只需在你離開它太遠時通知你。
此處你檢查區域是否是一個 CLBeaconRegion
,因為如果你同時也在執行地理定位區域監視的話,它還可能是一個 CLCircularRegion
。然后你就發送一個本地通知,附帶一個消息“Are you forgetting something?” 。
編譯并運行你的應用;離開某個你的注冊的 iBeacon,然后一旦你離開得足夠遠,你就會看到通知彈出來。
參考地址:https://github.com/nixzhu/dev-blog/blob/master/2014-04-23-ios7-ibeacons-tutorial.md
新聞熱點
疑難解答