2014-02-15 35 views
0

我在寫兩個簡單的應用程序。一個是信號燈應用程序,您可以通過觸摸按鈕來啓動或停止信號。另一個是接收器應用程序,它在檢測到信標信號時編輯標籤的文本。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"; 
} 

回答

2

地區的監測是相當緩慢的,你可以用它進行更廣泛的通知,讓你知道當你是一個iBeacon顯示附近。我認爲在這種情況下,您會希望使用didRangeBeacons,您的檢測應用程序會每秒通知信標信號強度。您可以使用此信號強度來決定是否不再看到信標(CoreLocation會在信號消失幾秒後仍然「看到」信標。

只是下面的方法添加到您的委託:

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

並開始爲您的區域:

[locationManager startRangingBeaconsInRegion:(CLBeaconRegion*)region]; 
+0

是的,但是當應用程序是不是在後臺didRangeBeacons纔有效。如果您的應用程序已最小化,則只能依賴enterRegion和區域監視。 –

+0

這是正確的。請記住,在後臺輸入和退出區域並非超級響應 - 在iOS通知您更改之前可能需要幾分鐘時間。 – csexton

+0

另外,didRangeBeacons沒有被調用,當藍牙關閉 - 只是試圖解決這個問題,可能必須跟蹤自上次didRangeBeacons以來的時間,看看它是否超過2-3秒 - 它被調用否則每隔1秒鐘。 ps:didRangeBeacons確實被調用,如果藍牙打開,但沒有信標 - 周圍有空信標數組。 – zigah