【问题标题】:Combining Beacon Monitoring with Beacon Ranging to get Major and Minor IDs on didEnterRegion Method将 Beacon Monitoring 与 Beacon Ranging 相结合以在 didEnterRegion 方法上获取主要和次要 ID
【发布时间】:2013-12-17 13:20:22
【问题描述】:

我正在尝试从触发 didEnterRegion 方法的信标中获取“主要”和“次要”ID。有人告诉我,我可以通过将测距和监控结合在一起来做到这一点,但我似乎无法让它正常工作。

我正在使用 Estimote 信标并且正在使用 Estimote API。任何想法这里出了什么问题?谢谢!

这是一个链接,它说您可以将监控和测距结合起来:iBeacon: get major and minor - only looking for uuid

设置:

#import "ViewController.h"
#import "ESTBeaconManager.h"

@interface ViewController () <ESTBeaconManagerDelegate>

@property (nonatomic, strong) ESTBeaconManager* beaconManager;
@property (nonatomic, strong) UIImageView*      bgImageView;
@property (nonatomic, assign) BOOL              notificationShown;
@property (nonatomic, strong) UIImageView*      productImage;

@end

@implementation ViewController

ViewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.beaconManager = [[ESTBeaconManager alloc] init];
    self.beaconManager.delegate = self;
    self.beaconManager.avoidUnknownStateBeacons = YES;

    ESTBeaconRegion* region = [[ESTBeaconRegion alloc] 
    initRegionWithIdentifier:@"EstimoteSampleRegion"];

    [self.beaconManager startMonitoringForRegion:region];
    [self.beaconManager requestStateForRegion:region];
    [self.beaconManager startRangingBeaconsInRegion:region];

    [[NSUserDefaults standardUserDefaults] setObject:@"FALSE" 
    forKey:@"connectedToBeacon"];
    [[NSUserDefaults standardUserDefaults] synchronize];

}

DidRangeBeacons:

-(void)beaconManager:(ESTBeaconManager *)manager
     didRangeBeacons:(NSArray *)beacons
            inRegion:(ESTBeaconRegion *)region {

    NSString *connectedToBeacon = [[NSUserDefaults standardUserDefaults] 
    stringForKey:@"connectedToBeacon"];

    if (connectedToBeacon == FALSE) {

        NSNumber *beaconMajor = region.major;
        NSNumber *beaconMinor = region.minor;

        NSString *alertText = [NSString stringWithFormat:@" Entering (%@,%@)", 
        beaconMajor, beaconMinor];

        UILocalNotification *notification = [[UILocalNotification alloc] init];
        notification.alertBody = alertText;
        notification.soundName = UILocalNotificationDefaultSoundName;

        [[UIApplication sharedApplication] presentLocalNotificationNow:notification];

        [[NSUserDefaults standardUserDefaults] setObject:@"TRUE" 
        forKey:@"connectedToBeacon"];
        [[NSUserDefaults standardUserDefaults] synchronize];

    }
}

【问题讨论】:

  • 请注意,当我进入该区域时,本地通知甚至根本没有触发:)
  • 我会添加两个 NSLog 语句,一个在 didRangeBeacons 方法的顶部,另一个在 if 语句中。你看到一个或两个日志行吗?
  • 为了澄清我在上述问题中的意思,您确定测距是否有效?你的信标工作吗?使用这些日志语句进行测试将帮助您找出答案。
  • 得到它的工作大卫,谢谢!
  • 很高兴听到!如果您有机会,请在此处记录问题所在,以便对遇到类似问题的其他人有所帮助。

标签: ios iphone objective-c ibeacon


【解决方案1】:

我还没有尝试过使用 Estimote 的框架。我一直在使用 Core Location 框架的 Core Location manager、CLBeaconRegion 和 CLBeacon 类,所以我的答案将基于此。

假设它们的工作方式相同,信标测距调用会向您传递一个包含 1 个或多个信标的数组以及它们匹配的区域。

区域中的主要和次要版本值将为零,除非您使用这些值设置区域。

但是,信标对象中的主要和次要值将包含您实际检测到的信标的主要和次要编号。如果您当前检测到多个,则必须提出选择一个的逻辑。我所做的是循环并选择最近的(使用准确性)谁的接近度是未知的。

(已编辑以更正一些错别字)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    相关资源
    最近更新 更多