iBeacon App 有时只能工作,在 iPhone 5 上传输从不工作

Posted

技术标签:

【中文标题】iBeacon App 有时只能工作,在 iPhone 5 上传输从不工作【英文标题】:iBeacon App only working sometimes, transmitting never working on iPhone 5 【发布时间】:2014-05-18 12:38:42 【问题描述】:

我在这里遇到了一个非常奇怪的问题。我正在尝试构建一个应用程序来检测信标。我还没有任何真正的信标,所以我正在使用 iPhone 5 和 iPad 3 进行测试。奇怪的是,传输只能在 iPad 上工作,即使我使用了 iPhone 也不能作为发射器工作相同的应用程序。

但即使使用 iPad 作为发射器,该应用程序也只能在某些时候工作 - 有时 iPhone 会通知我它找到了信标,有时却没有。我已经强制关闭了 iPad 上的应用程序,重新启动后它可以工作,但又一次没有。 由于一切都在零星地工作,我认为它不可能是导致这种行为的代码,但它可能是 - 我不是一个经验丰富的编码器,我刚刚开始这样做。我的代码基于本教程http://www.devfright.com/ibeacons-tutorial-ios-7-clbeaconregion-clbeacon/

我最初认为this 可能是答案,但并没有解决它:它有时仍然有效,有时却无效。

谁能告诉我我在和她打交道吗?

这是我的 Tracker 代码:

ladBeaconTracker.h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <CoreBluetooth/CoreBluetooth.h>

@interface ladBeaconTracker : UIViewController <CLLocationManagerDelegate>

@property (strong, nonatomic) CLBeaconRegion *beaconRegion;
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (weak, nonatomic) IBOutlet UILabel *beaconFoundLabel;

@end

ladBeaconTracker.m

#import "ladBeaconTracker.h"

    @interface ladBeaconTracker ()

    @property NSUUID *uuid;

@end  

@implementation ladBeaconTracker

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
        // Custom initialization
    
    return self;


- (void)viewDidLoad

    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;

    [self initRegion];
  

- (void)initRegion 
    NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"A5456D78-C85B-44C6-9F20-8268FD25EF8A"];
    self.beaconRegion = [[CLBeaconRegion alloc]initWithProximityUUID:uuid identifier:@"Museum"];

    [self.locationManager startMonitoringForRegion:self.beaconRegion];

    NSLog(@"Region %@ initated", _beaconRegion.identifier);


- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region  
    [self.locationManager startRangingBeaconsInRegion:self.beaconRegion];       
    self.beaconRegion.notifyEntryStateOnDisplay = YES;

    NSLog(@"Region %@ entered", _beaconRegion.identifier);


- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region 
    [self.locationManager stopRangingBeaconsInRegion:self.beaconRegion];

    NSLog(@"Region %@ exit", _beaconRegion.identifier);


- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
    CLBeacon *beacon = [[CLBeacon alloc] init];
    beacon= [beacons lastObject];    
    self.beaconFoundLabel.text =@"Yes";

    NSLog(@"Ranged Region %@", _beaconRegion.identifier );



- (void)didReceiveMemoryWarning

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.

*/

@end

这是发射器代码:

configViewController.h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <CoreBluetooth/CoreBluetooth.h>

@interface ConfigViewController : UIViewController <CBPeripheralManagerDelegate>

@property (strong,nonatomic) CLBeaconRegion *beaconRegion;
@property(strong,nonatomic) NSDictionary *beaconPeripheralData;
@property (strong, nonatomic) CBPeripheralManager *PeripheralManager;

@end

ConfigViewController.m

#import "ConfigViewController.h"

@interface ConfigViewController ()

@end

@implementation ConfigViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
        // Custom initialization
    
    return self;


- (void)viewDidLoad

    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initBeacon];  


- (void) peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral 
    if (peripheral.state == CBPeripheralManagerStatePoweredOn) 
        NSLog(@"Powered ON");
        [self.PeripheralManager startAdvertising:self.beaconPeripheralData];
    
    else if (peripheral.state == CBPeripheralManagerStatePoweredOff)
        NSLog(@"Powered OFF");
        [self.PeripheralManager stopAdvertising];
    


- (void)initBeacon 
    NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"A5456D78-C85B-44C6-9F20-8268FD25EF8A"];
    self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
                                                major:1
                                                minor:1
                                                identifier:@"Museum"];


- (IBAction)transmitBeacon:(UIButton *)sender 
    self.beaconPeripheralData = [self.beaconRegion peripheralDataWithMeasuredPower:nil];
    self.PeripheralManager = [[CBPeripheralManager alloc]initWithDelegate:self queue:nil options:nil];



- (void)didReceiveMemoryWarning

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


@end

【问题讨论】:

【参考方案1】:

您的代码看起来很适合在后台检测信标。两个建议:

    我怀疑 iPad 传输时的问题不是 iPad 而是 iPhone 无法接收。尝试重启 iPhone 以清除 known bug in iOS 7.1.

    如果您在设置监控的同时设置测距,iOS 可以更快地在前台检测 iBeacons。将[self.locationManager startRangingBeaconsInRegion:self.beaconRegion]; 移动到initRegion 并完全取出stopRangingBeaconsInRegion

完成 (2) 后,在前台重复测试并查找日志语句 Ranged Region。当信标打开时,您应该每秒都能看到这一点。

在后台,知道it can take up to 15 minutes 既检测到 iBeacon 又检测​​到 iBeacon 不再存在。同样,请注意您的日志行以获取有关当前状态的提示,并知道在您第一次获得区域退出事件或重新启动手机之前,您无法获得新的区域进入事件。

【讨论】:

同时调用监控和测距确实有助于更快地检测信标。我现在还获得了三个 Estimote Beacon 用于测试目的,并且这两个设备都可以接收通知。因此,我仍然认为 iPad 是问题所在。尽管如此,它现在正在工作,所以感谢您提供有关测距和监控的提示。

以上是关于iBeacon App 有时只能工作,在 iPhone 5 上传输从不工作的主要内容,如果未能解决你的问题,请参考以下文章

ibeacon是啥?能实现哪些功能?

iOS 中 iBeacon 开发

ibacon 如何在后台工作?

接近 API 的不一致行为 - iOS iBeacon

iBeacon-App:从锁屏启动时的自定义代码

机场室内导航,可以实现吗?