How to detect if a bluetooth headset plugged or not IOS 8?

前端 未结 3 863
慢半拍i
慢半拍i 2020-12-30 15:58

In my project, I use AVAudioSession to detect any headphone is plugged or unplugged. But in this case, I can\'t detect when bluetooth device is plugged. Here is

3条回答
  •  我在风中等你
    2020-12-30 16:07

    You can detect currently active bluetooth output devices (instead of input devices)

    Swift Code:

    import AVFoundation
    func bluetoothAudioConnected() -> Bool{
      let outputs = AVAudioSession.sharedInstance().currentRoute.outputs
      for output in outputs{
        if output.portType == AVAudioSessionPortBluetoothA2DP || output.portType == AVAudioSessionPortBluetoothHFP || output.portType == AVAudioSessionPortBluetoothLE{
          return true
        }
      }
      return false
    }
    

    Bluetooth devices are based on the following question: What's the difference among AVAudioSessionPortBluetoothHFP, A2DP and LE?

    I hope it helps someone

提交回复
热议问题