繁体   English   中英

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

[英]Can't detect when iBeacon bluetooth is turned off iOS

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

我尝试使用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";
}

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

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

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

并开始为您的地区提供:

[locationManager startRangingBeaconsInRegion:(CLBeaconRegion*)region];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM