2

As per apple documents apps running in background cannot receive callbacks except for apps which are location based, VOIP, Newstand, Music player or APNS enabled. How to receive the connection status callbacks in that case? The app is making use of AVAudioSession instance as:

try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, with:AVAudioSessionCategoryOptions.allowBluetooth)

And listens to notifications as:

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

This is called in the launch of the application.

Rakesh Sankar
  • 9,337
  • 4
  • 41
  • 66
Megha
  • 41
  • 5

1 Answers1

2

You can use the bluetooth-central background mode. According to Background modes documentation

(...) new data arriving from the peripheral causes the system to wake up the app so that it can process the data. The system also wakes up the app to process accessory connection and disconnection notifications.

mag_zbc
  • 6,801
  • 14
  • 40
  • 62
  • But CoreBluetooth allows only BLE communication and not Bluetooth. Since, bluetooth communication from application wasn't possible the application was making use of AVFoundation to know the route changes. But these callbacks were invoked only when app is in foreground and not in background. Plz help! – Megha Apr 10 '18 at 10:29
  • In that case you need background audio AND to actually be playing or recording audio. If these conditions are not met then your app will be descheduled and your route change notifications won’t be delivered until your app comes to the foreground. Seems a bit dirty to need background audio for Bluetooth, but there you are. – Rhythmic Fistman Apr 10 '18 at 12:38
  • But as per our requirements its not necessary that music is being played/recorded. When the music is being played and app is in background the music stops playing when its disconnected with the BT device but the callbacks were not invoked. Any suggestions! – Megha Apr 10 '18 at 15:43
  • I think you're out of luck, unless you take the very dirty route of playing silence. But why do you need to know if a BT audio device is connected? – Rhythmic Fistman Apr 15 '18 at 08:59