2

I am using the following code to detect available outputs and it isn't picking up my connected bluetooth device. I know that my bluetooth device is properly connected because I am playing music from it.

let session = AVAudioSession.sharedInstance()
        for description in session.currentRoute.outputs {
            if description.portType == AVAudioSessionPortHeadphones {
                NSLog("headphone plugged in")
                let alertActin = UIAlertAction(title: "Headphones", style: UIAlertActionStyle.default, handler:nil)
                alertActin.setValue(UIColor.blue, forKey: "titleTextColor")
                alert.addAction(alertActin)
            } else if description.portType == AVAudioSessionPortBluetoothA2DP {
                NSLog("Bluetooth plugged in")

                let alertActin = UIAlertAction(title: description.portName, style: UIAlertActionStyle.default, handler:nil)
                alertActin.setValue(UIColor.blue, forKey: "titleTextColor")
                alert.addAction(alertActin)
            } else if description.portType == AVAudioSessionPortBluetoothLE {
                NSLog("Bluetooth plugged in")

                let alertActin = UIAlertAction(title: description.portName, style: UIAlertActionStyle.default, handler:nil)
                alertActin.setValue(UIColor.blue, forKey: "titleTextColor")
                alert.addAction(alertActin)
            }
            else {
                NSLog("")
            }
        }
user1079052
  • 3,803
  • 4
  • 30
  • 55

1 Answers1

1

Perhaps you need to instruct AVAudioSession to allow Bluetooth outputs first as suggested here.

Note that this post discurages use of AVAudioSessionCategoryPlayAndRecord and recommends using AVAudioSessionCategoryPlayback when working with output devices.

orhtej2
  • 2,133
  • 3
  • 16
  • 26
  • I added this to my code but my bluetooth speakers still aren't appearing do { try session.setCategory(AVAudioSessionCategoryPlayAndRecord, with: [.allowBluetooth]) try session.setActive(true) } catch { fatalError("Error Setting Up Audio Session") } – user1079052 Aug 02 '17 at 19:16