最近有一个室内定位的项目,需要用到的就是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的参数,这两个参数要保持一致,要不会搜索不到设备。