How to switch to kAudioSessionProperty_OverrideCategoryEnableBluetoothInput without using AudioSessionSetProperty

My iOS6 and working code to set bluetooth as 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 AudioSessionSetProperty method has been deprecated since iOS7. Following this chain How do I direct audio to a speaker without using AudioSessionSetProperty? , you can change the output to AVAudioSessionPortOverrideSpeaker or AVAudioSessionPortOverrideNone, but there are no Bluetooth options.

My actual goal is to support Bluetooth devices that do not use A2DP but HFP.

So how can I achieve this without using obsolete methods?

+3
ios bluetooth avfoundation
Oct 02 '13 at 10:32 on
source share
2 answers

To expand on my previous answer and comment :

You must use the AVAudioSession method

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

with category like
AVAudioSessionCategoryPlayAndRecord
or AVAudioSessionCategoryRecord

and options like
AVAudioSessionCategoryOptionAllowBluetooth

In your answer you say

This is not the same, because it will only allow A2DP Bluetooth connection

But according to Apple docs

AVAudioSessionCategoryOptionAllowBluetooth
Allows Bluetooth hands-free devices to appear as available input routes.

I understand that to refer to Bluetooth HFP, which I believe is what you need. As far as "forcing" is concerned, Apple is not too keen on applications that force / override OS control for user device behavior.

Perhaps this does not work in practice - I could not test it. Presumably you have it, and it fails (you do not indicate in your question). But you fall within the scope of Apple's documentation on this. If you really can't get it to work, I would tend to go with the deprecated C interface and be prepared to make changes for iOS8.

+6
Oct 02 '13 at
source share

Turning to this answer , I came up with the following:

  [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]; } } 

It works the same way as the previous property overrides and redirects both input and output data of the hands-free device.

+2
Oct. 25 '16 at 21:18
source share



All Articles