JUCE IOS Bluetooth headphones issue and solution


Hey all,

I encountered a problem with my app (and the juce demo app) on bluetooth headsets. If you attempt to access an input when one of these devices is attached, it switches both the input and the output datasource to the HFP (hands-free) which switches the sample rate to 8k. Usually this is not wanted and makes everything sound awful.

To remedy this, First of all, It is necessary to set the AVAUdioSessionCategory to accept Bluetooth A2DP. This is done by adding AVAudioSessionCategoryOptionAllowBluetoothA2DP to the options in juce_ios_audio’s open function.

Also, I set the perferred input to the last available input that is not a bluetooth HFP input.

// Get the set of available inputs. If there are no audio accessories attached, there will be
    // only one available input -- the built in microphone.
    NSArray* inputs = [session availableInputs];
    // Locate the Port corresponding to the built-in microphone.
    AVAudioSessionPortDescription* perferredInput = nil;
    for (AVAudioSessionPortDescription* port in inputs)
        // ignore BluetoothHFP for input as it will be an 8k sample rate
        if ([port.portType isEqualToString:AVAudioSessionPortBluetoothHFP])
            perferredInput = port;
    NSError* theError = nil;
    auto result = [session setPreferredInput:perferredInput error:&theError];

This is done after the session’s category is set in the same Juce_IOS_Audio.cpp open function. This allows me to use the Ios Built in microphone with the bluetooth output headphones. at a reasonable sample rate.

Currently in the Juce Demo it is impossible to use the bluetooth headphones at any other sample rate than 8k. This change allows you to use the headphone speaker and iphone mic, both at 44100.

Audio to bluetooth headset on iOS

It worked for me too, thanks @Logan_Thomas

@Jules, Would you consider implementing this?