20.3 C
Canberra
Thursday, October 30, 2025

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 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.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles