iOS iBeacon基站开发

iBeacon在iOS 7之后的版本中有内置库,直接引入就可以使用

#import//自身作为基站

#import//作为连接基站的设备

首先对几个重要参数作一下说明:

uuid 唯一标识,唯一标识此类iBeacon

major 主要值

minor 次要值

主要值与次要值能够使你区分使用相同UUID的不同iBeacon设备。(在将手机模拟为iBeacon广播时,可将一些信息作为major或者minor广播)注意major与minor为16 位无符号整数。

proximity 远近范围,包含三种情况:

CLProximityFar 10米以外

CLProximityImmediate 几米范围之内

CLProximityNear 几厘米范围内

rssi 信号强度,为负值,越接近0表示信号强度越大,距离越近

一.自身作为基站

需要遵循协议  CBPeripheralManagerDelegate

声明管理者属性

@property (strong, nonatomic) CBPeripheralManager *peripheraManager;

初始化,queue为nil时会在主线程使用

_peripheraManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];

接下来是两个主要代理。更新状态 ,只有状态可用的时候才能够进行创建服务,发布等等操作

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

if (peripheral.state == CBPeripheralManagerStatePoweredOn) {

NSDictionary *peripheralData = nil;

CLBeaconRegion * region = [[CLBeaconRegion alloc] initWithProximityUUID:UUID major:0 minor:1 identifier:[UUID UUIDString]];

peripheralData = [region peripheralDataWithMeasuredPower:nil];

if(peripheralData)

{

[self.peripheraManager startAdvertising:peripheralData];//开始广播

}

}

}

开始向外广播数据  当startAdvertising被执行的时候调用这个代理方法,可以接收到一些错误信息

- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(NSError *)error{

NSLog(@"Failed advertising: %@", error);

}

二.作为设备连接基站

需要遵循协议  CLLocationManagerDelegate

声明一个属性

@property (strong, nonatomic) CLLocationManager * locationManager;

@property (strong, nonatomic) CLBeaconRegion *beaconRegion;

初始化

self.locationmanager = [[CLLocationManager alloc] init];//初始化

self.locationmanager.delegate = self;

self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID

alloc] initWithUUIDString:UUID]];//初始化监测的iBeacon信息

[self.locationManager requestAlwaysAuthorization];//设置location是一直允许

[self.locationManager startRangingBeaconsInRegion:self.beaconRegion];

代理

错误处理

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

NSLog(@"Failed monitoring region: %@", error);

}

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

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

}

//发现有iBeacon进入监测范围

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

[self.locationManager startRangingBeaconsInRegion:self.beaconRegion];//开始RegionBeacons

}

在 iBeacon 到达范围内、离开范围或某个 iBeacon 的范围改变时被调用。

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

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

if (![[region.proximityUUID UUIDString] isEqualToString:[UUID UUIDString]]){

[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);

}

}




你可能感兴趣的:(iOS iBeacon基站开发)