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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

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