【发布时间】:2015-07-30 15:34:06
【问题描述】:
我已经在 iBeacons 上玩了几个星期了,如果可以从同一个视图控制器监视两个信标,我很想知道。
例如,到目前为止,我已经制作了一个具有三个单独视图的应用程序,每个视图都会对单独的信标做出反应,根据范围改变内容并触发音乐和视频等动态内容。所有这些视图都是相同的布局,所以如果我可以更改它,我会很痛苦,所以我有一个视图控制器可以根据我附近的信标更改内容,而不必更改视图。
这会更有意义,因为您只能监视一个,这意味着当 segue 触发并且正在监视新信标时,如果您移回第一个信标,它不会在您进入第一个信标时返回该视图接近。
我确信有一种方法可以做到这一点,因为美国的苹果商店正在使用信标来触发促销等,并且他们没有问题地依赖最近的信标。
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ExhibitsViewController : UIViewController <CLLocationManagerDelegate,UIScrollViewDelegate>
{
CLBeacon *beacon;
}
@property (strong, nonatomic) CLBeaconRegion *beaconRegion;
@property (strong, nonatomic) CLLocationManager *locationManager
@end
这是我的头文件中定义信标、beaconRegion 和 locationManager 的代码。
#import "MultipleBeaconsViewController.h"
@interface MultipleBeaconsViewController ()
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *rangeLabel;
@property (weak, nonatomic) IBOutlet UILabel *uuidLabel;
@end
@implementation MultipleBeaconsViewController
NSUUID *iBeacon1uuid;
NSUUID *iBeacon2uuid;
- (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];
[self locationManager:self.locationManager didStartMonitoringForRegion:self.iBeacon1Region];
[self locationManager:self.locationManager didStartMonitoringForRegion:self.iBeacon2Region];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)initRegion {
iBeacon1uuid = [[NSUUID alloc] initWithUUIDString:@"11111111-2222-3333-4444-555555555555"];
_iBeacon1Region = [[CLBeaconRegion alloc] initWithProximityUUID:iBeacon1uuid identifier:@"com.private.Gallery"];
_iBeacon1Region.notifyOnEntry = YES;
_iBeacon1Region.notifyOnExit = YES;
// launch app when display is turned on and inside region
_iBeacon1Region.notifyEntryStateOnDisplay = YES;
if ([CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]])
{
[_locationManager startMonitoringForRegion:_iBeacon1Region];
[_locationManager startRangingBeaconsInRegion:_iBeacon1Region];
}
iBeacon2uuid = [[NSUUID alloc] initWithUUIDString:@"55555555-4444-3333-2222-111111111111"];
_iBeacon2Region = [[CLBeaconRegion alloc] initWithProximityUUID:iBeacon2uuid identifier:@"com.private.Gallery"];
_iBeacon2Region.notifyOnEntry = YES;
_iBeacon2Region.notifyOnExit = YES;
// launch app when display is turned on and inside region
_iBeacon2Region.notifyEntryStateOnDisplay = YES;
if ([CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]])
{
[_locationManager startMonitoringForRegion:_iBeacon2Region];
[_locationManager startRangingBeaconsInRegion:_iBeacon2Region];
}
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
if ([region isKindOfClass:[CLBeaconRegion class]])
{
CLBeaconRegion *beaconRegion = (CLBeaconRegion *)region;
if ([beaconRegion.proximityUUID isEqual: iBeacon1uuid])
{
_titleLabel.text = @"Beacon 1 proximity entered";
}
else if ([beaconRegion.proximityUUID isEqual: iBeacon2uuid])
{
_titleLabel.text = @"Beacon 2 proximity entered";
}
}
}
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
if ([region isKindOfClass:[CLBeaconRegion class]]) {
CLBeaconRegion *beaconRegion = (CLBeaconRegion *)region;
if ([beaconRegion.proximityUUID isEqual: iBeacon1uuid])
{
_titleLabel.text = @"Beacon 1 proximity exited";
}
else if ([beaconRegion.proximityUUID isEqual: iBeacon2uuid])
{
_titleLabel.text = @"Beacon 2 proximity exited";
}
}
}
-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
{
if ([region isKindOfClass:[CLBeaconRegion class]]) {
for (CLBeacon *beacon in beacons)
{
NSString *beaconID = [beacons objectAtIndex:0];
NSLog(@"%@",beaconID);
if ([region.proximityUUID isEqual:iBeacon1uuid])
{
if (beacon.accuracy >=0.000001 && beacon.accuracy <=0.500000)
{
_titleLabel.text = @"Beacon 1";
_rangeLabel.text = [NSString stringWithFormat:@"%f",beacon.accuracy];
_uuidLabel.text = [NSString stringWithFormat:@"%@", beacon.proximityUUID];
}
}
else if ([region.proximityUUID isEqual:iBeacon2uuid])
{
if (beacon.accuracy >=0.000001 && beacon.accuracy <=0.500000) {
_titleLabel.text = @"Beacon 2";
_rangeLabel.text = [NSString stringWithFormat:@"%f",beacon.accuracy];
_uuidLabel.text = [NSString stringWithFormat:@"%@", beacon.proximityUUID];
}
else
{
}
}
}
}
}
这是我在测量信标的实现文件中的代码。
【问题讨论】:
标签: ios objective-c bluetooth-lowenergy ibeacon