W mojej aplikacji chcę sprawdzić porty portu AVAudio, które są już podłączone do telefonu. Poniższy kod działa dla BluetoothA2DP i słuchawek, ale nie dla BluetoothHFP po podłączeniu telefonu do samochodowego zestawu głośnomówiącego. Czy ktoś może mi pomóc?! Wydaje mi się, że przeszły przez wszystkie posty SO na zestawie głośnomówiącym/AV/Bluetooth i wielu innych, ale nie mogę się domyślić, dlaczego nie rozpoznaje portu wyjściowego BluetoothHFP portType.Aplikacja nie może znaleźć portu AVAudio BluetoothHFP Swift
import AVFoundation
func startCheckAVConnection() {
// set the AVAudioSession to allow bluetooth. This do/try/catch doesn't seem to make a difference if it is here or not.
do{
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, with: AVAudioSessionCategoryOptions.allowBluetooth)
} catch{
print(error)
}
// Check possible outputs for handsfree
let outputs = AVAudioSession.sharedInstance().currentRoute.outputs
if outputs.count != 0 {
for output in outputs {
if output.portType == AVAudioSessionPortBluetoothA2DP {
peripheralLabel.text = "connected to BluetoothA2DP"
} else if output.portType == AVAudioSessionPortBluetoothHFP { // NOT RECOGNISED
peripheralLabel.text = "connected to BluetoothHFP"
} else if output.portType == AVAudioSessionPortHeadphones {
peripheralLabel.text = "connected to Headphones"
}
}
} else {
peripheralLabel.text = "Please connect handsfree"
}
// Add observer for audioRouteChangeListener
NotificationCenter.default.addObserver(
self,
selector: #selector(TopVC.audioRouteChangeListener(_:)),
name: NSNotification.Name.AVAudioSessionRouteChange,
object: nil)
}
Jaką wartość "output.portType" otrzymasz w takim przypadku? Ile masz wyjść? Czy to możliwe, że masz różne, a "peripheralLabel.text" zostanie zastąpione inną wartością? – Larme
Cześć Larme, istnieje tylko jeden .portType, gdy odbierze A2DP lub słuchawki, ale nigdy nie odbiera BluetoothHFP. Więcej pomysłów? – richc