最近时间一直在研究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. android xml 解析汉字只出来一个字的问题

    DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); // 实例化DocumentBuilder factor ...

  2. 下载win10

    http://www.xitongtiandi.net/win10yuanban/2039.html#download

  3. 【Java】Maven Tomcat插件使用

    本例是用的是tomcat7-maven-plugin 插件 依赖 tomcat7-maven-plugin 插件的pom.xml依赖为 <dependency> <groupId&g ...

  4. 神奇的幻方(NOIP2015)

    先给题目链接:神奇的幻方 太水了这题,直接模拟就行,直接贴代码. #include<bits/stdc++.h> using namespace std; int main(){ int ...

  5. GO介绍,环境的配置和安装 简单使用

    1. 介绍与安装 Golang 是什么 Go 亦称为 Golang(按照 Rob Pike 说法,语言叫做 Go,Golang 只是官方网站的网址),是由谷歌开发的一个开源的编译型的静态语言. Gol ...

  6. 使用 kbmmw 的ORM开发纯REST数据库访问服务

    运行环境: WIN 10  X64 delphi 10.2.2 kbmmw 5.05.11 Firefox 58.0.2 今天使用最新的kbmmw 版本做一个基于ORM的纯数据库访问的REST 服务器 ...

  7. AOP (切点表达式讲解)

    Spring EL表达式:: 1.execution 表达式 语法格式: execution(返回类型.包名.类名.方法名(参数表)) exection(*.com.xxx.AService.*(.. ...

  8. Python开课复习-10/17

    pickle是一个用来序列化的模块序列化是什么?指的是将内存中的数据结构转化为一种中间格式 并存储到硬盘上 反序列化?将硬盘上存储的中间格式数据在还原为内存中的数据结构 为什么要序列化?就是为了将数据 ...

  9. super-smack压测工具

    简介 super-smack是一款开源压测工具,支持MySQL.PostgreSQL.Oracle.本篇主要介绍一下使用super-smack压测MySQL体会. 1.SQL定义 2.数据字典定义 3 ...

  10. urllib — URL handling modules

    urllib is a package that collects several modules for working with URLs: •urllib.request for opening ...