【发布时间】:2014-09-17 14:14:10
【问题描述】:
我有 3 个 iBeacon,它们被放置在 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;
}
}
}
【问题讨论】:
-
您的问题是什么?您的代码中是否有什么不工作的地方?
标签: ios objective-c bluetooth ibeacon