我在寫兩個簡單的應用程序。一個是信號燈應用程序,您可以通過觸摸按鈕來啓動或停止信號。另一個是接收器應用程序,它在檢測到信標信號時編輯標籤的文本。iBeacon藍牙關閉時無法檢測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";
}
是的,但是當應用程序是不是在後臺didRangeBeacons纔有效。如果您的應用程序已最小化,則只能依賴enterRegion和區域監視。 –
這是正確的。請記住,在後臺輸入和退出區域並非超級響應 - 在iOS通知您更改之前可能需要幾分鐘時間。 – csexton
另外,didRangeBeacons沒有被調用,當藍牙關閉 - 只是試圖解決這個問題,可能必須跟蹤自上次didRangeBeacons以來的時間,看看它是否超過2-3秒 - 它被調用否則每隔1秒鐘。 ps:didRangeBeacons確實被調用,如果藍牙打開,但沒有信標 - 周圍有空信標數組。 – zigah