iOS開(kāi)發(fā) 關(guān)于iBeacon的一些記錄

更新下 把我之前寫(xiě)的demo找到了放到git上面了?地址

最近時(shí)間一直在研究ibeacon所以把自己遇到的一些問(wèn)題寫(xiě)下來(lái)做個(gè)筆記。

參考資料:https://github.com/nixzhu/dev-blog/blob/master/2014-04-23-ios7-ibeacons-tutorial.md

iBeacon是蘋(píng)果被允許能在后臺(tái)運(yùn)行的,不論你將應(yīng)用退出到后臺(tái)還是殺死,iBeacon都能激活應(yīng)用不過(guò)只能激活10秒左右,但是這段時(shí)間足可以做很多事情了。

一.iBeacon的使用

開(kāi)始監(jiān)聽(tīng)你的Ibeacon。

在iOS8里面蘋(píng)果改變了地位的開(kāi)啟方式(iBeacon的使用是基于藍(lán)牙和定位的),首先要在工程里的info.plist增加字段NSLocationAlwaysUsageDescription(這個(gè)是允許一直在后臺(tái)運(yùn)行的)

接著在程序里添加

`- (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可以換成自己設(shè)備的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)測(cè)的iBeacon信息

[self.locationmanager requestAlwaysAuthorization];//設(shè)置location是一直允許

}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{

if (status == kCLAuthorizationStatusAuthorizedAlways) {

[self.locationmanager startMonitoringForRegion:self.beacon1];//開(kāi)始MonitoringiBeacon

}

}

{

//發(fā)現(xiàn)有iBeacon進(jìn)入監(jiān)測(cè)范圍

-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{

[self.locationmanager startRangingBeaconsInRegion:self.beacon1];//開(kāi)始RegionBeacons

}

//找的iBeacon后掃描它的信息

- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region{

//如果存在不是我們要監(jiān)測(cè)的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 = @"遠(yuǎn)";

break;

case CLProximityUnknown:

str = @"不見(jiàn)了";

break;

default:

break;

}

cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ %ld %@ %@",str,beacon.rssi,beacon.major,beacon.minor];

return cell;

}

二.ibeacon的參數(shù)

uuid唯一標(biāo)識(shí)此類iBeacon。

proximity遠(yuǎn)近范圍的,有Near(在幾米內(nèi)),Immediate(在幾厘米內(nèi)),F(xiàn)ar(超過(guò) 10 米以外,不過(guò)在測(cè)試中超不過(guò)10米就是far),Unknown(無(wú)效)

major和minor組合后區(qū)分同一類型下的iBeacon。

accuracy和iBeacon的距離

rssi信號(hào)輕度為負(fù)值,越接近0信號(hào)越強(qiáng),等于0時(shí)無(wú)法獲取信號(hào)強(qiáng)度

三.碎碎念

當(dāng)進(jìn)入iBeacon范圍是會(huì)觸發(fā)didEnterRegion方法,此時(shí)可能獲取不到iBeacon的rssi ,proximity,accuracy值因?yàn)榫嚯x有點(diǎn)遠(yuǎn),所一要在此時(shí)做些動(dòng)作和這三個(gè)參數(shù)有關(guān)的話需要小心。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容