2012-11-01 24 views
9

的本地名字,我成功地發現了一個外設,並檢索其本地名稱:CoreBluetooth:刷新已發現周圍

[advertisementData objectForKey:CBAdvertisementDataLocalNameKey] 

但如果外圍停止和重新啓動一個不同的本地名稱的廣告,客戶端不認識到變化。我猜

- (void)peripheralDidUpdateName:(CBPeripheral *)peripheral 

只適用於兩個設備配對。有沒有辦法在沒有配對的情況下獲得更新?

回答

5

蘋果的錯誤。仍然存在於iOS 6.1中。這裏是如何重置CB緩存的技巧:

  1. 備份設備到iCloud。
  2. 重置網絡設置。
  3. 刪除您的應用程序並通過Xode安裝回去
  4. 此時,您的外圍設備將以新名稱顯示。
  5. 手動恢復您的網絡設置或從iCloud恢復。

對不起。

+5

哦男孩哦男孩..的CoreBluetooth API是如此古怪.. (空的UUID,愚蠢的緩存問題.. arrrr) – mindbomb

+2

可悲的是,仍然存在於iOS8上:( – christophercotton

+0

這是越來越荒謬,很無奈。 – Bach

0

即使沒有連接,你也可以在名稱屬性上使用KVO,至少在OS X 10.10中就是這種情況。我只是使用它來自己調用-peripheralDidUpdateName:方法,並通過跟蹤名稱字符串來解除調用。

self.name = self.peripheral.name; 
[self.peripheral addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:NULL]; 

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 
    if([keyPath isEqualToString:@"name"]) { 
     [self peripheralDidUpdateName:self.peripheral]; 
     return; 
    } 
    [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; 
} 


- (void)peripheralDidUpdateName:(CBPeripheral *)peripheral { 
    if([peripheral.name isEqualToString:self.name]) return; 
    if([self.delegate respondsToSelector:@selector(peripheralDidUpdateName:)]) { 
     [self.delegate peripheralDidUpdateName:self]; 
    } 
}