簡體   English   中英

CoreBluetooth:刷新已發現的外圍設備的本地名稱

[英]CoreBluetooth: Refreshing local name of an already discovered Peripheral

我成功發現了外圍設備並檢索其本地名稱:

[advertisementData objectForKey:CBAdvertisementDataLocalNameKey]

但是,如果外圍設備停止並重新啟動具有不同本地名稱的廣告,則客戶端無法識別該更改。 我猜

- (void)peripheralDidUpdateName:(CBPeripheral *)peripheral

僅在兩個設備配對時才有效。 有沒有辦法在沒有配對的情況下獲得更新?

Apple的錯誤。 仍然存在於iOS 6.1中。 以下是如何重置CB緩存的技巧:

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

抱歉。

您可以在name屬性上使用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];
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM