View Ticket
Not logged in
Ticket Hash: 25f9e9d002b31cf8fef3db6646143bd08a867b10
Title: Detect headset state and microphone button events
Status: Review Type: Feature_Request
Severity: Minor Priority: Medium
Subsystem: Resolution: Open
Last Modified: 2019-10-16 20:05:13
5.73 years ago
Created: 2019-10-15 21:50:06
5.73 years ago
Version Found In: all
User Comments:
anonymous added on 2019-10-15 21:50:06:

How can one detect the state of a headset and its buttons'?

For plugging/unplugging, I've found this partial solution:

# androidwish console, running on android device.

proc ::actionHandler args {
  # parse action here
  puts $args
}
borg broadcast register android.intent.action.HEADSET_PLUG ::actionHandler
0

# plug in headset (no microphone) and observe the following:
-1 android.intent.action.HEADSET_PLUG {} {} {} {address {} state 1 portName h2w microphone 0}

# unplugging shows a state change:
-1 android.intent.action.HEADSET_PLUG {} {} {} {address {} state 0 portName h2w microphone 0}
                                                                 ^
# state changed from 1 to 0 here --------------------------------^

# similarly, when plugging in a headset _with_ a mic, it shows up at the relevant property
-1 android.intent.action.HEADSET_PLUG {} {} {} {address {} state 1 portName h2w microphone 1}
                                                                                           ^
# here ------------------------------------------------------------------------------------^

# but when I try to register a microphone button activity listener, it doesn't seem to work:
borg broadcast register android.intent.action.MEDIA_BUTTON ::actionHandler
1

# pressing the headset's button does not do anything. Tried with different microphone headsets, still nothing.

# tried an activity
borg activity android.intent.action.MEDIA_BUTTON {} {} {} {} {} ::actionHandler
1

# no callback was trigger

Resources I checked:
  1. 3.5 mm Headset: Accessory Specification
  2. Headset Expected Behavior
  3. ACTION_HEADSET_PLUG
  4. ACTION_MEDIA_BUTTON
The MEDIA_BUTTON is supposed to report events for single, double, triple and quadruple headset buttons (as per the spec in 2 above), if they exist. Given that a 3.5mm audio socket is ubiquitous in android and other devices, it seems like an excellent hardware button input to all kinds of applications, besides audio. Could it be possible to report relevant events through the virtual event mechanism, like <<HEADSET-PLUGGED>>, <<HEADSET-BUTTON-1>> and so on?

How could one check the state of HEADSET_PLUG and MEDIA_BUTTON programmatically? The state of HEADSET_PLUG is reported upon registering the callback, but is there a way to poll it?

Thanks much.
dzach


anonymous added on 2019-10-15 21:56:24:

Actually, to be usefull, a virtual event should report both the ON and OFF state of the buttons, so that the duration of the press could be determined for various actions to be called, as specified in (2) above, e.g.:
One button audio A function


anonymous added on 2019-10-15 22:24:21:

I read here that there might be a need to include something like the following in AndroidManifest.xml:

  <receiver android:name="tk.tcl.wish.AndroWish" >
    <intent-filter android:priority="1000" >
      <action android:name="android.intent.action.MEDIA_BUTTON" />
     </intent-filter>
  </receiver>


chw added on 2019-10-16 05:06:29:
Regarding the HEADSET_PLUG event you might use the event command
to make it into a virtual event in the handler procedure of that
broadcast receiver.

However, for the MEDIA_BUTTON, don't expect me to add a special
broadcast receiver tag in AndroidManifest.xml for a plain AndroWish.
This clearly is something which has to be done for a separately
bundled App e.g. using the AndroWish SDK since it's an intervention
in the overall device user experience despite the priority field.

anonymous added on 2019-10-16 12:48:50:

I see.
I haven't tried to create a special androidwish for this yet, as I need to upgrade
the installation of the Android SDK on this machine.
I'll try it, and will report back, in case someone else finds this info usefull.
Thanks!


anonymous added on 2019-10-16 20:05:13:
It seems like just adding a <receiver> tag in the <application> section of the AndroidManifest.xml is not enought to make it work. 
Tried also to see if any permissions are needed/defined for MEDIA_BUTTON or anything like it, but there doesn't seem to be any.
Any suggestions?