Friday, December 9, 2022
HomeiOS Developmentswift - iOS Bluetooth Background course of not engaged on time

swift – iOS Bluetooth Background course of not engaged on time


I’m making an attempt to ship knowledge on my bluetooth gadget utilizing app when it’s on background mode. It’s talk to the gadget efficiently however behind schedule. If I would like the background course of begin after 2 minutes, however It begins after 10 minutes.

Right here is my code. And I wish to begin the background course of process after 2 minutes.

func sceneDidEnterBackground(_ scene: UIScene) {
        let date = Date()
        let calendar = Calendar.present
        let hour = calendar.element(.hour, from: date)
        let minutes = calendar.element(.minute, from: date)
        print(minutes)
        print("Scene enter background")
        if MotorControl.motorWorkItem != nil{
            scheduleAppRefresh()
        }
    }

    func scheduleAppRefresh() {

       let request = BGAppRefreshTaskRequest(identifier: "com.instance.apple-samplecode.ColorFeed.refresh")
       request.earliestBeginDate = Date(timeIntervalSinceNow: 2 * 60)
       do {
          strive BGTaskScheduler.shared.submit(request)
       } catch {
          print("Couldn't schedule app refresh: (error)")
       }
    }


SessionManager.runningSeriesIndex = 0

            MotorControl.motorWorkItem = DispatchWorkItem {

                print(SessionManager.runningSeriesIndex)

                print(SessionManager.tempoSeriesData[SessionManager.runningSeriesIndex])

                motor.setSpeedAndTimer(motorSpeedTimer: MotorSpeedTimer(velocity: Int(SessionManager.tempoSeriesData[SessionManager.runningSeriesIndex]), timer: (2), timeNow: Int(Date().timeIntervalSince1970)))

                if SessionManager.runningSeriesIndex < 8{

                    SessionManager.runningSeriesIndex += 1

                    DispatchQueue.primary.async {

                        if UIApplication.shared.applicationState == .background {

                            self.scheduleAppRefresh()

                        }

                        else{

                            DispatchQueue.world(qos: .utility).asyncAfter(deadline: .now() + .seconds(60), execute: MotorControl.motorWorkItem!)

                        }

                    }

                    

                }

                

            }

            DispatchQueue.world(qos: .utility).async(execute: MotorControl.motorWorkItem!)


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

13 − twelve =

Most Popular

Recent Comments