iOS无法再次找到iBeacon [英] iOS Can not find the iBeacon again in fore ground

查看:129
本文介绍了iOS无法再次找到iBeacon的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨〜我正在学习使用CLLocationManager来检测iBeacon.我读了这篇文章: http://developer.radiusnetworks .com/2013/11/13/ibeacon-monitoring-in-the-background-and-foreground.html 它说startRangingBeaconsInRegion将使系统每秒扫描一次信标.我测试,这是正确的.

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.

但是,如果程序仅执行startMonitoringForRegion而没有startRangingBeaconsInRegion,则会发生问题.

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

我的程序在我第一次启动信标硬件时可以找到信标,并且在我停止信标之后,将调用功能didExitRegion.但是在我第二次启动信标之后,程序根本找不到(执行didEnterRegion).我已经等了一个小时.

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.

我用于测试的硬件是具有iOS 8.1.2和radBeacon USB的iPhone 5s. 这是我的代码.

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

推荐答案

我偶然发现了一些东西,这解决了我的问题.当我使用名为Broadcaster的iOS APP模拟iBeacon时,无论前景,背景或屏幕是否关闭,它都会非常快速地触发iBeacon检测APP,该APP可以在其他iOS设备上运行. 当我使用Rad Beacon USB时,仍然会失败.我测试了所有4个Rad Beacon USB,并且发生了相同的事情. Rad Beacon USB发送的BLE消息似乎与iOS模拟的稍有不同(我刚刚尝试过APP Broadcaster). iOS8对待他们 在某些情况下会有所不同.

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.

我的Rad Beacon USB是2.0版.

My Rad Beacon USBs are version 2.0.

这篇关于iOS无法再次找到iBeacon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