swift – iOS Central App Not Relaunching After Reboot Regardless of BLE State Restoration Setup

swift – iOS Central App Not Relaunching After Reboot Regardless of BLE State Restoration Setup


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 shouldn’t be relaunching mechanically.

Anticipated Conduct:
The central app ought to relaunch after a reboot when the peripheral remains to be promoting.

launchOptions?[.bluetoothPeripherals] ought to comprise the restored peripherals if the app is launched as a result of BLE exercise.

Noticed Conduct:
The app does NOT relaunch after reboot.
Nevertheless willRestoreState will get known 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.identify ?? "Unknown")")

    // Verify 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.important.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 software(_ software: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    print("🚀 AppDelegate: didFinishLaunchingWithOptions known as")

    if let bluetoothPeripherals = launchOptions?[.bluetoothPeripherals] as? [String] {
        print("⚡ App relaunched by iOS as a result of 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 vastly appreciated! Thanks upfront.

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 
rooshohttps://www.roosho.com
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Latest Articles

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog.