Detect car in background (Via bluetooth, MotionActivity or other ?)

Hi guys,

I'm trying to detect when the users of my app are entering/leaving their car. Our app helps people with parking regulations in cities so it can be more than useful to have such detection.


Is it possible to detect Bluetooth connections/disconnections, even in Background ? It seems to be possible but only with BLE devices with CoreBluetooth. But cars are mainly using A2DP or HPF, thus it's not BLE... For those Classic Bluetooth devices, I saw ExternalAccessory Framework but id doesn't seem to work in background for Bluetooth detections.


I also tried to use the MotionActivity framework, the idea would be to detect the switch from walgint to driving or the opposit. But it seems it's not working in background, right ?


Another solution would be to use the Location in background, but its really not power effective and we don't want to kill the battery of our users.


Do I miss something ? Is there another solution ? Is the MotionActivity working in background and does it wake the app to process the data ?


What's really frustrating is that on Android, it took nearly no time to develop this feature and for iOS, I don't see any clear soluton for two days in a row now...


Thanks a lot for your help !

Replies

If only you supported CarPlay...

You can use AVFoundation

do {
      try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: [.allowBluetooth, .allowBluetoothA2DP, .allowAirPlay
      try audioSession.setActive(true)
} catch {
     fatalError("Audio session failure")
}


and add an observer

NotificationCenter.default.addObserver(self,
                                               selector: #selector(handleRouteChange),
                                               name: .AVAudioSessionRouteChange,
                                               object: AVAudioSession.sharedInstance())

@objc func handleRouteChange(_ notification: Notification) {
        guard let userInfo = notification.userInfo,
            let reasonValue = userInfo[AVAudioSessionRouteChangeReasonKey] as? UInt,
            let reason = AVAudioSessionRouteChangeReason(rawValue:reasonValue) else {
                return
        }
        switch reason {
        case .newDeviceAvailable:
            print("Handle new device available.")
            let session = AVAudioSession.sharedInstance()
            for output in session.currentRoute.outputs where output.portType == AVAudioSessionPortHeadphones {
            }
        case .oldDeviceUnavailable:
            print("Handle old device removed.")
            if let previousRoute =
                userInfo[AVAudioSessionRouteChangePreviousRouteKey] as? AVAudioSessionRouteDescription {
                for output in previousRoute.outputs where output.portType == AVAudioSessionPortHeadphones {
                }
            }
        default: ()
        }
     }
Hi, did you ever solve it? I am experiencing the exact same problem.
Do no try to do this using a driver’s personal smartphone. We do the exact same feature directly from the car. Feel free to contact me for more details. Regards
  • hey, how can I contact you?. I am also trying the exact same scenario. How you guys have achieved it? please if possible write me an email or reply here.

  • ^^^ Looking to solve the exact same problem

Add a Comment