HomeiOS Developmentios - Find out how to re-detect AirPods after switching to receiver...

ios – Find out how to re-detect AirPods after switching to receiver in AVAudioSession?


I’m managing audio routes in an iOS app utilizing AVAudioSession. I permit the person to change between speaker, receiver, and Bluetooth (AirPods) manually. The difficulty is:

If the person selects receiver (overrideOutputAudioPort(.none) with no .allowBluetooth), then later needs to change again to AirPods mechanically by eradicating the airpods then put them again within the ear, AirPods will not be detected anymore, despite the fact that they’re related within the system. But when I wish to handbook change the route I can, the habits I’m asking for is like what WhatsApp does.

func configureAudioSession(mode: AVTokAudioMode) {
    guard currentMode != mode || userHasManuallyOverriddenRoute else { return }

    let session = AVAudioSession.sharedInstance()

    do {
        change mode {
        case .forceSpeaker:
            attempt session.setCategory(.playAndRecord, choices: [.allowBluetooth, .allowBluetoothA2DP])
            attempt session.overrideOutputAudioPort(.speaker)

        case .allowBluetooth:
            attempt session.setCategory(.playAndRecord, choices: [.allowBluetooth, .allowBluetoothA2DP])
            attempt session.overrideOutputAudioPort(.none)

        case .forceReceiver:
            attempt session.setCategory(.playAndRecord, choices: [])
            attempt session.overrideOutputAudioPort(.none)
        }

        attempt session.setMode(.voiceChat)
        attempt session.setActive(true, choices: [.notifyOthersOnDeactivation])

        currentMode = mode

    } catch {
        print("❌ Audio session config failed: (error)")
    }
}

How can I correctly re-enable AirPods detection after switching to receiver mode?
Is there a dependable solution to refresh or reset the session to make AirPods present up once more in AVAudioSession.currentRoute or permit computerized rerouting?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments