Detecting if headphones are plugged in on Android and iOS

NickprovsNickprovs USMember ✭✭

Hi,

My application will behave differently whether or not headphones are plugged in.

It doesn't seem like there's a way to do this without a dependency service (please let me know if I'm wrong!).

I'm working on the Android portion atm. There are android methods out there that detect if a wired headset is plugged in.. but I'd also like to account for bluetooth.

Has anybody tackled this before?

Thanks,

Nick

Posts

  • RyanDixonRyanDixon USMember ✭✭✭

    iOS is a tad trickier as you can check for current routes using AVAudioSession.SharedInstance.CurrentRoute.Outputs
    Although if you want to pick up changes you will need to register an observer via the NSNotificationCenter, I believe its AVAudioSessionRouteChangeNotification but thats off the top of my head?

  • NickprovsNickprovs USMember ✭✭

    `
    [assembly: Xamarin.Forms.Dependency(typeof(SharedLibraryAndroid))]
    namespace Xamarin495.Droid
    {
    class SharedLibraryAndroid : ISharedLibrary
    {
    public SharedLibraryAndroid() { }

        public bool headphonesPluggedIn()
        {
            AudioManager am = (AudioManager)Application.Context.GetSystemService(Context.AudioService);
            return am.WiredHeadsetOn;
        }
    }
    

    `
    This did the job for wired headphones. Any ideas for bluetooth?

  • NickprovsNickprovs USMember ✭✭

    @Nickprovs said:

    `
    [assembly: Xamarin.Forms.Dependency(typeof(SharedLibraryAndroid))]
    namespace Xamarin495.Droid
    {
    class SharedLibraryAndroid : ISharedLibrary
    {
    public SharedLibraryAndroid() { }

        public bool headphonesPluggedIn()
        {
            AudioManager am = (AudioManager)Application.Context.GetSystemService(Context.AudioService);
            return am.WiredHeadsetOn;
        }
    }
    

    `
    This did the job for wired headphones. Any ideas for bluetooth?

    Revised above

            AudioManager am = (AudioManager)Application.Context.GetSystemService(Context.AudioService);
            if (am.WiredHeadsetOn || am.BluetoothA2dpOn)
                return true;
            else return false;
    

    This seems to be the quick and dirty way of doing it for android. Still looking into iOS.

  • NickprovsNickprovs USMember ✭✭
    edited October 2017

    @RyanDixon This is what I got. For now I'm not going to worry about unplug events. Just if there's headphones plugged in at the time of a button press.

    @ Anyone.... Does anyone want to test this code out for me :D... I don't have a mac.... I debug iOS stuff at school when I'm there.

    I used a gentleman's swift block from here: https://stackoverflow.com/questions/21292586/are-headphones-plugged-in-ios7

    It looks like...

    `
    class SharedLibraryIOS : ISharedLibrary
    {
    public SharedLibraryIOS() { }

        public bool headphonesPluggedIn()
        {
            AVAudioSessionPortDescription[] outputs = AVAudioSession.SharedInstance().CurrentRoute.Outputs;
            foreach (AVAudioSessionPortDescription output in outputs)
            {
                if (output.PortType == AVAudioSession.PortBluetoothA2DP || output.PortType == AVAudioSession.PortBluetoothHfp ||
                output.PortType == AVAudioSession.PortBluetoothLE || output.PortType == AVAudioSession.PortHeadphones)
                    return true;
            }
            return false;
        }
    }
    

    `

    Also what's the deal with Xamarin code blocks. I put code blocks in the ` characters but it always gets really picky and only blocks some of my code.

  • NashZhouNashZhou USMember ✭✭✭

    @Nickprovs said:
    @RyanDixon This is what I got. For now I'm not going to worry about unplug events. Just if there's headphones plugged in at the time of a button press.

    @ Anyone.... Does anyone want to test this code out for me :D... I don't have a mac.... I debug iOS stuff at school when I'm there.

    I used a gentleman's swift block from here: https://stackoverflow.com/questions/21292586/are-headphones-plugged-in-ios7

    It looks like...

        class SharedLibraryIOS : ISharedLibrary
        {
            public SharedLibraryIOS() { }
    
            public bool headphonesPluggedIn()
            {
                AVAudioSessionPortDescription[] outputs = AVAudioSession.SharedInstance().CurrentRoute.Outputs;
                foreach (AVAudioSessionPortDescription output in outputs)
                {
                    if (output.PortType == AVAudioSession.PortBluetoothA2DP || output.PortType == AVAudioSession.PortBluetoothHfp ||
                    output.PortType == AVAudioSession.PortBluetoothLE || output.PortType == AVAudioSession.PortHeadphones)
                        return true;
                }
                return false;
            }
        }
    

    Also what's the deal with Xamarin code blocks. I put code blocks in the ` characters but it always gets really picky and only blocks some of my code.

    Put 3 ` before a block of code and after its finished instead of one. You won't get the weird code blocks where half the code is in a block and the other isn't. I just quoted your post and put 3 instead of 1 so the entire code is put in a block.

    Best of luck on getting a dependency service that can detect if headphones are being used or not!

Sign In or Register to comment.