Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to route iPhone audio to the bluetooth headset

I'm attempting to output audio to the bluetooth headset (not A2DP) using AVAudioPlayer, AVAudioSession and AudioSessionSetProperty.

There seems to be functions to select the bluetooth headset as input (kAudioSessionProperty_OverrideCategoryEnableBluetoothInput), but no equivalent for setting the output. This is done in the Voicemail app, where you can select the headset, handset speaker or speaker phone. I've tried various combinations of SessionCategories and the AudioSession properties, but I just can't seem to hit on an approach that works.

I'm sure someone has figured this out, care to share an example?

like image 386
Ethan Avatar asked Mar 03 '10 23:03

Ethan


People also ask

How do I select audio output on iPhone?

On your iPhone, iPad, or iPod touchTouch and hold the audio card in the upper-right corner of Control Center to control audio. Tap , then choose the device that you want from the list.


2 Answers

This little test worked for me... it involves setting up the bluetooth headset as the input also (not sure if that's what you want). Sorry about the crappy formatting on the code...

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

// set up for bluetooth microphone input
UInt32 allowBluetoothInput = 1;
OSStatus stat = AudioSessionSetProperty (
                         kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,
                         sizeof (allowBluetoothInput),
                         &allowBluetoothInput
                        );
NSLog(@"status = %x", stat);    // problem if this is not zero

// check the audio route
UInt32 size = sizeof(CFStringRef);
CFStringRef route;
OSStatus result = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &route);
NSLog(@"route = %@", route);    
// if bluetooth headset connected, should be "HeadsetBT"
// if not connected, will be "ReceiverAndMicrophone"

// now, play a quick sound we put in the bundle (bomb.wav)
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef        soundFileURLRef;
SystemSoundID   soundFileObject;
soundFileURLRef  = CFBundleCopyResourceURL (mainBundle,CFSTR ("bomb"),CFSTR ("wav"),NULL);
AudioServicesCreateSystemSoundID (soundFileURLRef,&soundFileObject);
AudioServicesPlaySystemSound (soundFileObject);     // should play into headset

Hope that helps!

like image 182
Matthias Avatar answered Nov 13 '22 23:11

Matthias


I was able to get this working, but it took some doing. I've pieced together the pertinent piece of code here:

[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&err];

UInt32 allowBluetoothInput = 1;
AudioSessionSetProperty(
    kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,
    sizeof (allowBluetoothInput),
    &allowBluetoothInput);

It is also possible to which sources are available and switch between bluetooth, a headset, handset or the speakerphone but things become very involved at that point. The audio source manager I ultimately wrote was over 700 lines.

like image 29
Ethan Avatar answered Nov 13 '22 22:11

Ethan