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.