如何在多个iBeacons在范围内时显示通知

时间:2014-09-17 14:14:11

标签: ios objective-c bluetooth ibeacon

我有3个iBeacons,分别放在3个不同的房间里。当我走进每个房间时,我希望在我的应用程序关闭时收到通知,告诉我我在哪个房间。

我的信标都有UUID但不同的主要和次要版本。

这是我们到目前为止在课堂上实现的(不在App Delegate中)

-(void)locationManager:(CLLocationManager*)manager didRangeBeacons:(NSArray*)beacons inRegion:(CLBeaconRegion*)region {

// firstBeacon is the closest beacon
CLBeacon *firstBeacon = [beacons firstObject];

NSLog(@" Major %@ Minor %@", firstBeacon.major, firstBeacon.minor);

int major = [firstBeacon.major intValue];
int minor = [firstBeacon.minor intValue];

if (major == 43005 && minor == 52679) {

    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.soundName = @"Default";
    notification.alertBody = @"Green";
    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];

    self.beaconColour.text = @"Green";
}
else if (major == 48891 && minor == 47852) {

    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.soundName = @"Default";
    notification.alertBody = @"Light Blue";
    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];

    self.beaconColour.text = @"Light Blue";
}
else if (major == 59510 && minor == 42953) {

    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.soundName = @"Dark Blue";
    notification.alertBody = @"Green";
    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];

    self.beaconColour.text = @"Dark Blue";
}

self.major.text = [NSString stringWithFormat:@"%d", major];
self.minor.text = [NSString stringWithFormat:@"%d", minor];

}

根据回答更新了代码

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;

    NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"B9407F30-F5F8-466E-AFF9-25556B57FE6D"];

    self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
                                                             identifier:@"com.accenture.testregion"];


    self.myBeaconRegion.notifyEntryStateOnDisplay = YES;
    self.myBeaconRegion.notifyOnEntry = YES;
    self.myBeaconRegion.notifyOnExit = YES;
    [self.locationManager startMonitoringForRegion:self.myBeaconRegion];

}

- (void)didReceiveMemoryWarnins {

    [super didReceiveMemoryWarning];
}

BOOL _isInsideRegion;

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

    NSLog(@"didEnterRegion");
    if ([region isKindOfClass:[CLBeaconRegion class]]) {
        CLBeaconRegion *beaconRegion = (CLBeaconRegion *)region;
        int major = [beaconRegion.major intValue];
        int minor = [beaconRegion.minor intValue];

        NSLog(@" Major %d Minor %d", major, minor);

        if (major == 43005 && minor == 52679) {

            self.beaconColour.text = @"Green";
            if (!_isInsideRegion) {
                UILocalNotification *notification = [[UILocalNotification alloc] init];
                notification.soundName = @"Default";
                notification.alertBody = @"Green";
                [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
            }
            _isInsideRegion = YES;
        }
        else if (major == 48891 && minor == 47852) {

            self.beaconColour.text = @"Light Blue";
            if (!_isInsideRegion) {
                UILocalNotification *notification = [[UILocalNotification alloc] init];
                notification.soundName = @"Default";
                notification.alertBody = @"Green";
                [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
            }
            _isInsideRegion = YES;
        }
        else if (major == 59510 && minor == 42953) {

            self.beaconColour.text = @"Dark Blue";
            if (!_isInsideRegion) {
                UILocalNotification *notification = [[UILocalNotification alloc] init];
                notification.soundName = @"Default";
                notification.alertBody = @"Green";
                [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
            }
            _isInsideRegion = YES;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

尽量不要从beacons数组中获取第一个对象,你应该枚举它并比较哪个是哪个,有时你可以从几个信标中接收信号。

我通过调用didEnterRegion:代理而不是didRangeBeacons:让它成功了 您还应该创建一个布尔标志(变量)以防止重复发送通知。在次要/主要匹配时在if条件中标记为true,并在didExitRegion中将其设置为false:

BOOL _isInsideRegion;


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

    if ([region isKindOfClass:[CLBeaconRegion class]]) {
        CLBeaconRegion *beaconRegion = (CLBeaconRegion *)region;
        int major = [beaconRegion.major intValue];
        int minor = [beaconRegion.major.minor intValue];

        if (major == 43005 && minor == 52679) {
            if (!_isInsideRegion) {
                UILocalNotification *notification = [[UILocalNotification alloc] init];
                notification.soundName = @"Default";
                notification.alertBody = @"Green";
                [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
            }
            _isInsideRegion = YES;    
            self.beaconColour.text = @"Green";
    }
        else if (major == 48891 && minor == 47852) {
            if (!_isInsideRegion) {....
    }
}

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

    if ([region isKindOfClass:[CLBeaconRegion class]]) {
        CLBeaconRegion *beaconRegion = (CLBeaconRegion *)region;
        if (major == 43005 && minor == 52679) {
                if (!_isInsideRegion) {
                     // You can send another notification to inform user that he left the region
                 }
                _isInsideRegion = NO;
}

答案 1 :(得分:0)

当您的应用在后台时,您很快就无法获得区域进入/退出事件。获取通知最多可能需要15分钟。见这里:http://developer.radiusnetworks.com/2013/11/13/ibeacon-monitoring-in-the-background-and-foreground.html