iBeacon组成信息:

1 、UUID(universally unique identifier):一个128位的唯一标识一个或多个Beacon基站为特定类型或特定的组织。

2、 Major:一个16位的无符号整数,可以将具有相同proximity UUID的Beacon基站组织联系起来。(用户可以自定义)

3、 Minor:同上。

4、Measured Power :是iBeacon发送模块与接收器之间距离为1米时的信号强度(RSSI)参照值。

通过蓝牙低功耗技术(BLE)发送特定的识别信息,来确定Beacon基站和设备之间的相对距离。而这个距离并不是精密推算的,而且我们是无法取设置它的距离的,而是将其分为三级:

未知(unknown)

约10厘米(immediate)

1米以内(near)

10米以外(far)

这是因为,发送和接受设备之间的距离在1米之内时,RSSI值基本是按照理论值减少的,而在1米外,收到发射波的影响,RSSI不会明显的随距离增加减少,而是会上下波动。也就是说,1米外的距离无法非常精密推测,可以用far来概括。'

好了废话不多说,直接说重点,也就是怎么干:

1.首先工程配置:由于ibeacon技术是基于蓝牙和定位的,作为熟练的开发者,我们第一眼想到的就是如下两个framework库:

035B9975-9465-45D3-BFA7-E0E30817650E.png

由于我的工程需要蓝牙后台扫描,所以我需要添加以下两个modes:

28201F86-9A71-4460-8E80-C95786814310.png

然后需要在info.plist文件里面添加描述:

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>

<string>XXX感应开门需要持续使用后台定位</string>

<key>NSLocationWhenInUseUsageDescription</key>

<string>使用您的位置来获取附近的小区</string>

1. iBeacon的使用

1、iBeacon的使用是基于蓝牙和定位的,所以我们需要先到入两个库:

#import <CoreLocation/CoreLocation.h>

#import <CoreBluetooth/CoreBluetooth.h>

在iOS8之后,苹果改变了定位的开启方式,需要在plist文件中加入字段NSLocationAlwaysUsageDescription(允许一直开启定位)

129603-ab293305d84223f4.png.jpg

2.直接上代码,以下代码包含广播自身与搜索附近广播:

#define UUID [[NSUUID alloc]initWithUUIDString:@"E2C56DB5-DFFB-48D2-B060-D0F5A71096E0"] // uuid可替换为自己需要的

@interface ViewController ()<CBPeripheralManagerDelegate,CLLocationManagerDelegate>

@property (strong, nonatomic) CLLocationManager *locationManager;

@property (strong, nonatomic) CLBeaconRegion *beaconRegion;

@property (strong, nonatomic) CBPeripheralManager *peripheraManager;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

[self beaconBroadCasting]; // 广播自身

[self beaconMonitoring]; // 扫描附近广播

}

#pragma mark - BroadCast

- (void)beaconBroadCasting{

// 创建并广播信号

if (!_peripheraManager)

{

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

}

else

{

_peripheraManager.delegate = self;

}

}

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

if (peripheral.state == CBPeripheralManagerStatePoweredOn) {

NSDictionary *peripheralData = nil;

CLBeaconRegion * region = [[CLBeaconRegion alloc] initWithProximityUUID:UUID major:0 minor:1 identifier:[UUID UUIDString]];// UUID、major、minor将会在后面解释

peripheralData = [region peripheralDataWithMeasuredPower:nil];

if(peripheralData)

{

[self.peripheraManager startAdvertising:peripheralData];

}

}

}

#pragma mark - Monitor

- (void)beaconMonitoring{

self.beaconRegion = [[CLBeaconRegion alloc]initWithProximityUUID:UUID identifier:[UUID UUIDString]];

self.locationManager = [[CLLocationManager alloc]init];

self.locationManager.delegate = self;

[self.locationManager startRangingBeaconsInRegion:self.beaconRegion];

}

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

// 发现有iBeacon设备进入扫描范围回调

}

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

// 扫描到iBeacon设备回调,扫描到的beacon存在数组beacons中

// 打印iBeacon信息

for (CLBeacon *beacon in beacons) {

// iBeacon参数将在后面介绍

}

}

当位置管理者的代理被调用,知道了可以时刻使用用户的位置时然后开始读取指定beacon的数据

1
2
3
4
5
6
7
8
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    if (status == kCLAuthorizationStatusAuthorizedAlways) {
         
        [self.locationmanager startMonitoringForRegion:self.beacon1];//开始
        [self.locationmanager startRangingBeaconsInRegion:self.beacon1];
         
    }
}

2.iBeacon的参数

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

major 主要值

minor 次要值

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

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

CLProximityFar  10米以外

CLProximityImmediate 几米范围之内

CLProximityNear  几厘米范围内

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

链接:https://www.jianshu.com/p/1207c8727889

來源:简书

下面说说ibeacon的使用。

1 首先需要需要在项目plist 中配置 Privacy - Location Always Usage Description 让程序允许使用位置

2 要使用ibeacon ,需要在项目中导入  CoreLocation 框架

3 实例化一个位置管理者对象,这里叫做 CLLocationManager ,再实例化一个ibeacon 对象: CLBeaconRegion

1
2
3
4
5
6
7
8
9
10
11
12
13
14
self.locationmanager = [[CLLocationManager alloc]
                         
                        init];//初始化
 
self.locationmanager.delegate = self;
_locationmanager.distanceFilter=10;//实时更新定位位置
_locationmanager.desiredAccuracy=kCLLocationAccuracyBest;//定位精确度
self.beacon1 = [[CLBeaconRegion alloc]
                 
                initWithProximityUUID:[[NSUUID alloc]
                                        
                                       initWithUUIDString:BEACONUUID]
                 
                identifier:@"media"];//初始化监测的iBeacon信息

  [self.locationmanager requestAlwaysAuthorization];

