iOS Can not find the iBeacon again in fore ground

微笑、不失礼 提交于 2019-12-25 04:45:38

问题


Hi~ I'm learning to use CLLocationManager to detect iBeacon. I read this article: http://developer.radiusnetworks.com/2013/11/13/ibeacon-monitoring-in-the-background-and-foreground.html It says that startRangingBeaconsInRegion will make system scan beacon every second. I test and it's right.

But a problem happens if the program only execute startMonitoringForRegion without startRangingBeaconsInRegion.

My program can find the beacon first time I start a beacon hardware, and after I stop beacon the founction didExitRegion is called. But after I start the beacon second time, program cannot find it(execute didEnterRegion) at all. I have wait for 1 hour.

The Hardware I use for test are iPhone 5s with iOS 8.1.2 and radBeacon USB. Here is my code.

#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

回答1:


I found something by accident and this solve my problem. When I use a iOS APP named Broadcaster to simulate a iBeacon, it triggers iBeacon detection APP very quickly which run on another iOS device no matter in foreground, background or screen off. When I use Rad Beacon USB, still failure. I test all my 4 Rad Beacon USBs and same thing happens. It seems that the BLE message sent by Rad Beacon USB is a little different from which simulated by iOS(I just tried the APP Broadcaster). And iOS8 treats them different in some situations.

My Rad Beacon USBs are version 2.0.



来源:https://stackoverflow.com/questions/27961587/ios-can-not-find-the-ibeacon-again-in-fore-ground

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!