0
votes

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
In the article you reference, my tests showed that the scenario you describe leads to a maximum detection delay of 15 minutes on an iPhone 4S with iOS 6 or iOS 7 with a beacon transmitting at 1 Hz -10 Hz. What mobile device type and OS version did you see this on? What type of beacon did you use, and what was its transmission frequency?davidgyoung
Can you please post your code? This may be caused by logic that gets executed on exiting the region.davidgyoung
Thanks for your reply. I edit my question.pkucamel
I'm not very sure about the frequency of radBeacon USB, I think it is faster than 10Hz as my observation.pkucamel
I would send a note to [email protected] and include a link to this question and note the version of your RadBeacon USB firmware (visible in the Radbeacon config app). Mention there may be a problem with your device and that davidgyoung sent you.davidgyoung

1 Answers

0
votes

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.