2017-03-08 7 views
8

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) 
} 
+0

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

+0

Cześć Larme, istnieje tylko jeden .portType, gdy odbierze A2DP lub słuchawki, ale nigdy nie odbiera BluetoothHFP. Więcej pomysłów? – richc

Odpowiedz

0

Potrzebowałem dodać setActive po setCategory.

do { try AVAudioSession.sharedInstance().setActive(true) 
     print("setActive") 
    } catch { 
     print(error) 
    } 
0

Spróbuj zmienić konfigurację do kategorii:

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

HFP nie działa z wielu tras kategorii AFAICT.

+0

Witam Austino, dzięki temu spróbowałem PlayAndRecord i nadal nie odbieram HFP. Podnosi A2DP dobrze, ale nie HFP. Więcej pomysłów? – richc

+0

Potrzebowałem również dodać SetActive po SetCategory. Teraz działa dobrze. do {spróbuj AVAudioSession.sharedInstance(). setActive (true) print ("setActive") } catch { print (błąd) } – richc