gpt4 book ai didi

ios - 核心蓝牙状态恢复

转载 作者:行者123 更新时间:2023-11-29 03:04:52 24 4
gpt4 key购买 nike

我正在开发一个对外围设备断开连接使用react的应用程序,我现在正尝试采用 iOS 7 中引入的 ne 状态保存和恢复。

我做了文档中所说的一切,意思是:

  1. 我为 centrals 添加了后台模式。

  2. 我总是用相同的唯一实例化我的中央管理器 标识符。

  3. 我实现了 centralManager:willRestoreState: 方法。

当我的应用移至后台时,我在 AppDelegate 回调中使用 kill(getpid(), SIGKILL); 将其终止。 ( Core Bluetooth State Preservation and Restoration Not Working, Can't relaunch app into background )

当我现在通过移除电池断开外围设备时,我的应用程序被按预期唤醒并且 launchOptions[UIApplicationLaunchOptionsBluetoothCentralsKey] 包含正确的标识符,但 centralManager:willRestoreState:没有被调用。只有当我断开另一个外围设备时,才会调用此方法。

最佳答案

这就是我的方式:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{

NSArray *peripheralManagerIdentifiers = launchOptions[UIApplicationLaunchOptionsBluetoothPeripheralsKey];

if (peripheralManagerIdentifiers) {

// We've restored, so create the _manager on the main queue
_manager = [[CBPeripheralManager alloc] initWithDelegate:self
queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
options:@{CBPeripheralManagerOptionRestoreIdentifierKey:@"YourUniqueIdentifier"}];

} else {

// Not restored so just create as normal
manager = [[CBPeripheralManager alloc] initWithDelegate:self
queue:nil
options:@{CBPeripheralManagerOptionRestoreIdentifierKey:@"YourUniqueIdentifier"}];

}
return YES;
}

然后:

- (void)peripheralManager:(CBPeripheralManager *)peripheral
willRestoreState:(NSDictionary *)dict
{


// This is the advertisement data that was being advertised when the app was terminated by iOS
_advertisementData = dict[CBPeripheralManagerRestoredStateAdvertisementDataKey];

NSArray *services = dict[CBPeripheralManagerRestoredStateServicesKey];

// Loop through the services, I only have one service but if you have more you'll need to check against the UUID strings of each
for (CBMutableService *service in services) {

_primaryService = service;

// Loop through the characteristics
for (CBMutableCharacteristic *characteristic in _primaryService.characteristics) {

if ([characteristic.UUID.UUIDString isEqualToString:CHARACTERISTIC_UUID]) {

_primaryCharacteristic = characteristic;

NSArray *subscribedCentrals = characteristic.subscribedCentrals;

// Loop through all centrals that were subscribed when the app was terminated by iOS
for (CBCentral *central in subscribedCentrals) {

// Add them to an array
[_centrals addObject:central];

}
}
}
}
}

关于ios - 核心蓝牙状态恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22897891/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com