最近时间一直在研究ibeacon所以把自己遇到的一些问题写下来做个笔记。

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

iBeacon是苹果被允许能在后台运行的,不论你将应用退出到后台还是杀死,iBeacon都能激活应用不过只能激活10秒左右,但是这段时间足可以做很多事情了。

一.iBeacon的使用

开始监听你的Ibeacon。

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

接着在程序里添加

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

{

if (status == kCLAuthorizationStatusAuthorizedAlways) {

[self.locationmanager startMonitoringForRegion:self.beacon1];

}

}

.h文件

#import<UIKit/UIKit.h>

#import<CoreLocation/CoreLocation.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, 400)];

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;

}

二.ibeacon的参数

uuid唯一标识此类iBeacon。

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

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

accuracy和iBeacon的距离

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

三.碎碎念

当进入iBeacon范围是会触发didEnterRegion方法,此时可能获取不到iBeacon的rssi ,proximity,accuracy值因为距离有点远,所一要在此时做些动作和这三个参数有关的话需要小心。

iOS开发 关于iBeacon的一些记录的更多相关文章

  1. 记录我的点点滴滴从此刻做起——iOS开发工程师

    作为一个iOS工程师,想写博客也是有原因的:首先有这个想法(写博客的想法)也是因为想到自己都从事iOS开发快两年了,怎么也只会堆代码,写view,技术真的很一般,感觉都要被淘汰了:基于以上原因,自己也 ...

  2. iOS开发之记录用户登录状态

    iOS开发之记录用户登录状态 我们知道:CoreData的配置和使用步骤还是挺复杂的.但熟悉CoreData的使用流程后,CoreData还是蛮好用的.今天要说的是如何记录我们用户的登陆状态.例如微信 ...

  3. IOS开发之记录用户登陆状态,ios开发用户登陆

    IOS开发之记录用户登陆状态,ios开发用户登陆 上一篇博客中提到了用CoreData来进行数据的持久化,CoreData的配置和使用步骤还是挺复杂的.但熟悉CoreData的使用流程后,CoreDa ...

  4. iOS开发架构学习记录

    闲着没事看了一些iOS开发架构的视频,简单的介绍了几个常用的架构设计,现将它记录如下,以后有时间再专门写这方面的内容,大家可以看看,感兴趣的就进一步学习. 一.架构基础 1.架构设计的目的 进一步解耦 ...

  5. iOS开发--性能调优记录

    CPU VS GPU 关于绘图和动画有两种处理的方式:CPU(中央处理器)和GPU(图形处理器).但是由于历史原因,我们可以说CPU所做的工作都在软件层面,而GPU在硬件层面 对于图像处理,通常用硬件 ...

  6. iOS开发系列--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开发汇总

    --系统应用与系统服务 iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用 ...

  7. iOS开发系列通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开

    --系统应用与系统服务 iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用 ...

  8. iOS开发之再探多线程编程:Grand Central Dispatch详解

    Swift3.0相关代码已在github上更新.之前关于iOS开发多线程的内容发布过一篇博客,其中介绍了NSThread.操作队列以及GCD,介绍的不够深入.今天就以GCD为主题来全面的总结一下GCD ...

  9. iOS开发系列--App扩展开发

    概述 从iOS 8 开始Apple引入了扩展(Extension)用于增强系统应用服务和应用之间的交互.它的出现让自定义键盘.系统分享集成等这些依靠系统服务的开发变成了可能.WWDC 2016上众多更 ...

随机推荐

  1. PID参数调节口诀

    参数整定找最佳, 从小到大顺序查. 先是比例后积分, 最后再把微分加. 曲线振荡很频繁, 比例度盘要放大. 曲线漂浮绕大弯, 比例度盘往小扳. 曲线偏离回复慢, 积分时间往下降. 曲线波动周期长, 积 ...

  2. option_match

    //与match区别:如果没有匹配到,返回NULLCREATE (olive:Person {name:'Olive Stone'}),(charlie:Person {name:'Charlie S ...

  3. Exception 异常 输出的各个方法的区别

    try{ System.out.println(1/0); }catch(Exception e){ //System.out.println(e+""); //对象+字符串 = ...

  4. sql条件查询-日期比较(取年月部分)

    查询当年当月的数据: select * from compalete_task where to_Char(create_date,'yyyyMM') = to_Char(sysdate,'yyyyM ...

  5. Python 语法糖装饰器的应用

    Python中的装饰器是你进入Python大门的一道坎,不管你跨不跨过去它都在那里. 为什么需要装饰器 我们假设你的程序实现了say_hello()和say_goodbye()两个函数. def sa ...

  6. python之常用字符串用法

    1.isdigit=indecimal(判断是否是数字) s=" print(s.isdigit()) 输出为:True 2.format(标识符) a =("I have a { ...

  7. Syslog和Windows事件日志收集

    Syslog和Windows事件日志收集 EventLog Analyzer从分布式Windows设备收集事件日志,或从分布式Linux和UNIX设备.交换机和路由器(Cisco)收集syslog.事 ...

  8. 关于syslog日志功能详解 事件日志分析、EventLog Analyzer

    关于syslog日志功能详解 事件日志分析.EventLog Analyzer 一.日志管理 保障网络安全 Windows系统日志分析 Syslog日志分析 应用程序日志分析 Windows终端服务器 ...

  9. docker 加速器配置目录

    centos 7 : /lib/systemd/system/docker.service

  10. presentation skills

    下面是从一个网站摘录下来的关于presentation skill需要回答的14个问题:网站的地址为:http://www.mindtools.com/pages/article/newCS_96.h ...