我在這裏有一個非常奇怪的問題。我正在嘗試構建一個應用程序來檢測信標。我還沒有任何真正的信標,所以我正在測試iPhone 5和iPad 3.奇怪的是,傳輸只能在iPad上工作,即使我使用iPhone,iPhone也不能用作發射器它同樣的應用程序。iBeacon應用程序有時只工作,發送從未在iPhone 5上工作
但即使將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 [email protected]"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
調用監視和測距同時確實有助於更快地檢測信標。 我現在還有三個Estimote Beacons用於測試目的,並且這兩個設備都可以接收通知。所以我仍然認爲iPad是麻煩。儘管如此,它現在正在工作,所以感謝與測距和監測技巧。 – deadfishli