菜鸟教程小白 发表于 2022-12-13 11:20:20

ios - CoreBluetooth状态保存: correct way to restore CBCentralManager


                                            <p><p>当应用由于状态保存事件而获得选项午餐时,从 AppDelegate 恢复 CBCentralManager 的正确方法是什么?</p>

<pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // The system provides the restoration identifiers only for central managers that had active or pending peripheral connections or were scanning for peripherals.
    NSArray * centralManagerIdentifiers = launchOptions;

    if (centralManagerIdentifiers != nil) {
      for (int i=0; i&lt;; i++) {
            NSString * identifier = ;
            NSLog(@&#34;bluetooth central key identifier %@&#34;, identifier);
            // here I expect to re-instatiate the CBCentralManager but not sure how and if this is the best place..
      }
    }

    // Override point for customization after application launch.
    return YES;
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>当您获得标识符列表时,您必须遍历此列表并为每个标识符初始化 <code>CBCentralManager</code> 的实例。列表包含 <code>NSString</code> 的对象。</p>

<pre><code>NSArray *centralManagerIdentifiers = launchOptions;

for (NSString *centralManagerIdentifier in centralManagerIdentifiers) {
    CBCentralManager *centralManager = [ initWithDelegate:self
                                                                            queue:nil
                                                                        options:@{CBCentralManagerOptionRestoreIdentifierKey: centralManagerIdentifier}];

    ;
}
</code></pre>

<p>更多详情请引用<a href="https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html#//apple_ref/doc/uid/TP40013257-CH7-SW1" rel="noreferrer noopener nofollow" title="Core Bluetooth Programming Guide">State Preservation and Restoration</a>在核心蓝牙编程指南中。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - CoreBluetooth状态保存: correct way to restore CBCentralManager,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/33125295/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/33125295/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - CoreBluetooth状态保存: correct way to restore CBCentralManager