gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:07:01 25 4
gpt4 key购买 nike

我遇到了一个非常奇怪的问题。我正在尝试构建一个应用程序来检测信标。我还没有任何真正的信标,所以我正在用 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. 我怀疑 iPad 传输时的问题不是 iPad 而是 iPhone 无法接收。尝试重新打开 iPhone 电源以清除 known bug in iOS 7.1.

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

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

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

关于ios - iBeacon App 有时只能工作,传输从不工作在 iPhone 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23721979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com