WISH:library to detect headset is plugged in

cotralis

Member
Licensed User
Longtime 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);

**intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);**

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);
}
}
 

cotralis

Member
Licensed User
Longtime 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

thanks
 

brelto85

Active Member
Licensed User
Longtime 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):

log

B4X:
** 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
java.lang.Exception: Sub broadcastreceiver_onreceive signature does not match expected signature.
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
   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)
   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)
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

B4X:
Sub Service_Create
   Broadcast.Initialize("BroadcastReceiver")
End Sub

Sub Service_Start (StartingIntent As Intent)
   Broadcast.addAction("android.intent.action.HEADSET_PLUG")
   Broadcast.SetPriority(2147483647)
   Broadcast.registerReceiver("")
End Sub

Sub BroadcastReceiver_OnReceive (Action As String)
   ToastMessageShow(Action,False)
   'can only abort when sendOrderedbroadcast is called.
   Broadcast.AbortBroadcast
End Sub
 

fotosettore

Member
Licensed User
Longtime User
B4X:
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
 

fotosettore

Member
Licensed User
Longtime User
Log(i2.ExtrasToString)

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 :

abc=object.ExtrasToString
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.
 

TKC

New Member
Licensed User
Longtime 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

TKC
 
Top