2017-03-08 12 views
8

私のアプリでは、既に電話に接続されているAVAudioのポートタイプを確認したいと思います。以下のコードは、BluetoothA2DPとヘッドフォンで動作しますが、電話機を車のハンズフリーに接続するとBluetoothHFPでは動作しません。誰でも助けてくれますか?私はハンズフリー/ AV /ブルートゥース、および多くの他のすべてのSOの投稿をしていると思うが、それがBluetoothHFPの出力ポートの種類を認識していない理由を解決することはできません。アプリが見つかりません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

「output.portType」にはどのような値がありますか?いくつの出力がありますか?あなたはいろんなことがあり、 'peripheralLabel.text'は別の値で上書きされますか? – Larme

+0

こんにちは、A2DPまたはヘッドフォンをピックアップするときに.portTypeが1つしかありませんが、BluetoothHFPは決してピックアップしません。これ以上のアイデアは? – richc

答えて

0

setCategoryの後にsetActiveを追加する必要がありました。

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

にあなたのカテゴリの設定を変更してみてください:

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

HFPは、マルチルートカテゴリAFAICTでは動作しません。

+0

こんにちはAustino、感謝私はPlayAndRecordを試して、まだHFPを手に入れません。 A2DPではなく、HFPを選択します。これ以上のアイデアは? – richc

+0

SetCategoryの後にSetActiveを追加する必要もありました。今はうまくいく。 (不正) 印刷(エラー) } {AVAudioSession.sharedInstance()を試してください。 – richc

関連する問題