我正在编写两个简单的应用程序。一个是信标应用程序,它的信号可以通过触摸按钮启动或停止。另一个是接收器应用程序,当它检测到信标信号时,它会编辑标签的文本。
我已经尝试使用didDetermineStateForRegion、didExitRegion和didEnterRegion方法来检测应用程序何时运行。这些可以很好地确定接收器何时移入和移出信标,但大约需要30秒才能确定我已经关闭了信标上的蓝牙。我也尝试过将CLLocationManager的pausesLocationUpdatesAutomatic字段设置为NO,但情况相同。理想情况下,它会立即在我的标签上写上“不”;我该怎么做?
我的视图.h
@interface MyView : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
@property (strong, nonatomic) CLBeaconRegion *myBeaconRegion;
@property (strong, nonatomic) CLLocationManager *locationManager;
@end
我的视图.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";
}