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

时间:2014-05-18 12:38:43

标签: ios objective-c ibeacon

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

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

我首先想到this可能是答案,但是没有解决它:它有时仍然有效,有时它没有。

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

以下是我的跟踪器代码:

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 个答案:

答案 0 :(得分:2)

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

  1. 我怀疑iPad发送时的问题不是iPad,而是iPhone无法接收。尝试重启iPhone电源以清除known bug in iOS 7.1.

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

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

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