iBeacon Scanner實作概念

iOS

1. 利用 CoreBluetoooth 來找尋附近的藍芽裝置

Code Snippet

#import <CoreBluetooth/CoreBluetooth.h>

@interface ViewController ()<CBCentralManagerDelegate, CBPeripheralDelegate>
{
    CBCentralManager *_manager;
    CBPeripheral *_peripheral;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _manager =  [[CBCentralManager alloc] initWithDelegate:self
                                                    queue:nil];


}


- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
    switch ((int)central.state)
    {
        case CBCentralManagerStatePoweredOn:
        {
            NSDictionary *options = @{
                                      CBCentralManagerScanOptionAllowDuplicatesKey: @YES
                                      };
            [_manager scanForPeripheralsWithServices:nil
                                             options:options];
            NSLog(@"I just started scanning for peripherals");
            break;
        }
    }
}

- (void) centralManager:(CBCentralManager *)central
    didDiscoverPeripheral:(CBPeripheral *)peripheral
        advertisementData:(NSDictionary *)advertisementData
                     RSSI:(NSNumber *)RSSI
{

    if([advertisementData objectForKey:@"kCBAdvDataLocalName"]!=nil){
        NSLog(@"I see an advertisement with identifer: %@, state: %ld, name: %@, services: %@, description: %@",
              [peripheral identifier],
              [peripheral state],
              [peripheral name],
              [peripheral services],
              [advertisementData description]);
    }



    if (_peripheral == nil)
    {
        NSLog(@"Trying to connect to peripheral");
        _peripheral = peripheral;
        _peripheral.delegate = (id)self;
        [central connectPeripheral:_peripheral
                           options:nil];
    }
}

- (void) centralManager:(CBCentralManager *)central
    didConnectPeripheral:(CBPeripheral *)peripheral
{
    if (peripheral == nil)
    {
        NSLog(@"connect callback has nil peripheral");
    } else {
        NSLog(@"Connected to peripheral with identifer: %@, state: %ld, name: %@, services: %@",
              [peripheral identifier],
              [peripheral state],
              [peripheral name],
              [peripheral services]);

        NSLog(@"discovering services...");
        _peripheral = peripheral;
        _peripheral.delegate = (id)self;
        [_peripheral discoverServices:nil];
    }
}

- (void) peripheral:(CBPeripheral *)peripheral
    didDiscoverServices:(NSArray *)serviceUuids
{
    NSLog(@"discovered a peripheral's services: %@", serviceUuids);
}

其中可以過濾掉一些不會廣播的設備

if([advertisementDataobjectForKey:@"kCBAdvDataLocalName"]!=nil){

可以取該設備的UUID

<__NSConcreteUUID 0x17403f520> 2750A093-A1C2-9953-A59F-A0769538DC15

附帶一提的是,小米手環也會廣播自已的藍芽訊號

2. 接著利用 CLLocationManagerDelegate 對 已取得的UUID 對 iBeacon 做程式的實作

 for (NSUUID *uuid in [APLDefaults sharedDefaults].supportedProximityUUIDs)
    {
        CLBeaconRegion *region = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:[uuid UUIDString]];
        self.rangedRegions[region] = [NSArray array];
    }


#pragma mark - Location manager delegate

- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
{
    // 這裡就可以取到 iBeacon 了
}

在這裡 小米手環 不會出現在 CLLocationManagerDelegate,所以會出現的就只剩下iBeacon了

打完收工