简体   繁体   English

CoreBluetooth:刷新已发现的外围设备的本地名称

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

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? 有没有办法在没有配对的情况下获得更新?

Apple's bug. Apple的错误。 Still present in iOS 6.1. 仍然存在于iOS 6.1中。 Here is the trick how to reset CB cache: 以下是如何重置CB缓存的技巧:

  1. BackUP device to iCloud. BackUP设备到iCloud。
  2. Reset network settings. 重置网络设置。
  3. Delete your app and install it back via Xode 删除您的应用并通过Xode安装回来
  4. At this point, your peripheral will appear with the new name. 此时,您的外围设备将显示新名称。
  5. Restore your network settings manually or restore from iCloud. 手动恢复网络设置或从iCloud恢复。

Sorry. 抱歉。

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. 您可以在name属性上使用KVO,即使没有连接也可以使用,至少在OS X 10.10中就是这种情况。 I just use this to call the -peripheralDidUpdateName: method myself, and de-dupe calls by tracking the name string. 我只是用它来自己调用-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