iOS蓝牙开发之iBeacon技术

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数据   

猜你喜欢

转载自www.cnblogs.com/sundaysgarden/p/10319501.html