iOS开发iBeacon蓝牙技术

iBeacon使用的是BLE技术,具体而言,利用的是BLE中名为“通告帧”(Advertising)的广播帧。通告帧是定期发送的帧,只要是支持BLE的设备就可以接收到。iBeacon通过在这种通告帧的有效负载部分嵌入苹果自主格式的数据来实现。

iBeacon的数据主要由四种资讯构成,分别是UUID(通用唯一标识符)、Major、Minor、Measured Power。

UUID是规定为ISO/IEC11578:1996标准的128位标识符。

Major和Minor由iBeacon发布者自行设定,都是16位的标识符。比如,连锁店可以在Major中写入区域资讯,可在Minor中写入个别店铺的ID等。另外,在家电中嵌入iBeacon功能时,可以用Major表示产品型号,用Minor表示错误代码,用来向外部通知故障。

Ibeacon一项低耗能蓝牙技术技术,工作原理类似之前的蓝牙技术,由iBeacon发射信号,IOS设备定位接受,反馈信号。根据这项简单的定位技术可以做出许多的相应技术应用。


#import "ViewController.h"

@interface ViewController ()@property(nonatomic, strong)NSArray *beaconArr;//存放扫描的iBeacon的数组

@property(nonatomic, strong)CLBeaconRegion *beacon; //被扫描的iBeacon

@property(nonatomic, strong)CLLocationManager *locationManager;

@property(nonatomic, strong)UITableView *tableView;

@property (strong, nonatomic) CBPeripheralManager *peripheralManager;

@property (strong, nonatomic) NSDictionary *myBeaconData;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

_beaconArr  = [[NSArray alloc] init];

_locationManager = [[CLLocationManager alloc] init];

_locationManager.delegate = self;

_beacon = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:@"FDA50693-A4E2-4FB1-AFCF-C6EB07647825"] identifier:@"media"];

//_beacon = [[CLBeaconRegion alloc] init];

[_locationManager requestWhenInUseAuthorization];//设置location一直允许

_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 600) style:UITableViewStylePlain];

_tableView.delegate = self;

_tableView.dataSource = self;

[self.view addSubview:_tableView];

BOOL enable = [CLLocationManager locationServicesEnabled];

if (enable) {

if ([CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]])

{

[self.locationManager requestAlwaysAuthorization];

[self.locationManager startMonitoringForRegion:_beacon];

[self.locationManager startRangingBeaconsInRegion:_beacon];

}

}

self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self

queue:nil

options:nil];

}

- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{

if (peripheral.state == CBPeripheralManagerStatePoweredOn)

{

// Bluetooth is on

// Update our status label

//self.statusLabel.text = @"Broadcasting...";

// Start broadcasting

//  [self.peripheralManager startAdvertising:self.myBeaconData];

}

else if (peripheral.state == CBPeripheralManagerStatePoweredOff)

{

// Update our status label

// self.statusLabel.text = @"Stopped";

}

}

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

if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) {

[_locationManager startMonitoringForRegion:_beacon];//开始启动

}

}

//发现有ibeacon进入检测范围

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

[_locationManager startRangingBeaconsInRegion:_beacon];

[_locationManager startMonitoringForRegion:_beacon];

}

-(void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion*)region

{

[_locationManager stopRangingBeaconsInRegion:_beacon];

}

//找到IBeacon后扫描- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray*)beacons inRegion:(CLBeaconRegion *)region{

//如果存在不是我们要检测的IBeacon那就停止扫描他

if (![[region.proximityUUID UUIDString] isEqualToString:@"12334566-7173-4889-9579-954995439125"]) {

[_locationManager stopMonitoringForRegion:region];

[_locationManager stopRangingBeaconsInRegion:region];

}

//打印所有的IBeacon的信息

for (CLBeacon *beacon in beacons) {

NSLog(@"rssi is : %ld", beacon.rssi);

NSLog(@"beacon.proximity %ld", beacon.proximity);

}

_beaconArr = beacons;

[_tableView reloadData];

}

- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error

{

NSLog(@"monitoringDidFailForRegion %@ %@",region, error.localizedDescription);

for (CLRegion *monitoredRegion in manager.monitoredRegions) {

NSLog(@"monitoredRegion: %@", monitoredRegion);

}

if ((error.domain != kCLErrorDomain || error.code != 5) &&

[manager.monitoredRegions containsObject:region]) {

NSString *message = [NSString stringWithFormat:@"%@ %@",

region, error.localizedDescription];

NSLog(@"%@", message);

//  [AlertView alert:@"monitoringDidFailForRegion" message:message];

}

}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {

NSLog(@"Location manager failed: %@", error);

}

- (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)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容

  • @end 与之前一样,你需要初始化位置管理器并设置它们的 delegate 。 在 application:did...
    LiWeiJ阅读 2,109评论 0 0
  • 2015.10.19 airlocate 本文摘抄加个人总结 ========= airlocate显示如何使用这...
    Puiwah_Wai阅读 5,335评论 2 5
  • 更新下 把我之前写的demo找到了放到git上面了地址 最近时间一直在研究ibeacon所以把自己遇到的一些问题写...
    沙瓦迪卡阅读 21,284评论 53 55
  • iBeacon在iOS 7之后的版本中有内置库,直接引入就可以使用 #import<CoreLocation/Co...
    ___Lynn阅读 1,984评论 0 0
  • 今日观点: 第一,抓大的意思是只管大事。 思考:老板、中层管理、员工好比是金字塔式形状,老板站在金字塔最高处,员工...
    杨雪雪阅读 194评论 0 0