title: AVAudioSession and AirPods
body: <p>It seems that my app is not working with AirPods. Right now I'm using this code for the playback and record:</p>
  
  <pre><code>    let audioSession = AVAudioSession.sharedInstance()
      do { try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: AVAudioSessionCategoryOptions.defaultToSpeaker)
      }catch {
          print("audioSession properties weren't set because of an error.")
      }
  </code></pre>
  
  <p>Will it be enough if I change <code>defaultToSpeaker</code> to <code>allowBluetooth</code>?</p>
  
  <p>P.S. I know it's quite a stupid question because it'd be much simpler to just change this line and check, but I don't have AirPods with me right now, so the only option for me is to upload the new build to Testflight (and I want to do this with minimum iterations).</p>
  
  <p><strong>update:</strong> (quite naive approach — but all I need is to use bluetooth headphones if they are available):</p>
  
  <pre><code>func selectDevice(audioSession: AVAudioSession) {
  
      var headphonesExist = false
      var bluetoothExist = false
      var speakerExist = false
  
      let currentRoute = AVAudioSession.sharedInstance().currentRoute
  
      for output in audioSession.currentRoute.outputs {
          print(output)
  
          if output.portType == AVAudioSessionPortHeadphones || output.portType == AVAudioSessionPortHeadsetMic {
              headphonesExist = true
          }
  
          if output.portType == AVAudioSessionPortBluetoothA2DP || output.portType == AVAudioSessionPortBluetoothHFP {
              bluetoothExist = true
          }
  
          if output.portType == AVAudioSessionPortBuiltInSpeaker {
              speakerExist = true
          }
      }
  
      if bluetoothExist == true {
          do { try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: AVAudioSessionCategoryOptions.allowBluetooth) } catch {
              print("error with audiosession: bluetooth")
          }
      }
  }
  </code></pre>