IOS ibeacon的基本使用

最近有一个室内定位的项目,需要用到的就是ibeacon,精度可以达到几厘米。经过一系列的研究与探索终于在昨天成功的做了出来,在这里写下与大家分享,探讨。

再本项目中需要导入CoreLocation.framework和CoreBluetooth.framework

首先:需要最少一个广播,下面是建立广播的方法。

#import <CoreBluetooth/CoreBluetooth.h>

#import <CoreLocation/CoreLocation.h>


@interface ViewController () <CBPeripheralManagerDelegate>

@property (weak, nonatomic) IBOutletUILabel *statusLabel;


@property (strong,nonatomic) CLBeaconRegion *myBeaconRegion;

@property (strong,nonatomic) NSDictionary *myBeaconData;

@property (strong,nonatomic) CBPeripheralManager *peripheralManager;

@property (strong,nonatomic)CLLocationManager *manager;


@end


@implementation ViewController


-(void)peripheralManagerDidUpdateState:(CBPeripheralManager*)peripheral

{

    if (peripheral.state ==CBPeripheralManagerStatePoweredOn)

    {

        // Bluetooth is on

        

        // Update our status label

        self.statusLabel.text = @"Broadcasting...";

        

        // Start broadcasting

        [self.peripheralManagerstartAdvertising:self.myBeaconData];

    }

    elseif (peripheral.state ==CBPeripheralManagerStatePoweredOff)

    {

        // Update our status label

        self.statusLabel.text =@"Stopped";

    }

    elseif (peripheral.state ==CBCentralManagerStateUnsupported)

    {

        self.statusLabel.text = @"CBCentralManagerStateUnsupported";

    }

}

- (IBAction)buttonClick:(UIButton *)sender {

    

    // Get the beacon data to advertise

    self.myBeaconData = [self.myBeaconRegionperipheralDataWithMeasuredPower:nil];

    

    // Start the peripheral manager

    self.peripheralManager = [[CBPeripheralManageralloc] initWithDelegate:self

                                                                     queue:nil

                                                                   options:nil];

    

}


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    self.manager = [[CLLocationManageralloc] init];

    self.manager.pausesLocationUpdatesAutomatically =NO;

    NSUUID *uuid = [[NSUUIDalloc] initWithUUIDString:@"23542266-18D1-4FE4-B4A1-23F8195B9D39"];

    

    // Initialize the Beacon Region

    int i = arc4random()%10;

    self.myBeaconRegion = [[CLBeaconRegionalloc] initWithProximityUUID:uuid

                        major:i

                        minor:i

                   identifier:[[NSStringalloc] initWithFormat:@"com.appcoda.testregion_%d",i]];

}


建立广播没有需要注意的地方,唯一需要注意的就是建立广播时需要打开蓝牙。


广播建立成功之后,下面就是需要建立灯塔。

_manager = [[CLLocationManageralloc] init];

 _manager.delegate =self;

    [_manager requestAlwaysAuthorization];

    

    _beacon = [[CLBeaconRegionalloc] initWithProximityUUID:[[NSUUIDalloc] initWithUUIDString:@"23542266-18D1-4FE4-B4A1-23F8195B9D39"]identifier:@"com.main.beacon"];

    _beacon.notifyEntryStateOnDisplay =YES;

    _beacon.notifyOnEntry =YES;

    

    [self.managerstartMonitoringForRegion:_beacon];

    [self.managerrequestStateForRegion:self.beacon];



-(void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region

{

    [_managerstartRangingBeaconsInRegion:_beacon];

}




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

{

    [_managerstartRangingBeaconsInRegion:_beacon];

}


-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region

{

    [_managerstopRangingBeaconsInRegion:_beacon];

}


-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

{

    NSLog(@"error = %@",error);

}


-(void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error

{

    NSLog(@"error = %@",error);

}


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

{

    //[_algo updateBeacon:beacons];

}

这些是manager的代理方法,具体的我们使用到的是最后一个。返回beacons数组,就是我们扫描后的beacon设备。


附上下载地址  http://download.csdn.net/detail/a174455171/9512193


在做这个项目的时候我碰到了下面几个问题

1.扫描beacon设备的代理方法没有回调。原因可能是因为没有大功率扫描,在代码里面可以看到。

2.扫描beacon设备的代理方法,beacon数组元素为0。在建立广播和灯塔的时候都需要初始化一个beacon,这里我们看到有一个uuid的参数,这两个参数要保持一致,要不会搜索不到设备。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值