I am making an attempt to get my iOS central app to relaunch after a reboot when it detects my peripheral promoting once more. Nevertheless, after following Apple’s Core Bluetooth tips for state restoration, my app just isn’t relaunching routinely.
Anticipated Conduct:
The central app ought to relaunch after a reboot when the peripheral remains to be promoting.
launchOptions?[.bluetoothPeripherals] ought to include the restored peripherals if the app is launched attributable to BLE exercise.
Noticed Conduct:
The app does NOT relaunch after reboot.
Nevertheless willRestoreState will get referred to as.
launchOptions?[.bluetoothPeripherals] is nil when manually opening the app after reboot.
override init() {
tremendous.init()
centralManager = CBCentralManager(delegate: self, queue: nil,choices: [CBCentralManagerOptionShowPowerAlertKey: true, CBCentralManagerOptionRestoreIdentifierKey: "MyCentralManager"])
}
func centralManager(_ central: CBCentralManager, willRestoreState state: [String : Any]) {
if let peripherals = state[CBCentralManagerRestoredStatePeripheralsKey] as? [CBPeripheral] {
print("Restored peripherals: (peripherals)")
let choices: [String: Any] =
[CBConnectPeripheralOptionNotifyOnConnectionKey: true, CBConnectPeripheralOptionNotifyOnDisconnectionKey: true, CBConnectPeripheralOptionNotifyOnNotificationKey: true]
for peripheral in peripherals {
// Optionally, reconnect to any restored peripherals
centralManager.join(peripheral, choices: choices)
}
}
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber) {
print("🔍 Found Peripheral: (peripheral.title ?? "Unknown")")
// Examine if the found peripheral matches the goal machine
if let peripheralName = advertisementData[CBAdvertisementDataLocalNameKey] as? String, peripheralName == "test-peripheral" {
print("✅ Goal Peripheral Discovered: (peripheralName). Stopping scan...")
centralManager.stopScan()
// Save reference to found peripheral
self.discoveredPeripheral = peripheral
// Delay connection to permit time for testing reconnection situations
DispatchQueue.primary.asyncAfter(deadline: .now() + 7) { [self] in
let choices: [String: Any] = [
CBConnectPeripheralOptionNotifyOnConnectionKey: true,
CBConnectPeripheralOptionNotifyOnDisconnectionKey: true,
CBConnectPeripheralOptionNotifyOnNotificationKey: true
]
print("⚡ Connecting to (peripheralName)...")
centralManager.join(peripheral, choices: choices)
}
} else {
print("❌ No matching peripheral discovered.")
}
}
func utility(_ utility: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
print("🚀 AppDelegate: didFinishLaunchingWithOptions referred to as")
if let bluetoothPeripherals = launchOptions?[.bluetoothPeripherals] as? [String] {
print("⚡ App relaunched by iOS attributable to BLE occasion! Restored peripherals: (bluetoothPeripherals)")
showAlert(title: "Peripheral Restoration", message: "Restored peripherals: (bluetoothPeripherals.joined(separator: ", "))")
} else {
print("❌ No peripherals present in launch choices.")
}
return true
}
I added a delay in my didDiscover delegate earlier than it connects in order that I’ve time to background the app after which flip off and again on my iPhone. Nevertheless after unlocking my telephone the app would not relaunch. Any insights can be drastically appreciated! Thanks upfront.
