iOS iBeacon 使用

作者: flyhao | 来源:发表于2015-12-15 10:41 被阅读1799次

最近做一个店铺签到获取积分的App,用到了iBeacon,蛮好玩的一个小玩意,简单来说iBeacon这个小设备,可以被手机通过蓝牙搜索到,并能比较精确的显示距离,和拿到该iBeacon的uuid,major,minor。其中uuid 是一个区域内的唯一标识符,用它可以区别一个公司的iBeacon,而用major和minor 来区别店铺和具体哪台设备。

客户端代码实现

//BeaconClient.h
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <CoreBluetooth/CoreBluetooth.h>

@interface BeaconClient : NSObject<CLLocationManagerDelegate>
{
    CLLocationManager * _locationManager;
    CLBeaconRegion * _region;
    BOOL _isInsideRegion;
}

- (BOOL)openClient;
- (void)closeClient;

@end

//BeaconClient.m
#import "BeaconClient.h"
@implementation BeaconClient

- (id)init
{
    if (self = [super init]) {
        _isInsideRegion = NO;
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        
        NSUUID *estimoteUUID = [[NSUUID alloc] initWithUUIDString:kUUID];
        _region = [[CLBeaconRegion alloc] initWithProximityUUID:estimoteUUID identifier:kIndetifier];
        // launch app when display is turned on and inside region
        _region.notifyEntryStateOnDisplay = YES;
    }
    return self;
}

- (BOOL)openClient
{
    if ([CLLocationManager locationServicesEnabled]) {
        //开始定位用户的位置
        [_locationManager startUpdatingLocation];
        [_locationManager requestAlwaysAuthorization];
        //每隔多少米定位一次(这里的设置为任何的移动)
        _locationManager.distanceFilter=kCLDistanceFilterNone;
        _locationManager.desiredAccuracy=kCLLocationAccuracyBestForNavigation;
    }else{//不能定位用户的位置
        //1.提醒用户检查当前的网络状况
        //2.提醒用户打开定位开关
    }
    
    if ([CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]])
    {
        [_locationManager startMonitoringForRegion:_region];
        [_locationManager startRangingBeaconsInRegion:_region];
        [_locationManager requestStateForRegion:_region];
        return YES;
    }else{
        [self showAlertView:nil message:@"This device does not support monitoring beacon regions"];
        return NO;
    }
}

- (void)closeClient
{
    [_locationManager stopMonitoringForRegion:_region];
    [_locationManager stopRangingBeaconsInRegion:_region];
    
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    NSLog(@"!!");
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager
      didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
    if(state == CLRegionStateInside)
    {
        _isInsideRegion = YES;
    }else if(state == CLRegionStateOutside){
        _isInsideRegion = NO;
    }else{
        _isInsideRegion = NO;
    }
}

-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
{
    //发现iBeacon Server
    for (CLBeacon* beacon in beacons) {
        
        NSString * info_1 = [NSString stringWithFormat:@"UUIDString:%@",beacon.proximityUUID.UUIDString];
        NSString * info_2 = [NSString stringWithFormat:@"major:%@",beacon.major];
        NSString * info_3 = [NSString stringWithFormat:@"minor:%@",beacon.minor];
        NSString * info_4 = [NSString stringWithFormat:@"accuracy:%0.4f米",beacon.accuracy];
        NSString * info_5 = [NSString stringWithFormat:@"proximity:%ld",beacon.proximity];
        NSString * info_6 = [NSString stringWithFormat:@"rssi:%ld",(long)beacon.rssi];
        
        NSString * debugInfo = [NSString stringWithFormat:@"%@\n%@\n%@\n%@\n%@\n%@\n",info_1,info_2,info_3,info_4,info_5,info_6];
        NSLog(@"%@",debugInfo);
    }
}

- (void)locationManager:(CLLocationManager *)manager
         didEnterRegion:(CLRegion *)region
{
    NSLog(@"didEnterRegion");
    if (_isInsideRegion) return;
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
    {
        [self sendEnterLocalNotification];
    }else{
        [self showAlertView:nil message:@"Hi,你已经进入 iSS iBeacon region"];
    }
}


- (void)locationManager:(CLLocationManager *)manager
          didExitRegion:(CLRegion *)region
{
    NSLog(@"didExitRegion");
    if (!_isInsideRegion) return;
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
    {
        [self sendExitLocalNotification];
    }else{
        [self showAlertView:nil message:@"sorry,你离开了 iSS iBeacon region"];
    }
}

- (void)showAlertView:(NSString *)title message:(NSString *)msg
{
    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:title
                                                         message:msg
                                                        delegate:nil
                                               cancelButtonTitle:@"确定"
                                               otherButtonTitles:nil];
    [alertView show];
}

- (void)sendEnterLocalNotification
{
    UILocalNotification *notice = [[UILocalNotification alloc] init];
    notice.alertBody = @"Hi,你已经进入 iSS iBeacon region,打开应用获取最新信息";
    notice.alertAction = @"Open";
    notice.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication] scheduleLocalNotification:notice];
}

- (void)sendExitLocalNotification
{
    UILocalNotification *notice = [[UILocalNotification alloc] init];
    notice.alertBody = @"sorry,你离开了 iSS iBeacon region";
    notice.alertAction = @"Open";
    notice.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication] scheduleLocalNotification:notice];
}
@end

项目源码地址 感谢作者的分享
同时可以参考苹果官方源码,里面有设计多个uuid同时检测
苹果官方提供demo

相关文章

  • 开发使用 iBeacon 的 iOS 7 应用

    开发使用 iBeacon 的 iOS 7 应用 开发使用 iBeacon 的 iOS 7 应用

  • iOS 中 iBeacon 开发

    iOS 中 iBeacon 开发 iOS 中 iBeacon 开发

  • iBeacon开发学习资料整理

    开发使用 iBeacon 的 iOS 7 应用:http://www.cocoachina.com/industr...

  • iOS iBeacon 使用

    最近做一个店铺签到获取积分的App,用到了iBeacon,蛮好玩的一个小玩意,简单来说iBeacon这个小设备,可...

  • iOS蓝牙开发之iBeacon

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

  • iBeacon 应用实例

    iBeacon是什么?     苹果官方对iBeacon的描述:iBeacon是iOS 7推出的一项技术,可为AP...

  • IOS iBeacon的使用

    @end 与之前一样,你需要初始化位置管理器并设置它们的 delegate 。 在 application:did...

  • iBeacon相关知识

    从iBeacon开始 入门iBeacon概述 在iOS 7中引入的iBeacon是一项令人兴奋的技术,可以实现新的...

  • ibeacon 技术记录

    ibeacon是苹果公司在ios7发布的一款硬件,可以感知ibeacon的位置。ibeacon 只是一个硬件设备,...

  • iOS iBeacon基站开发

    iBeacon在iOS 7之后的版本中有内置库,直接引入就可以使用 #import

网友评论

  • 9abf4a84b4cd:你好,请问一下,怎么才能搜索附近所有的ibeacon呢?或者根据多个uuid来搜索ibeacon?
  • ChanJaWe:你好,我想问一下iOS10对ibeacon有什么改动吗?我在iOS10上 didExit和didEnter方法有时能触发有时不能,iOS9上是正常的,iOS10需要添加什么代码吗?
  • 74b112e2f20c:挺有帮助的 能请问一下代码中的KUUID怎么设置的么
    c9d2a0619981:@dingdinghh 你也是做搜索蓝牙uuid,major,minor的的demo么

本文标题:iOS iBeacon 使用

本文链接:https://www.haomeiwen.com/subject/vwuuhttx.html