CoreBluetooth: Refreshing local name of an already discovered Peripheral

∥☆過路亽.° 提交于 2019-12-05 17:10:09

问题


I successfully discover a Peripheral and retrieve its local name:

[advertisementData objectForKey:CBAdvertisementDataLocalNameKey]

But if the Peripheral stops and restarts advertising with a different local name, the Client doesn't recognise the change. I guess

- (void)peripheralDidUpdateName:(CBPeripheral *)peripheral

only works if the two devices are paired. Is there a way to get an update without pairing?


回答1:


Apple's bug. Still present in iOS 6.1. Here is the trick how to reset CB cache:

  1. BackUP device to iCloud.
  2. Reset network settings.
  3. Delete your app and install it back via Xode
  4. At this point, your peripheral will appear with the new name.
  5. Restore your network settings manually or restore from iCloud.

Sorry.




回答2:


You can use KVO on the name property which will work even when not connected, at least this is the case in OS X 10.10. I just use this to call the -peripheralDidUpdateName: method myself, and de-dupe calls by tracking the name string.

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];
    }
}


来源:https://stackoverflow.com/questions/13180134/corebluetooth-refreshing-local-name-of-an-already-discovered-peripheral

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!