嗨〜我正在学习用CLLocationManager检测iBeacon显示。 我读这篇文章: http://developer.radiusnetworks.com/2013/11/13/ibeacon-monitoring-in-the-background-and-foreground.html它说,startRangingBeaconsInRegion将使系统扫描灯塔每一秒。 我测试,它是正确的。
但是,一个问题发生,如果程序只无startRangingBeaconsInRegion执行startMonitoringForRegion。
我的程序可以找到灯塔我第一次开始的灯塔硬件后,我停止信标的职能A anaylse didExitRegion被调用。 但之后我开始信标第二次,程序无法找到它在所有(执行didEnterRegion)。 我必须等待1小时。
我使用的测试平台的硬件与iOS 8.1.2和radBeacon USB iPhone 5S。 这里是我的代码。
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
@property (retain, nonatomic) IBOutlet UITextView *textView;
@property (strong, nonatomic) NSMutableString *myLog;
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) CLBeaconRegion *beaconRegion;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self startBeaconMonitoring];
}
- (void)startBeaconMonitoring {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
_locationManager.delegate = self;
[_locationManager performSelector:@selector(requestAlwaysAuthorization)];
[self userGeofencingPreferencesWereUpdatedWithNotification:nil];
[self updateLogWithString:@"start the app"];
}
- (void) userGeofencingPreferencesWereUpdatedWithNotification: (NSNotification *) notification
{
if (1) {
NSUUID *proximityUUID = [[NSUUID UUID] initWithUUIDString:@"EEF45689-BBE5-4FB6-9E80-41B78F6578E2"];
_beaconRegion = [[CLBeaconRegion alloc]
initWithProximityUUID:proximityUUID
identifier:@"1"];
_beaconRegion.notifyEntryStateOnDisplay = YES;
[_locationManager startMonitoringForRegion:_beaconRegion];
//[_locationManager startRangingBeaconsInRegion:beaconRegion];
//[_locationManager stopUpdatingLocation];
}
}
- (void) locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
[self updateLogWithString:@"enter"];
NSLog(@"enter");
}
- (void) locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
[self updateLogWithString:@"exit"];
NSLog(@"exit");
}
- (void)locationManager:(CLLocationManager *)manager
didRangeBeacons:(NSArray *)beacons
inRegion:(CLBeaconRegion *)region {
//NSLog(@"range");
}
- (void)dealloc {
[_textView release];
[_myLog release];
[_locationManager release];
[_beaconRegion release];
[super dealloc];
}
- (NSMutableString *)myLog {
if (!_myLog) {
_myLog = [[NSMutableString alloc] init];
}
return _myLog;
}
- (void) updateLogWithString:(NSString*)newLog {
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"hh:mm:ss";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString * logWithTime = [NSString stringWithFormat:@"%@---%@\n",[dateFormatter stringFromDate:now], newLog];
[self.myLog appendString:logWithTime];
self.textView.text = self.myLog;
[dateFormatter release];
}
@end