How to make sound output from Bluetooth instead of speakers in IOS 17

When I set the AVAudioSessionCategory avAudioSessionCategory: AVAudioSessionCategory.playAndRecord, sound to come out of the built-in speaker instead of Bluetooth headphones。ios17 。It is normal in IOS 16 or 15

final session = await AudioSession.instance; await session.configure(AudioSessionConfiguration( avAudioSessionCategory: AVAudioSessionCategory.playAndRecord, avAudioSessionCategoryOptions:AVAudioSessionCategoryOptions.allowBluetooth | AVAudioSessionCategoryOptions.allowBluetoothA2dp, avAudioSessionMode: AVAudioSessionMode.defaultMode, avAudioSessionRouteSharingPolicy: AVAudioSessionRouteSharingPolicy.defaultPolicy, avAudioSessionSetActiveOptions: AVAudioSessionSetActiveOptions.none, androidAudioAttributes: const AndroidAudioAttributes( contentType: AndroidAudioContentType.music, flags: AndroidAudioFlags.none, usage: AndroidAudioUsage.media, ), androidAudioFocusGainType: AndroidAudioFocusGainType.gain, androidWillPauseWhenDucked: true, ));

How to make sound output from Bluetooth instead of speakers in IOS 17?

How to make sound output from Bluetooth instead of speakers in IOS 17?

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
   // 设置为内置麦克风采集
   [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord mode:AVAudioSessionModeVideoRecording options:nil error:nil];
   
    // 切换输入麦克风
    AVAudioSessionPortDescription *builtInMicPort = nil;
    for (AVAudioSessionPortDescription *portDescription in audioSession.availableInputs) {
        if ([portDescription.portType isEqualToString:AVAudioSessionPortBuiltInMic]) {
            builtInMicPort = portDescription;
            break;
        }
    }

    if (builtInMicPort) {
        NSError *inputError = nil;
        [audioSession setPreferredInput:builtInMicPort error:&inputError];

        if (inputError) {
            NSLog(@"切换输入麦克风失败:%@", inputError);
        }else{
            NSLog(@"内置麦克风");
        }
    } else {
        NSLog(@"找不到内置麦克风");
    }
    [audioSession setActive:YES error:nil];

set mode AVAudioSessionModeVideoRecording normall

How to make sound output from Bluetooth instead of speakers in IOS 17
 
 
Q