• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Headphone pause button event capture

Hello everyone, I've had a full-on Android day today :D

I got sidetracked looking to find code examples about reverse geolocation... and found some details about the play/pause button on the headphones.

According to some poking around with a spare headset and a multimeter I discovered that there is a means of captureing data from external devices via the TRRS plug/jack.

I've discovered that the standard media player responds to 4 states:
1. Nothing, no presses, voltage on ring2 connector is 2.6 volts.
2. Forward/next track, voltage on ring 2 is around 0.5 volts.
3. Back/previous track, voltage is around 0.18 ~ 0.20 volts.
4. Play/pause, ring 2 is at 0 volts.

I have made a circuit that replicates these voltages, simply by switching output pins to resistors and i'd like to capture these 4 states as a simple data logger.

Basically the end goal is to update an old weather monitoring project so the data on the eeprom is simply sent using the headphone socket connection so save me from having to dismantle the weather proof housing, extract the chip to go into the reader, and all the reverse.

Has anyone got any code samples that could guide me in the right direction.

As you may guess im more at home with a soldering iron and PIC assembly code than Java, but I'm finding more and more reasons to keep on the learning curve :).

Thanks for any help,
GIR
 
UPDATE

I was tipped off about some this site and it looks like the way to go for me. :)

However I am stuck at the first hurdle and being new to this I am really really stumped. :eek:

The code block of
Code:
MediaButtonIntentReceiver mMediaButtonReceiver = new MediaButtonIntentReceiver();
 IntentFilter mediaFilter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON); 
mediaFilter.setPriority(MEDIA_BUTTON_INTENT_EMPIRICAL_PRIORITY_VALUE); 
registerReceiver(mMediaButtonReceiver, mediaFilter);
.. is throwing up some errors in eclipse, and despite all my efforts so far I cannot correct them. :thinking:
The errors are:
Code:
Syntax error on token "MEDIA_BUTTON_INTENT_EMPIRICAL_PRIORITY_VALUE", VariableDeclaratorId expected after this token
+
Code:
mMediaButtonReceiver cannot be resolved to a type
+
Code:
Return type for the method is missing
+ 2x
Code:
Syntax error on token ";", delete this token
I know Ii'm in over my head with making the jump from a simple soundboard to this, but I have to try eh?

Many thanks for any help,
GIR
 
Upvote 0
It sounds like you are missing a import statement for the MediaButtonIntentReceiver

Thanks for getting back to me miXer.

I found the following code samples:

1. From Gaunt Face - Matthew Gaunt | Using Android Headset Buttons (Earphone Buttons)
Code:
HardButtonReceiver buttonReceiver = new HardButtonReceiver();
IntentFilter iF = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
iF.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
registerReceiver(buttonReceiver, iF);
2. From [CSDb] - User Forums - Android SID/MOD/etc player
Code:
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
 intentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY - 1);                 
service.registerReceiver(headsetButtonReceiver, intentFilter);
The pair of these examples look very similar, so I'm wondering if I'm using the incorrect API, as currently Im using 2.1.

My code looks like this:
Code:
package com.nds.PausebtnPress;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.TextView;

public class PausebtnPress extends Activity {
    /** Called when the activity is first created. 
     * 
     * MediaButtonIntentReceiver mMediaButtonReceiver = new MediaButtonIntentReceiver();
     * IntentFilter mediaFilter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
     * mediaFilter.setPriority(MEDIA_BUTTON_INTENT_EMPIRICAL_PRIORITY_VALUE);
     * registerReceiver(mMediaButtonReceiver, mediaFilter);
*/
    
    HardButtonReceiver buttonReceiver = new HardButtonReceiver();{
    IntentFilter iF = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
    //SYSTEM_HIGH_PRIORITY
    iF.setPriority(0);
    registerReceiver(buttonReceiver, iF);}
    
    // do something
    
    //text = "The PAUSE button is pressed!";
    
    
        
    public String text = null;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // need to setup to change the textview
        TextView tv = (TextView)this.findViewById(R.id.textView1);
        
        tv.setText(text);
        
  

      
    }
    public class HardButtonReceiver extends BroadcastReceiver
    {
    @Override
    public void onReceive(Context context, Intent intent)
    {
    Log.v("TestApp", "Button press received");
    abortBroadcast();
    KeyEvent key = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
    text ="Button pressed";

    if(key.getAction() == KeyEvent.ACTION_UP)
    {
    int keycode = key.getKeyCode();
    if(keycode == KeyEvent.KEYCODE_MEDIA_NEXT)
    {
    Log.d("TestApp", "Next Pressed");
    }
    else if(keycode == KeyEvent.KEYCODE_MEDIA_PREVIOUS)
    {
    Log.d("TestApp", "Previous pressed");
    }
    else if(keycode == KeyEvent.KEYCODE_HEADSETHOOK)
    {
    Log.d("TestApp", "Head Set Hook pressed");
    }
    }
    }
    }
}
Thanks for any help,

The code above now compiles with out any errors, BUT when run on my phone it instantly force-closes!
GIR
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones