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])
            continue;
        else
            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.

1 Like

It worked for me too, thanks @Logan_Thomas

@Jules, Would you consider implementing this?

Edit: Sorry. I didn’t read the post accurate enough…
We have problems with bluetooth headphones (not microphones). We only get a mono connection (44.1 kHz) and crackles.
The AUv3 version running in Garageband works fine (apart from the latency).
Did you solve this problem, too?

Addition: If I first start the standalone app (with bluetooth off) and then enable bluetooth for my headphones, then everything works fine. The other way round (first enable bluetooth, then start the app) results in a monophonic connection.

This doesn’t sound like the same problem that the OP is describing. Can you make a separate thread describing the issue that you are having in more detail and with detailed steps to reproduce?

Hi Ed. Ok, no problem, I will open a new thread, although the title of this thread is misleading and would fit very well to my problem.