Problems with ACTION_HEADSET_PLUG casting on Android

I tried these phones: Motorolla Backflip 1.5, Nexus One 2.1

I basically register BroadcastReceiver to get the ACTION_HEADSET_PLUG broadcast and look at the 3 add-ons that come in target:

  • state
  • name
  • microphone

Here is a description from the API:

* state - 0 for unplugged, 1 for plugged. * name - Headset type, human readable string * microphone - 1 if headset has a microphone, 0 otherwise 

Problem # 1: Broadcasting occurs when activity starts (not expected), when the screen rotates (not expected), and when the headset / headphones are connected / disconnected (expected).

Problem No. 2: Backflip phone (1.5) sends null for status + microphone, “No device” as name when headset / headphone is turned off and sends zero for status + microphone, “Stereo HeadSet” / “Stereo HeadPhones” as name when connecting headphones / headphones.

UPDATE: The T-Mobile G1 with 1.6 behaves the same as a Backflip phone.

Nexus is even worse, it always sends zero for status + microphone, “Headset” as a name when a headset / headphone is connected or disconnected.

Question: How can I explain that the API is so badly broken both on version 1.5, and on version 2.1, and on different devices?

UPDATE:

Code in onCreate main activity:

 // Register receiver this.registerReceiver(new BroadcastsHandler(), new IntentFilter(Intent.ACTION_HEADSET_PLUG)); 

Now the BroadcastReceiver code:

 public class BroadcastsHandler extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equalsIgnoreCase(Intent.ACTION_HEADSET_PLUG)) { String data = intent.getDataString(); Bundle extraData = intent.getExtras(); String st = intent.getStringExtra("state"); String nm = intent.getStringExtra("name"); String mic = intent.getStringExtra("microphone"); String all = String.format("st=%s, nm=%s, mic=%s", st, nm, mic); AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("Headset broadcast"); builder.setMessage(all); builder.setPositiveButton("Okey-dokey", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } } 

}

+1
source share
3 answers

The code is wrong!

“state” and “microphone” are integer, not string. Therefore, the code should be changed as follows:

  int st = intent.getIntExtra("state" , -1); String nm = intent.getStringExtra("name"); int mic = intent.getIntExtra("microphone", -1); String all = "st="+Integer.toString(st)+" nm="+nm+" mic="+Integer.toString(mic); 

It works!

+3
source

Broadcasting starts when activity starts (not expected)

This is in the registerReceiver documentation:

The system can broadcast intentions that are “sticky” - they remain after the broadcast is completed in order to be sent for subsequent registrations. If your IntentFilter matches one of these sticky intentions, that Intent will be returned by this function and sent to your receiver, as if it had just been transmitted.

My suggestion is that your activity has a chance to get the current state for such “sticky” broadcasts right after you register for this.

I am currently working on an application with two devices that need to receive ACTION_HEADSET_PLUG, and there seem to be devices that do not broadcast this system (I do not receive it on my tablet, but I receive it on my phone), so you can do The conclusion is that after registration for this broadcast and it was not received at least once, the device does not support sending it. I have not tested if this applies to other broadcasts of the system, but I would have thought so.

+1
source

Stupid me, the problem is a little different - there is a "state" and a "name" without a "microphone". Another thing is the "state" - 0 and 1 for headphones, and 0 and 3 for a headset. Super weird ...

0
source

All Articles