·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> iBeacon开发笔记

iBeacon开发笔记

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

iBeacon开发笔记

 

2015.10.19

airlocate

=========

airlocate显示如何使用这个监控范围clbeaconregions。

代码还提供了一个例子,你如何能校准和配置iOS设备作为信标corebluetooth。

您可以配置一个iOS设备作为信标如下:

1)获得两个iOS设备配备蓝牙LE。一个将是目标设备,一个将是一个远程(校准)设备。

2)负载和启动这个应用程序在这两个设备上。

3)通过选择配置和打开启用的开关,将目标设备转为信标。

4)取校准装置,并将一米距离的目标设备移动。

5)在校准装置上通过选择校准校准过程。

6)从表格视图中选择目标设备。

7)校准过程将开始。你应该在这一过程中,在这个过程中,从一边到另一边的校准装置的波。

8)当校准过程完成后,它会显示一个校准的rssI值在屏幕上。

9)在目标设备上,返回到配置屏幕并输入该值在测量功率下。

注:校准过程是可选的,但建议将微调范围为您的环境。

您可以配置一个iOS设备没有校准它不指定测量功率信标。

如果未指定测量功率,CoreLocation默认为预定值。

一旦你设置你的目标设备作为一个灯塔,你可以使用这个应用程序演示灯塔范围和监测。

要演示范围,选择远程设备。alrangingviewcontroller范围一套clbeaconregions。

要演示监控,选择远程设备监控。almonitoringviewcontroller允许您配置一个clbeaconregion监测。

版权所有(2013)苹果公司保留所有权利。

零.写在前面

关于测试:建议下载Estimote的app,作为基站,得到它的UUID,majon,minor参数。

关于设备:iBeacon 使用 Bluetooth LE 技术,所以你必须要有一个内置有低功耗蓝牙的 iOS 设备以便与 iBeacon 协同工作。目前这个列表里包含如下一些设备:

  • iphone 4s 或更新的
  • 第三代 iPad 或更新的
  • iPad mini 或更新的
  • 第五代iPod touch 或更新的
  • 系统版本7.0以上

测试结果:rssi信号轻度大概到-90,有效距离大概为30m。

用途:蓝牙BLE,定位,智能家居等。自己还做了个上班打卡的 app,只有进入有效范围内才能打卡成功。

UUID、主要、次要标识符

如果你不熟悉 iBeacon,你可能也不熟悉术语 UUID主要值(major value)次要值(minor value)

一个 iBeacon 除了是一个低功耗蓝牙设备之外什么也不是,它们以特定结构发布信息。这些特定的东西超出本教程的范围,但要明白的一件重要事情是 iOS 之所以能够监控这些 iBeacon 就是基于 UUID主要值次要值

UUDID 是 Universally Unique Identifier(通用唯一标识符)的缩写,它实际上是一个随机字符串;B558CBDA-4472-4211-A350-FF1196FFE8C8 就是一个例子。在 iBeacon 的讨论范围里,一个 UUID 通常用于表示你的顶层标识。作为开发者如果你生成一个 UUID 并将其分配给你的 iBeacon 设备,那么当一个设备检测到你的 iBeacon 时,它就知道它是在和哪个 iBeacon 通信。

主要值与次要值在 UUID 之上提供了稍多的粒度。这些值只是 16 位无符号整数,能够标识每个单独的 iBeacon ,甚至是具有同样 UUID 的哪些。

举个例子,如果你有多间百货公司,那么你所有的 iBeacon 发射器都可有同一个 UUID ,但每个店都有它自己的主要值,而里面的每个部门就会有它自己的次要值。你的应用能够对一个位于你在迈阿密、佛罗里达店的鞋类部们里的 iBeacon 做出响应。

一.iBeacon的使用

开始监听你的Ibeacon。

在iOS8里面苹果改变了地位的开启方式(iBeacon的使用是基于蓝牙和定位的),首先要在工程里的info.plist增加字段NSLocationAlwaysUsageDescription(这个是允许一直在后台运行的)

可能你会有些奇怪 iBeacon 会与 Core Location 相关,毕竟它是蓝牙设备,但考虑到 iBeacon 提供微定位信息对应 GPS 提供宏定位信息,也就不奇怪了。在将一个 iOS 设备当作一个iBeacon 而编程时,你就要利用 Core Bluetooth 框架,而在监控 iBeacon 时,你只需同 Core Location 打交道。

当程序运行起来你会发现,设备左下角有你的程序 icon 图标

.h文件

#import<UIKit/UIKit.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"//iBeacon的uuid可以换成自己设备的uuid

- (void)viewDidLoad {

[super viewDidLoad];

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 568)];

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"];//初始化监测的iBeacon信息

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

}

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

if (status == kCLAuthorizationStatusAuthorizedAlways) {

[self.locationmanager startMonitoringForRegion:self.beacon1];//开始MonitoringiBeacon

}

}

{

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

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

[self.locationmanager startRangingBeaconsInRegion:self.beacon1];//开始RegionBeacons

}

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

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

//如果存在不是我们要监测的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 = @"远";

break;

case CLProximityUnknown:

str = @"不见了";

break;

default:

break;

}

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

return cell;

}

//一些错误处理,因为你正在同非常具体的硬件特性打交道,你需要知道任何原因导致的监控和测距失败

- (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的参数

uuid唯一标识此类iBeacon。

proximity远近范围的,有Near(在几米内),Immediate(在几厘米内),Far(超过 10 米以外,不过在测试中超不过10米就是far),Unknown(无效)

major和minor组合后区分同一类型下的iBeacon。

accuracy和iBeacon的距离

rssi信号轻度为负值,越接近0信号越强,等于0时无法获取信号强度

 

三.通知

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    if ([region isKindOfClass:[CLBeaconRegion class]]) {
        UILocalNotification *notification = [[UILocalNotification alloc] init];
        notification.alertBody = @"Are you forgetting something?";
        notification.soundName = @"Default";
        [[UIapplication sharedApplication] presentLocalNotificationNow:notification];
    }
}

你的位置管理器将在你离开某个区域时调用上面的方法,这就是这个应用有用的时刻。你不需要在你接近你的电脑包时被告知,只需在你离开它太远时通知你。

此处你检查区域是否是一个 CLBeaconRegion ,因为如果你同时也在执行地理定位区域监视的话,它还可能是一个 CLCircularRegion 。然后你就发送一个本地通知,附带一个消息“Are you forgetting something?” 。

编译并运行你的应用;离开某个你的注册的 iBeacon,然后一旦你离开得足够远,你就会看到通知弹出来。

 

参考地址:https://github.com/nixzhu/dev-blog/blob/master/2014-04-23-ios7-ibeacons-tutorial.md