Action Bluetooth button on Broadcastreceive (Android studio) **Java**

Asked

Viewed 130 times

0

I’m creating an App in android studio**(Java)**e Preciso parear um botão via bluetooth exemplo: Selfie stick button that when triggered it opens an Activity even the application being minimized if possible closed imagined in Broadcastreceiver but found nothing related. thank you in advance.

Mainactivit ->

AudioManager mAudioManager;
ComponentName mReceiverComponent;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
        MediaButtonIntentReceiver r = new MediaButtonIntentReceiver();
        filter.setPriority(1000); //this line sets receiver priority
        registerReceiver(r, filter);
        mAudioManager =  (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        mReceiverComponent = new ComponentName(this,MainActivity.class);
        mAudioManager.registerMediaButtonEventReceiver(mReceiverComponent);

    }
    public void onDestroy(){
        super.onDestroy();
        mAudioManager.unregisterMediaButtonEventReceiver(mReceiverComponent);

    }

Mediabuttonintentreceiver -> Broadcastreceiver

public class MediaButtonIntentReceiver extends BroadcastReceiver {

public MediaButtonIntentReceiver() {
    super();
}

@Override
public void onReceive(Context context, Intent intent) {
    String intentAction = intent.getAction();
    if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
        return;
    }
    KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
    if (event == null) {
        return;
    }
    int action = event.getAction();
    if (action == KeyEvent.ACTION_DOWN) {
        // do something
        Toast.makeText(context, "BUTTON PRESSED!", Toast.LENGTH_SHORT).show();
    }
    abortBroadcast();
}
    }

my xml manifest.

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="sysystem.vaipf">
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".MediaButtonIntentReceiver">
            <intent-filter>
                <action android:name="android.intent.action.MEDIA_BUTTON" />
            </intent-filter>
        </receiver>
    </application>

    </manifest>

this is the example I used last but unsuccessfully.

What I need is when press the bluetooth control button it open the message Toast.makeText(context, "BUTTON PRESSED!", Toast.LENGTH_SHORT).show(); of Broadcastreceiver put at the push of the button nothing happens!!! Remembering that the button I’m referring to is that selfie stick control!

  • @Maniero The development of the application is in java(android studio) the button that I need to capitulate the action is selfie stick for example but I do not know if I could express myself well, and I really need this strength...

  • Important you [Dit] your post and explain in detail the problem by describing what you tried and where the current difficulty is (leaving only the tags really relevant to the problem itself). Tutorials and broad doubts do not fit the site scope, It is important to ask each question about a very specific problem (even because, after solving a question can open another question). Here are some tips for better use of the site: [Ask], Manual on how NOT to ask questions and [Help].

  • if you can check now I thank you very much and I’m sorry, as I said I’m new here and I didn’t get the hang of it yet but I get there.

  • 1

    There is still a description of what went wrong (what I expected to happen, and what happened instead, especially if there were error messages, and on which line). A [MCVE] can help a lot.

  • I think I expressed myself better now thank you very much for the help and if I am missing any more information I will gladly put!

  • Good night!!! As I did not succeed in using the selfie stick button I migrated to the headset button. It didn’t go as planned, but it’s working. in case any good soul has the solution I will be grateful but until you are in that way, still very grateful for the help of the moderators in trying to help me write the question...

  • always try to detail well in the next ones and take a look at the links recommended above when you have a little time, then increases the chance of success.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.