Post

Replies

Boosts

Views

Activity

Two functions of centralManager used
I am trying to code out a background refresh part of my app but there are two centralManager functions in the app. func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { if peripheral.identifier.uuidString == selected { selectedPeripheral = peripheral centralManager?.stopScan() centralManager?.connect(selectedPeripheral!, options: nil) } } func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { print("Connected to peripheral: \(peripheral)") // Subscribe to disconnection events peripheral .publisher(for: \.state) .sink { [weak self] state in if state == .connected { self?.isConnected = true } else if state == .disconnected { // Peripheral is disconnected, send notification if self?.isConnected == true { self?.sendNotification() self?.isConnected = false } } } .store(in: &cancellables) } and this is the refresh part private func handleBluetoothMonitoring(task: BGAppRefreshTask, bluetoothMonitor: BluetoothMonitor) { let queue = DispatchQueue(label: "com.yourcompany.bluetoothmonitor") task.expirationHandler = { // Handle expiration of the task if needed } queue.async { // Perform Bluetooth monitoring tasks here if let centralManagerInstance = bluetoothMonitor.centralManager { DispatchQueue.main.async { bluetoothMonitor.centralManagerDidUpdateState(centralManagerInstance) } } else { print("Central manager is nil") } // End the task task.setTaskCompleted(success: true) } } Is there a way to solve this? Thanks!
1
0
309
May ’24
Push Notifications
Hi, I am trying to add in a push notification but the send push notifications function does not seem to be working. Can someone help? func sendNotification() { print("Bluetooth device disconnected. Sending notification.") let content = UNMutableNotificationContent() content.title = "Car Disconnected" content.body = "Please key in where you have left your car" content.sound = UNNotificationSound.default // Create a trigger to send the notification immediately let request = UNNotificationRequest(identifier: "BluetoothDisconnected", content: content, trigger: nil) UNUserNotificationCenter.current().add(request) { error in if let error = error { print("Error adding notification request: \(error.localizedDescription)") } else { print("Notification request added successfully.") } } }
1
0
452
May ’24