Action button Bluetooth in BroadcastReceive (Android studio) ** Java **

0

I'm creating an app in android studio ** (Java) ** and I need to pair a button via bluetooth example: selfie stick button that when triggered open an activity even the application being minimized if possible closed I imagined in BroadcastReceiver but I did not find anything 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 manifest.xml

 <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 for the last time without success.

What I need is when I press the bluetooth control button it opens the message Toast.makeText(context, "BUTTON PRESSED!", Toast.LENGTH_SHORT).show(); of BroadcastReceiver but by pressing the button nothing happens !!! Remembering that the button I'm referring to is that selfie stick control!

    
asked by anonymous 04.11.2018 / 15:29

0 answers