我按照AppCoda和Devfright的在线教程创建了一个 iBeacon 检测应用程序。我正在使用来自 estimote 的 iBeacon,一个带有 iOS 8 的 iPad 3。该应用程序根本没有检测到我的 iBeacon,而它被其他 iBeacon 应用程序检测到。我无法理解我在代码中遗漏了什么或做错了什么。
这是我的 .h 文件:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController<CLLocationManagerDelegate>
@property (nonatomic, strong) CLBeaconRegion *beaconRegion;
@property (nonatomic, strong) CLLocationManager *locManager;
@property (nonatomic, strong) IBOutlet UILabel *label1;
@property (nonatomic, strong) IBOutlet UILabel *label2;
@end
这是我的 .m 文件:
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.locManager = [[CLLocationManager alloc] init];
self.locManager.delegate = self;
//default uuid for estimote beacons
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"B9407F30-F5F8-466E-AFF9-25556B57FE6D"];
self.beaconRegion = [[CLBeaconRegion alloc]initWithProximityUUID:uuid identifier:@"com.rk.testregion"];
[self.locManager startMonitoringForRegion:self.beaconRegion];
[self locationManager:self.locManager didStartMonitoringForRegion:self.beaconRegion];
// Check if beacon monitoring is available for this device
if (![CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Monitoring not available" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil]; [alert show]; return;
}
}
-(void)locationManager:(CLLocationManager*)manager didRangeBeacons:(NSArray*)beacons inRegion:(CLBeaconRegion*)region
{
// Beacon found!
self.label1.text = @"Welcome to";
self.label2.text = @"Location 1";
CLBeacon *foundBeacon = [beacons firstObject];
// retrieve the beacon data from its properties
NSString *uuid = foundBeacon.proximityUUID.UUIDString;
NSString *major = [NSString stringWithFormat:@"%@", foundBeacon.major];
NSString *minor = [NSString stringWithFormat:@"%@", foundBeacon.minor];
NSLog(@"UUID: %@", uuid);
NSLog(@"major: %@", major);
NSLog(@"minor: %@", minor);
}
- (void)viewDidDisappear:(BOOL)animated
{
[self.locManager stopRangingBeaconsInRegion:self.beaconRegion];
[super viewDidDisappear:animated];
}
- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion*)region
{
[self.locManager startRangingBeaconsInRegion:self.beaconRegion];
}
-(void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion*)region
{
[self.locManager stopRangingBeaconsInRegion:self.beaconRegion];
self.label1.text = @"Searching again...";
}
- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
[self.locManager startRangingBeaconsInRegion:self.beaconRegion];
}
@end
委托方法 didRangeBeacons 根本不会被调用。有人可以让我知道如何解决这个问题。