无法检测iBeacon蓝牙何时关闭iOS

时间:2014-02-15 14:02:22

标签: ios bluetooth ibeacon

我正在写两个简单的应用程序。一个是信标应用程序,只需按一下按钮即可启动或停止信号。另一个是接收器应用程序,它在检测到信标的信号时编辑标签的文本。

我尝试使用didDetermineStateForRegion,didExitRegion和didEnterRegion方法来检测应用程序何时运行。这些工作可以很好地确定接收器何时移入和移出信标附近,但是大约需要30秒才能确定我已经关闭了信标上的蓝牙。我也尝试将我的CLLocationManager的pausesLocationUpdatesAutomatically字段设置为NO,但同样的事情。理想情况下,它只是立即在我的标签上加上“否”;我该怎么做?

MyView.h

@interface MyView : UIViewController

@property (weak, nonatomic)   IBOutlet UILabel *statusLabel;
@property (strong, nonatomic) CLBeaconRegion   *myBeaconRegion;
@property (strong, nonatomic) CLLocationManager *locationManager;

@end

MyView.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // Initialize location manager and set ourselves as the delegate
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.pausesLocationUpdatesAutomatically=NO;

    // Create a NSUUID with the same UUID as the broadcasting beacon
    NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"ID"];

    // Setup a new region with that UUID and same identifier as the broadcasting beacon
    self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
                                        identifier:@"identifier"];

    // Tell location manager to start monitoring for the beacon region
    [self.locationManager startMonitoringForRegion:self.myBeaconRegion];
}

- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion*)region
{
[self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];
self.statusLabel.text = @"Yes";
}

- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region {
if (state == CLRegionStateInside) {
    self.statusLabel.text = @"Yes";
} else {
    self.statusLabel.text = @"No";
}

}

-(void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion*)region
{
[self.locationManager stopRangingBeaconsInRegion:self.myBeaconRegion];
self.statusLabel.text = @"No";
}

1 个答案:

答案 0 :(得分:2)

区域监控非常慢,您可以使用它进行更一般的通知,以便在您靠近iBeacon时通知您。我认为对于这种情况,您可能希望使用didRangeBeacons,其中您的检测应用程序将每秒通知信标信号强度。您可以使用此信号强度来确定您是否不再看到信标(CoreLocation在消失后几秒钟仍然会“看到”信标)。

只需将以下方法添加到您的委托:

-(void)locationManager:(CLLocationManager*)manager
       didRangeBeacons:(NSArray*)beacons
              inRegion:(CLBeaconRegion*)region

使用以下内容开始为您所在的地区量身打造:

[locationManager startRangingBeaconsInRegion:(CLBeaconRegion*)region];