Can't get bluetooth audio to work in iOS

I can’t for the life of me figure out how to get bluetooth audio (headphones w/ mic) to work in my iOS app. I’ve confirmed the hardware works, I can play music in the music app. But as soon as I open or switch to my app, the music stops, and sounds come out of the iPad speakers instead of the headphones.

I’ve followed these steps to edit the AVAudioSession in Xcode project before building:

and the linked thread in that post.

I was mostly fiddling with the “startUnity” function in UnityAppController.mm:

- (void)startUnity:(UIApplication*)application
{
    NSAssert(_unityAppReady == NO, @"[UnityAppController startUnity:] called after Unity has been initialized");

    UnityInitApplicationGraphics();

    // we make sure that first level gets correct display list and orientation
    [[DisplayManager Instance] updateDisplayListCacheInUnity];

    UnityLoadApplication();
    Profiler_InitProfiler();

    [self showGameUI];
    [self createDisplayLink];

    UnitySetPlayerFocus(1);

    AVAudioSession* audioSession = [AVAudioSession sharedInstance];

   // ***********************************
   // I EDITED THE LINES HERE.... TO...
   // ***********************************

    [audioSession setCategory: AVAudioSessionCategoryAmbient error: nil];
    if (UnityIsAudioManagerAvailableAndEnabled())
    {
        if (UnityShouldPrepareForIOSRecording())
        {
            [audioSession setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];
        }
        else if (UnityShouldMuteOtherAudioSources())
        {
            [audioSession setCategory: AVAudioSessionCategorySoloAmbient error: nil];
        }
    }

   // ***********************************
   // HERE
   // ***********************************

    [audioSession setActive: YES error: nil];
    [audioSession addObserver: self forKeyPath: @"outputVolume" options: 0 context: nil];
    UnityUpdateMuteState([audioSession outputVolume] < 0.01f ? 1 : 0);

#if UNITY_REPLAY_KIT_AVAILABLE
    void InitUnityReplayKit();  // Classes/Unity/UnityReplayKit.mm

    InitUnityReplayKit();
#endif
}

and tried this:

which modifies a different function, applicationDidFinishLaunching

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    ::printf("-> applicationDidFinishLaunching()\n");

   // ***********************************
   // I ADDED THE LINES HERE!!!
   // ***********************************

    // send notfications
#if !PLATFORM_TVOS
    if (UILocalNotification* notification = [launchOptions objectForKey: UIApplicationLaunchOptionsLocalNotificationKey])
        UnitySendLocalNotification(notification);

    if ([UIDevice currentDevice].generatesDeviceOrientationNotifications == NO)
        [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
#endif

    UnityInitApplicationNoGraphics(UnityDataBundleDir());

    [self selectRenderingAPI];
    [UnityRenderingView InitializeForAPI: self.renderingAPI];

#if (PLATFORM_IOS && defined(__IPHONE_13_0)) || (PLATFORM_TVOS && defined(__TVOS_13_0))
    if (@available(iOS 13, tvOS 13, *))
        _window = [[UIWindow alloc] initWithWindowScene: [self pickStartupWindowScene: application.connectedScenes]];
    else
#endif
    _window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].bounds];

    _unityView = [self createUnityView];

    [DisplayManager Initialize];
    _mainDisplay = [DisplayManager Instance].mainDisplay;
    [_mainDisplay createWithWindow: _window andView: _unityView];

    [self createUI];
    [self preStartUnity];

    // if you wont use keyboard you may comment it out at save some memory
    [KeyboardDelegate Initialize];

    return YES;
}

And tried every version of the AVAudioSessionCategory:

I’m pretty sure the settings I need are:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];

Nothing worked. I also tried every combination of “Prepare IOS for Recording” and “Mute Other Audio Sources” in the project settings.

The sounds always play, but out of the iPad’s speakers, not the bluetooth headset.

A couple times, I got it to come out of the bluetooth headset AFTER I booted up my app, then switched to music app, press play there, switch back, and suddenly there was game audio in the speakers along with the music. But I couldn’t replicate this result, and it seemed to happen intermittently.

Note: my app has a voice chat feature using Vivox, so I assume some people will be using bluetooth headphones/mic.

If possible I’d like to mute the music when my app is playing, but I’d take anything at this point.
Any help would be appreciated.

Did you ever figure this out?