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.
            Asked
            
        
        
            Active
            
        
            Viewed 3,876 times
        
    3 Answers
3
            The answer is very similar to the answer to this question, but you'll want to get the kAudioSessionProperty_AudioRoute property instead.
- 
                    Thanks! This is what I need. Now I wonder what are the possible values for the Auidio_Route property. – iter Mar 26 '10 at 00:36
 - 
                    For possible values, see this discussion: http://stackoverflow.com/questions/2753562/what-kind-of-routes-could-i-get-back-from-kaudiosessionproperty-audioroute-proper – Reuven Jul 07 '11 at 21:48
 
2
            
            
        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;
    }
        Pradip Sutariya
        
- 257
 - 2
 - 16
 
1
            
            
        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
}
        Petro Novosad
        
- 119
 - 1
 - 3
 
- 
                    2While this link may answer the question, it is better to include the [essential parts of the answer](https://meta.stackexchange.com/a/8259) here and provide the link for reference. Link-only answers can become invalid if the linked page changes. [Answers that are little more than a link may be deleted](https://stackoverflow.com/help/deleted-answers) – adiga Mar 25 '19 at 11:59