4

我遇到了一个涉及 iPhone 4S 的奇怪问题。我正在开发一个使用 iBeacons 的应用程序。以下代码是在 iPad mini、iPhone 5s 和 iPhone 4s 上运行的,但只有 iPad 和 5S 在遇到 iBeacon 时能够做出响应,而 4S 什么也不做。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    //start ibeacon monitoring mode
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
    [self enableMonitoring];
}

- (void)enableMonitoring
{
    [self initRegion];
    [self locationManager:self.locationManager didStartMonitoringForRegion:self.beaconRegion];
}

- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
{
    [self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
}

- (void) initRegion
{
    self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:UUID]
                                                           identifier:@"iBeacon"];
    [self.locationManager startMonitoringForRegion:self.beaconRegion];
    hasAlerted = NO;
}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
    ST_UNUSED(manager);
    ST_UNUSED(region);
    NSLog(@"Beacon Found");
    [self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
}

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    ST_UNUSED(manager);
    ST_UNUSED(region);
    NSLog(@"Left Region");
    [self.locationManager stopRangingBeaconsInRegion:self.beaconRegion];
}

- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
    ST_UNUSED(manager);
    ST_UNUSED(region);
    CLBeacon *beacon = [beacons lastObject];

    // stuff gets done here

}

4S可以毫无问题地广播iBeacons,另外2个可以找到它们。

我在4S上做过[CLLocationManager isMonitoringAvailableForClass:[CLBeacon class]][CLLocationManager isRangingAvailable]测试,他们没有失败。

如果这只是我们4S的问题,或者是一般的4S问题,谁能告诉我?

4

4 回答 4

4

我不认为您的设备有缺陷,因为我遇到了完全相同的问题!相同的代码在 5S 上运行良好,但没有找到使用 4S 的信标。事实证明(看起来)4S BLE 堆栈的工作方式与 5S 略有不同。

按照我编写代码的方式,直到调用了“locationManager didDetermineState forRegion”并且我确定我实际上在信标区域内,我才开始对信标进行测距。这在 iPhone 5S 上效果很好,而且你没有充分的理由不进行测距。但是在 4S 上你永远不会收到回电!

但是,如果你提前开始测距:

[locationManager startRangingBeaconsInRegion:beaconRegion];

在“locationManager didStartMonitoringForRegion”回调中,它就像一个冠军!

于 2014-01-16T00:25:58.557 回答
2

我在“iPhone4s”中也遇到了同样的问题。尽管开启了蓝牙服务和定位服务,但感觉好像没有任何信标服务正常工作。

我刚刚重新启动它,它开始工作。有那么一刻我很高兴,因为它现在正在工作,但是我仍然无法弄清楚这是什么问题。如果最终用户发生这种情况,他将如何知道他需要重新启动他的设备?

于 2014-04-09T11:56:30.693 回答
1

我有一个与此问题中提到的类似的问题: IBeacon 区域监控在设备之间无法始终如一地工作

我发现区别在于设备是否启用了后台刷新。要检查这一点,我使用以下代码段:

if ([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusAvailable) {

    NSLog(@"Background updates are available for the app.");
}else if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusDenied)
{
    NSLog(@"The user explicitly disabled background behavior for this app or for the whole system.");
}else if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusRestricted)
{
    NSLog(@"Background updates are unavailable and the user cannot enable them again. For example, this status can occur when parental controls are in effect for the current user.");
}

在这个答案中找到:Detecting user settings for Background App Refresh in iOS 7

于 2014-01-30T11:50:12.287 回答
0

听起来你可能有一个有缺陷的4s。可以肯定的是,我建议下载 Apple 的 AirLocate 演示(来自 2013 WWDC)并在所有设备上运行它。它将作为信标和“中央”(使用 BLE 术语),即信标接收器。

我有 2 部 iPhone 4s,它们绝对可以作为信标发射器和接收器工作,我们的第 5 代 iPod touch(4 英寸型号)也是如此。可悲的是,我的 iPad 2 没有。mini 和 iPad 3 是第一款支持 BLE 的 iPad。

于 2013-12-03T00:18:46.383 回答