iBeacon 无法被 iPhone 4S 发现。

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,而其他两种手机可以找到它们。

我在4S手机上进行了[CLLocationManager isMonitoringAvailableForClass:[CLBeacon class]][CLLocationManager isRangingAvailable]测试,结果都没有失败。

请问这是否只是我们的4S手机存在问题,还是4S手机普遍存在此问题?


是的。所有运行iOS 7.0.2/3/4的设备。 - Reapo
你在测试4S时,哪个设备是您设置为信标? - Khaled Barazi
无论是设备、iPad还是5S,我都可以将其设置为信标,而另一个设备可以接收到信号,但4S却不能。 - Reapo
1
我有一台iPhone 4S,我一直用它进行iBeacon测试,所以这不是一个常见问题。这可能是个愚蠢的问题,但你确定4s上启用了蓝牙吗? - davidgyoung
1
你好,Reapo,你的问题解决了吗?我在4S上也遇到了同样的问题,即使使用Locate iB应用程序,除非重新启动iPhone,否则它无法检测到iBeacon。 - sibley
显示剩余2条评论
4个回答

4
我认为你的设备没有问题,因为我也遇到了同样的问题!在5S上相同的代码运行得很好,但在4S上却无法找到信标。看起来,4S的BLE堆栈的工作方式与5S略有不同。
我编写的代码是这样的,直到“locationManager didDetermineState forRegion”被调用并确定我实际上在信标区域内,我才开始搜索信标。这在iPhone 5S上很有效,而且你不会因为没有充分的理由而进行搜索。但是在4S上,你永远不会得到回调!
但是,如果你提前开始搜索,可以通过添加以下代码:
[locationManager startRangingBeaconsInRegion:beaconRegion];

在“locationManager didStartMonitoringForRegion”回调中,它能够很好地工作!

2

我在iPhone4s上也遇到了同样的问题。尽管蓝牙服务和位置服务都开启,但感觉beacon服务都没有运行。

我只是重启了设备,然后它就开始工作了。一开始我很高兴,因为现在它能正常工作了,但我仍然无法弄清楚这个问题。如果最终用户遇到这种情况,他怎么知道需要重新启动设备呢?


我也是这样...我一直在苦苦思索。重启后,它完美地运行了...这种服务的表现非常不可接受,我希望它能在适当的时间唤醒我的应用程序。用户永远不会知道它没有工作。 - Lucas Goossen

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.");
}

在这个答案中发现:检测iOS 7中的后台应用刷新用户设置。

0

听起来你的4s可能有缺陷。为了确定,我建议下载苹果的AirLocate演示(从2013年WWDC)并在所有设备上运行。它将作为信标和“中心”(使用BLE术语),即信标接收器。

我有2个iPhone 4s,它们明显都可以作为信标发射器和接收器,我们的第五代iPod touch(4英寸型号)也是如此。不幸的是,我的iPad 2不支持。迷你版和iPad 3是第一批支持BLE的iPad。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接