4 当位置管理者的代理被调用,知道了可以时刻使用用户的位置时然后开始读取指定beacon的数据

1
2
3
4
5
6
7
8
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    if (status == kCLAuthorizationStatusAuthorizedAlways) {
         
        [self.locationmanager startMonitoringForRegion:self.beacon1];//开始
        [self.locationmanager startRangingBeaconsInRegion:self.beacon1];
         
    }
}

5 当手机进入到了硬件设备的区域之后就会收到硬件设备发出的beacon 信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- (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-=mj%d-====min%d",beacon.rssi,beacon.major.intValue,beacon.minor.intValue);
    }
}

如果在硬件范围内,硬件一直在发射信号,那么手机就会一直收到硬件的ibeacon数据

iOS蓝牙开发之iBeacon技术的更多相关文章

  1. iOS 蓝牙开发之(mutipeerConnectivity)

    蓝牙 mutipeerConnectivity iOS7 引入的一个全新框架 替代GameKit框架 多用于文件传输 iOS设备不联网也能给附近的人聊天 搜索和传输的方式 * 双方WIFI和蓝牙都没有 ...

  2. iOS 蓝牙开发之(CoreBlueTooth)

    CoreBlueTooth 简介: 可用于第三方的蓝牙交互设备 设备必须支持蓝牙4.0 iPhone的设备必须是4S或者更新 iPad设备必须是iPad mini或者更新 iOS的系统必须是iOS 6 ...

  3. iOS多线程开发之GCD(中篇)

    前文回顾: 上篇博客讲到GCD的实现是由队列和任务两部分组成,其中获取队列的方式有两种,第一种是通过GCD的API的dispatch_queue_create函数生成Dispatch Queue:第二 ...

  4. iOS多线程开发之NSOperation - 快上车,没时间解释了!

    一.什么是NSOperation? NSOperation是苹果提供的一套多线程解决方案.实际上NSOperation是基于GCD更高一层的封装,但是比GCD更加的面向对象.代码可读性更高.可控性更强 ...

  5. iOS游戏开发之UIDynamic

    iOS游戏开发之UIDynamic 简介 什么是UIDynamic UIDynamic是从iOS 7开始引入的一种新技术,隶属于UIKit框架 可以认为是一种物理引擎,能模拟和仿真现实生活中的物理现象 ...

  6. iOS多线程开发之NSOperation

    一.什么是NSOperation? NSOperation是苹果提供的一套多线程解决方案.实际上NSOperation是基于GCD更高一层的封装,但是比GCD更加的面向对象.代码可读性更高.可控性更强 ...

  7. iOS多线程开发之GCD(死锁篇)

    上篇和中篇讲解了什么是GCD,如何使用GCD,这篇文章将讲解使用GCD中将遇到的死锁问题.有兴趣的朋友可以回顾<iOS多线程开发之GCD(上篇)>和<iOS多线程开发之GCD(中篇) ...

  8. iOS多线程开发之GCD(中级篇)

    前文回顾: 上篇博客讲到GCD的实现是由队列和任务两部分组成,其中获取队列的方式有两种,第一种是通过GCD的API的dispatch_queue_create函数生成Dispatch Queue:第二 ...

  9. android蓝牙4.0(BLE)开发之ibeacon初步

    一个april beacon里携带的信息如下 ? 1 <code class=" hljs ">0201061AFF4C0002159069BDB88C11416BAC ...

随机推荐

  1. Microsoft.AspNet.Identity 重置密码

    重置密码:先生成重置密码的Token,然后调用ResetPassword方法重置密码,密码要符合规则.. ApplicationUserManager UserManager => _userM ...

  2. f.lux 自动调节显示器色温

    我的环境 f.lux 我的使用感受是让屏幕看起来舒服一些,因为我有近视,所以需要保护眼睛. f.lux官网:https://justgetflux.com/ f.lux v4.47 windows 1 ...

  3. nmap参数原理抓包分析

    nmap参数原理抓包分析 实验环境: Nmap7.70 实验步骤: 1.主机发现 2.端口扫描 3.服务版本探测 一.主机发现 主机发现,如果主机活跃,扫描1000个常用的tcp端口 1.Nmap i ...

  4. June 5. 2018 Week 23rd Tuesday

    Learn to let go and be clear of where you really want to head for. 学会放手,同时也要弄清楚自己的真正所爱. From Kissing ...

  5. March 11th, 2018 Week 11th Sunday

    All good things must come to an end. 好景无常. Love is when the other person's happiness is more importa ...

  6. Custom partition assignment and migration kafka集群扩充迁移指定partition

    The partition reassignment tool can also be used to selectively move replicas of a partition to a sp ...

  7. 【Java多线程通信】syncrhoized下wait()/notify()与ReentrantLock下condition的用法比较

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6556925.html  一:syncrhoized使用同一把锁的多个线程用通信实现执行顺序的调度 我们知道,使 ...

  8. 对flexbox伸缩概念的深入浅出解释

    flex布局最难理解的,就是剩余空间和伸缩概念了,此文很好的作了解释: https://www.cnblogs.com/ghfjj/p/6529733.html 转自:http://zhoon.git ...

  9. UVA140-Bandwidth(搜索剪枝)

    Problem UVA140-Bandwidth Time Limit: 3000 mSec  Problem Description Given a graph (V, E) where V is ...

  10. 【vue】vue +element 搭建项目,el-input 常用的验证

    1.el-input 常用布局 <el-input class="filter-item dialog-search" size="small" @key ...