Ios – How to route to kAudioSessionProperty_OverrideCategoryEnableBluetoothInput without using AudioSessionSetProperty

avfoundationbluetoothios

My iOS6 and working code to set bluetooth as an output:

// 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
                                         );

The method AudioSessionSetProperty is deprecated since iOS7. Following this thread How Do I Route Audio to Speaker without using AudioSessionSetProperty? you can change the output to AVAudioSessionPortOverrideSpeaker OR AVAudioSessionPortOverrideNone but no Bluetooth options here.

My actual goal is to support bluetooth devices who are not using A2DP but HFP.

So how can I achieve this without using deprecated methods?

Best Answer

To expand on my previous answer and comment:

You would use the AVAudioSession method

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

with category as
AVAudioSessionCategoryPlayAndRecord
or AVAudioSessionCategoryRecord

and options as
AVAudioSessionCategoryOptionAllowBluetooth

In your reply you say

that is not the same because that would allow A2DP bluetooth only

But according to the Apple docs

AVAudioSessionCategoryOptionAllowBluetooth
Allows Bluetooth handsfree devices to appear as available input routes.

I understand that to mean bluetooth HFP, which I presume is what you are after. As regards "forcing", Apple is not too keen on apps forcing/overriding OS control of a user's experience of device behaviour.

It may be that this does not work in practice - I have not been able to test it. Presumably you have, and it fails (you don't indicate in your question). But you are hitting the limits of Apple's documentation on this issue. If you really can't get it to work I would be inclined to go with the deprecated C interface, and be prepared to make changes for iOS8.