Mobile - Check for headphones

Is there a way to check to see if something is plugged into the audio jack, like headphones, for instance?

If you’re on iOS, you can use Objective C to do what you want. Here’s one example: audio - Detecting if headphones are plugged into iPhone - Stack Overflow

Calling into Objective C from Unity can be tricky. Here’s one great reference: http://www.tinytimgames.com/2010/01/10/the-unityobjective-c-divide/

Good luck.

First, to update this thread a little, there is still no easy way to do this…

Making a headphone detector for Android i discovered some pitfalls, so here is it a little more detailed:

  1. Build an Android library detecting a headset

With AndroidStudio, start a new project or module from the library template.
Apply the same targetSDK and minSDK values as in Unity, as Unity would fail on merging the AndroidManifests otherwise.
Now there are two things of doing it. First is to register a BroadcastReceiver on the intent “andorid.intent.action.HEADPHONE_PLUGGED”, unfortunately this will only be fired when a wired headphone is plugged (kinda outdated), and it must be actively registered in the code which means declaring it in the AndroidManifest.xml is not enough.
Second is to poll on some methods of the AudioManager:

// initialize this once // get context from unity
AudioManager audioManager = (AudioManager) context.getSystemService(Service.AUDIO_SERVICE);

// poll on these
boolean headsetConnected = !audioManager.isSpeakerphoneOn()
                && (audioManager.isBluetoothA2dpOn()
                || audioManager.isBluetoothScoOn()
                || audioManager.isWiredHeadsetOn());

Be shure to use some kind of factory method to initialize this (providing the Context of the actual Unity game app).
Build the lib. For Android Studio the output is in build/outputs/aar.

  1. Add the .aar lib to your Unity project

Copy the .aar package to any directory in the Unity project. I recommend using Assets/Plugins/Android. Such added, such wow.
If you are using the intent way, add

<category android:name="com.google.intent.action.HEADSET_PLUG"/>

to the section in all AndroidManifest in the project.

  1. Access the lib from your Unity C# code

  AndroidJavaClass libClass; // analog to java Class objects - the class of your lib
  AndroidJavaObject libInstance; // instance of your lib
    
  libClass = new AndroidJavaObject("fully.qualified.class.name.of.your.lib");
  libObject = libClass.CallStatic<AndroidJavaObject>("factoryMethodName", getContext());
    
  // access this call in a loop to get continuous detection
  bool isConnected = libObject.Call<bool> ("detectConnectedHeadsetMethod");
    
  private static AndroidJavaObject getContext () {
          AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
          AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
          return activity.Call<AndroidJavaObject>("getApplicationContext");
  }

Be shure to throttle the dection method invocation when accessing in a loop as the call is really slow.

I used macOS Sierra, Unity 5.4.1f1 and AndroidStudio 2.2.3 but i haven’t discovered any version issues.