WISH:library to detect headset is plugged in

Discussion in 'Bugs & wishlist' started by cotralis, Jul 31, 2012.

  1. cotralis

    cotralis Member Licensed User

    Can someone make a library which can detect that a headset is plugged in

    i found some code

    This is the part of "HeadsetObserver.java", Android SDK Source.

    private final void sendIntent(int headset, int headsetState, int prevHeadsetState, String headsetName) {
    if ((headsetState & headset) != (prevHeadsetState & headset)) {
    // Pack up the values and broadcast them to everyone
    Intent intent = new Intent(Intent.ACTION_HEADSET_PLUG);


    int state = 0;
    int microphone = 0;

    if ((headset & HEADSETS_WITH_MIC) != 0) {
    microphone = 1;
    if ((headsetState & headset) != 0) {
    state = 1;
    intent.putExtra("state", state);
    intent.putExtra("name", headsetName);
    intent.putExtra("microphone", microphone);

    if (LOG) Slog.v(TAG, "Intent.ACTION_HEADSET_PLUG: state: "+state+" name: "+headsetName+" mic: "+microphone);
    // TODO: Should we require a permission?
    ActivityManagerNative.broadcastStickyIntent(intent, null);
  2. Roger Garstang

    Roger Garstang Well-Known Member Licensed User

    I'd be interested in this too, as well as a way to manage it. When I plug my headphones in on my phone it keeps starting the music app which is annoying. Haven't been able to find the option to stop it.
  3. Erel

    Erel Administrator Staff Member Licensed User

    You can use the BroadcastReceiver library to catch this event. If the FLAG_RECEIVER_REGISTERED_ONLY was not added then it could have been possible to catch it by adding an intent filter to the manifest.
  4. cotralis

    cotralis Member Licensed User

    beginner question

    can you provide an example how to use broadcastreceive?

    i have found the library but i don't know how to catch this event

  5. Erel

    Erel Administrator Staff Member Licensed User

  6. brelto85

    brelto85 Active Member Licensed User

    i'm interesting this but if i add the "android.intent.action.HEADSET_PLUG" action, i receive this error when clicked to "start service" (with bradcastreceiver application sample):


    ** Activity (main) Create, isFirst = true **
    BroadcastReceiver has been initialized.
    Activity (main) Resume **
    Service (receiver) Create **
    BroadcastReceiver has been initialized.
    Service (receiver) Start **
    BroadCastReceiver$1onReceive (B4A line: 30)
    End Sub
    Sub broadcastreceiver_onreceive signature does not match expected signature.
       at anywheresoftware.b4a.BA.raiseEvent2(BA.java:
       at anywheresoftware.b4a.BA.raiseEvent(BA.java:
       at com.rootsoft.broadcastreceiver.BroadCastReceiver$
       at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:
       at android.os.Handler.handleCallback(Handler.java:
       at android.os.Handler.dispatchMessage(Handler.java:
       at android.os.Looper.loop(Looper.java:
       at android.app.ActivityThread.main(ActivityThread.java:
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
       at dalvik.system.NativeStart.main(Native Method)
    java.lang.RuntimeException: Error receiving broadcast 
    Intent { act=android.intent.action.HEADSET_PLUG flg=0x40000010 (has extras) } in com.rootsoft.broadcastreceiver.BroadCastReceiver$1@42102738

       at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:846)
       at android.os.Handler.handleCallback(Handler.java:615)
       at android.os.Handler.dispatchMessage(Handler.java:92)
       at android.os.Looper.loop(Looper.java:155)
       at android.app.ActivityThread.main(ActivityThread.java:5454)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:511)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
       at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.RuntimeException: java.lang.Exception: Sub broadcastreceiver_onreceive signature does not match expected signature.
       at anywheresoftware.b4a.BA.raiseEvent2(BA.java:193)
       at anywheresoftware.b4a.BA.raiseEvent(BA.java:151)
       at com.rootsoft.broadcastreceiver.BroadCastReceiver$1.onReceive(BroadCastReceiver.java:110)
       at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:832)
       ... 9 more
    Caused by: java.lang.Exception: Sub broadcastreceiver_onreceive signature does not match expected signature.
       at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
       ... 12 more
    this is my code

    Sub Service_Create
    End Sub

    Sub Service_Start (StartingIntent As Intent)
    End Sub

    Sub BroadcastReceiver_OnReceive (Action As String)
    'can only abort when sendOrderedbroadcast is called.
    End Sub
  7. Erel

    Erel Administrator Staff Member Licensed User

    Seems like the v2.00 of this library is broken. Have you tried it with the first version?
  8. XverhelstX

    XverhelstX Well-Known Member Licensed User

    Multiverse app likes this.
  9. fotosettore

    fotosettore Member Licensed User

    Sub BroadcastReceiver_OnReceive (Action As String, i As Object)
    Dim i2 As Intent
        i2 = i
    End Sub
    hi !
    it's ok ! it works !
    i2 is : --- > (Intent) Intent { act=android.intent.action.HEADSET_PLUG flg=0x40000010 (has extras) }

    but the intent is the same if i put in or remove the jack of my headphones.
    so... how can understand the difference from inside and outside ?

    many thanks
  10. Erel

    Erel Administrator Staff Member Licensed User

    What is the value of:
    Sub BroadcastReceiver_OnReceive (Action As String, i As Object)
    Dim i2 As Intent = i ' nicer ;)
    End Sub
  11. fotosettore

    fotosettore Member Licensed User

    perfect !!! :icon_clap: the value is :

    Bundle[{state=0, microphone=0, name=h2w}] ----> if no headset
    Bundle[{state=1, microphone=0, name=h2w}] ----> if headset

    so ... i obtained a variable in this way :

    HeadTemp = abc.indexof("state=")
    HeadInside = abc.CharAt(HeadTemp+6)

    right ? or you advice me to use more direct instructions ?

    p.s. there is a little bug in library BroadCastReceiver :) 1.00 and 2.00 are always 1.00 in refereced libraries. i modified the number from 1.00 to 2.00 in bottom of 2.00 xlm file.
  12. Erel

    Erel Administrator Staff Member Licensed User

    No... You should use Intent.GetExtra("state").
  13. fotosettore

    fotosettore Member Licensed User

    great ...!!!
    everyday i learn something even if i'm 53 years old!

    many thanks
  14. TKC

    TKC New Member Licensed User

    Fantastic post. Exactly what I need.

    Please, can you put a small example of how to use Intent.GetExtra ("state") to capture the value of "state". Sure to be easy, but I do not get it. :sign0013:

    With this little help, and I can use this cool feature.

    Thank you very much for your patience (I am newbie, but I learn fast) :D

  15. NJDude

    NJDude Expert Licensed User

    Take a look at the attached sample.

    Make sure you have the BroadcastReceiver lib installed.

    Attached Files:

    Peter Simpson and hookshy like this.
  16. TKC

    TKC New Member Licensed User

    It works fine !!

    Thanks soooooo much :icon_clap:

  17. hookshy

    hookshy Well-Known Member Licensed User

    Thanks NJDude ..the example works great.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice