更新下 把我之前寫的demo找到了放到git上面了?地址
最近時間一直在研究ibeacon所以把自己遇到的一些問題寫下來做個筆記蒸走。
參考資料:https://github.com/nixzhu/dev-blog/blob/master/2014-04-23-ios7-ibeacons-tutorial.md
iBeacon是蘋果被允許能在后臺運行的征绎,不論你將應用退出到后臺還是殺死,iBeacon都能激活應用不過只能激活10秒左右,但是這段時間足可以做很多事情了。
一.iBeacon的使用
開始監(jiān)聽你的Ibeacon。
在iOS8里面蘋果改變了地位的開啟方式(iBeacon的使用是基于藍牙和定位的),首先要在工程里的info.plist增加字段NSLocationAlwaysUsageDescription(這個是允許一直在后臺運行的)
接著在程序里添加
`- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status`
{
if (status == kCLAuthorizationStatusAuthorizedAlways) {
[self.locationmanager startMonitoringForRegion:self.beacon1];
}
}`
.h文件
#import<UIKit/UIKit.h>
#import<CoreLocation/CoreLocation.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, 400)];
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"];//初始化監(jiān)測的iBeacon信息
[self.locationmanager requestAlwaysAuthorization];//設置location是一直允許
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
if (status == kCLAuthorizationStatusAuthorizedAlways) {
[self.locationmanager startMonitoringForRegion:self.beacon1];//開始MonitoringiBeacon
}
}
{
//發(fā)現(xiàn)有iBeacon進入監(jiān)測范圍
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{
[self.locationmanager startRangingBeaconsInRegion:self.beacon1];//開始RegionBeacons
}
//找的iBeacon后掃描它的信息
- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region{
//如果存在不是我們要監(jiān)測的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;
}
二.ibeacon的參數(shù)
uuid唯一標識此類iBeacon躺翻。
proximity遠近范圍的,有Near(在幾米內(nèi))卫玖,Immediate(在幾厘米內(nèi))公你,F(xiàn)ar(超過 10 米以外,不過在測試中超不過10米就是far)假瞬,Unknown(無效)
major和minor組合后區(qū)分同一類型下的iBeacon陕靠。
accuracy和iBeacon的距離
rssi信號輕度為負值迂尝,越接近0信號越強,等于0時無法獲取信號強度
三.碎碎念
當進入iBeacon范圍是會觸發(fā)didEnterRegion方法剪芥,此時可能獲取不到iBeacon的rssi ,proximity,accuracy值因為距離有點遠垄开,所一要在此時做些動作和這三個參數(shù)有關的話需要小心。