Main Contents

[objective-c] Ibeacons et iphone

septembre 28, 2015

Les ibeacons sont des petits émetteurs bluetooth qui peuvent être détecter par une application et enclencher des actions. Ils ont un UUID et 2 valeurs, major et minor.

On va faire une petite application qui va détecter nos beacons et afficher certaines de leurs infos.
On commence par rajouter la librairie CoreLocation.
Puis dans le controller :

 locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    [locationManager requestWhenInUseAuthorization];
    [locationManager requestAlwaysAuthorization];
 
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"EBEFD083-70A2-47C8-9837-E7B56SSDF524"];
 
    self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"com.appcoda.testregion"];
 
    if(self.myBeaconRegion) {
        self.myBeaconRegion.notifyOnEntry = YES;
        self.myBeaconRegion.notifyOnExit = YES;
        [locationManager startMonitoringForRegion:self.myBeaconRegion];
        NSLog(@"start");
    }
 
    [locationManager requestStateForRegion:self.myBeaconRegion];

Cela permet de bien demander l’autorisation d’utiliser la localisation.

Ensuite on va rajouter les méthodes de delegate.

- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion*)region
{
    [locationManager startRangingBeaconsInRegion:self.myBeaconRegion];
}
 
-(void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion*)region
{
    [locationManager stopRangingBeaconsInRegion:self.myBeaconRegion];
 
}
-(void)locationManager:(CLLocationManager*)manager didRangeBeacons:(NSArray*)beacons inRegion:(CLBeaconRegion*)region
{
 
 
     for (CLBeacon *foundBeacon in beacons) {
 
        NSString *uuid = foundBeacon.proximityUUID.UUIDString;
        NSString *major = [NSString stringWithFormat:@"%@", foundBeacon.major];
        NSString *minor = [NSString stringWithFormat:@"%@", foundBeacon.minor];
        NSString *rssi = [NSString stringWithFormat:@"%lu", foundBeacon.rssi];
 
        switch (foundBeacon.proximity) {
            case CLProximityUnknown:
                NSLog(@"Unknown");
                break;
            case CLProximityImmediate:
                NSLog( @"Immediate");
                break;
            case CLProximityNear:
                NSLog( @"Near");
                break;
            case CLProximityFar:
                NSLog( @"Far");
                break;
        }
        NSLog(@"Uuid : %@",uuid);
        NSLog(@"Major : %@",major);
        NSLog(@"RSSi : %@",rssi);
        float a = (foundBeacon.rssi-70)/(-100*log(10));
        NSLog(@"%f",a);
        NSLog(@"----------------------");
 
 
    }
 
}
 
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region {
    if (state == CLRegionStateInside){
        NSLog(@"is in target region");
        [locationManager startRangingBeaconsInRegion:self.myBeaconRegion];
    }else{
        NSLog(@"is out of target region");
    }
 
}

Cela permet d’afficher dans la console les infos des beacons que l’on a détecté. Il faut bien comprendre que l’on cherche des Beacons ayant un UUID bien précis, on ne verra pas les autres beacons. Par contre, tous les beacons ayant cet UUID pourront être différencié grace à leurs 2 valeurs, major et minor.
Donc par exemple, envoyer un message, ou une alerte quand on est très proche du beacon ayant le major égal à XX.

Catégorie(s): Développement, Objective-C, Tutorial | Comments (0)

Leave a comment