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

你可能感兴趣的:(iOS,iOS)