3

我的 iOS6 和将蓝牙设置为输出的工作代码:

// create and set up the audio session
AVAudioSession* audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];
[audioSession setActive: YES error: nil];

// set up for bluetooth microphone input
UInt32 allowBluetoothInput = 1;
OSStatus stat = 0;
stat = AudioSessionSetProperty (
                                         kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,
                                         sizeof (allowBluetoothInput),
                                         &allowBluetoothInput
                                         );

自 iOS7 起,AudioSessionSetProperty 方法已被弃用。遵循此线程如何在不使用 AudioSessionSetProperty 的情况下将音频路由到扬声器?您可以将输出更改为 AVAudioSessionPortOverrideSpeaker 或 AVAudioSessionPortOverrideNone 但此处没有蓝牙选项。

我的实际目标是支持不使用 A2DP 而是使用 HFP 的蓝牙设备。

那么如何在不使用不推荐使用的方法的情况下实现这一点呢?

4

2 回答 2

6

要扩展我之前的回答和评论

您将使用 AVAudioSession 方法

- (BOOL)setCategory:(NSString *)category 
        withOptions:(AVAudioSessionCategoryOptions)options 
              error:(NSError **)outError 

categoryas
AVAudioSessionCategoryPlayAndRecord
AVAudioSessionCategoryRecord

并且options作为
AVAudioSessionCategoryOptionAllowBluetooth

在你的回复中你说

这是不一样的,因为那将只允许 A2DP 蓝牙

但根据苹果文档

AVAudioSessionCategoryOptionAllowBluetooth
允许蓝牙免提设备显示为可用的输入路由。

我理解这意味着蓝牙 HFP,我认为这就是你所追求的。关于“强制”,Apple 不太热衷于应用程序强制/覆盖操作系统对用户设备行为体验的控制。

这可能在实践中不起作用 - 我无法对其进行测试。大概你有,但它失败了(你没有在你的问题中指出)。但是您在此问题上达到了 Apple 文档的限制。如果你真的不能让它工作,我会倾向于使用已弃用的 C 接口,并准备为 iOS8 进行更改。

于 2013-10-02T23:51:29.087 回答
2

通过参考这个答案,我想出了以下内容:

    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:&error];
    NSArray* routes = [audioSession availableInputs];
    for (AVAudioSessionPortDescription* route in routes)
    {
        if (route.portType == AVAudioSessionPortBluetoothHFP)
        {
            [audioSession setPreferredInput:route error:nil];
        }
    }

它似乎与旧属性覆盖的工作方式相同,并将输入和输出重定向到免提设备。

于 2016-10-25T21:18:33.253 回答