iter
iter

Reputation: 4303

How to find out if an external headset is connected to an iPhone?

Is it possible to detect that the user has an external headset plugged into the iPhone's 3.5mm connector or the 30-pin connector? I want to output audio only to an external audio device, and keep silent if nothing is connected.

Upvotes: 4

Views: 4033

Answers (3)

Petro Novosad
Petro Novosad

Reputation: 129

There is nice article about this in Apple documentation: https://developer.apple.com/documentation/avfoundation/avaudiosession/responding_to_audio_session_route_changes

Only you have to verify if portType == AVAudioSessionPortBluetoothA2DP

func setupNotifications() {
    let notificationCenter = NotificationCenter.default
    notificationCenter.addObserver(self,
                               selector: #selector(handleRouteChange),
                               name: .AVAudioSessionRouteChange,
                               object: nil)
}


@objc func handleRouteChange(notification: Notification) {
    guard let userInfo = notification.userInfo,
        let reasonValue = userInfo[AVAudioSessionRouteChangeReasonKey] as? UInt,
        let reason = AVAudioSessionRouteChangeReason(rawValue:reasonValue) else {
            return
    }
    switch reason {
    case .newDeviceAvailable:
        let session = AVAudioSession.sharedInstance()
        for output in session.currentRoute.outputs where output.portType == AVAudioSessionPortBluetoothA2DP {
            headsetConnected = true
            break
        }
    case .oldDeviceUnavailable:
        if let previousRoute =
            userInfo[AVAudioSessionRouteChangePreviousRouteKey] as? AVAudioSessionRouteDescription {
            for output in previousRoute.outputs where output.portType == AVAudioSessionPortBluetoothA2DP {
                headsetConnected = false
                break
            }
        }
    default: ()
    }
}

func isBluetoothHeadsetConnected() -> Bool {
    var result = false
    let session = AVAudioSession.sharedInstance()
    for output in session.currentRoute.outputs where output.portType == AVAudioSessionPortBluetoothA2DP {
        result = true
    }
    return result
}

Upvotes: 1

Pradip Sutariya
Pradip Sutariya

Reputation: 287

Call this method to find out the bluetooth headset is connected or not.

First import this framework #import <AVFoundation/AVFoundation.h>

- (BOOL) isBluetoothHeadsetConnected
    {
        AVAudioSession *session = [AVAudioSession sharedInstance];
        AVAudioSessionRouteDescription *routeDescription = [session currentRoute];

        NSLog(@"Current Routes : %@", routeDescription);

        if (routeDescription)
        {
            NSArray *outputs = [routeDescription outputs];

            if (outputs && [outputs count] > 0)
            {
                AVAudioSessionPortDescription *portDescription = [outputs objectAtIndex:0];
                NSString *portType = [portDescription portType];

                NSLog(@"dataSourceName : %@", portType);

                if (portType && [portType isEqualToString:@"BluetoothA2DPOutput"])
                {
                    return YES;
                }
            }
        }

        return NO;
    }

Upvotes: 2

Chuck
Chuck

Reputation: 237010

The answer is very similar to the answer to this question, but you'll want to get the kAudioSessionProperty_AudioRoute property instead.

Upvotes: 3

Related Questions