I have Selfie stick connected to my phone. I am able to find device ID using below code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
this.registerReceiver(mReceiver, filter);
}
//The BroadcastReceiver that listens for bluetooth broadcasts
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
//Device is now connected
Toast.makeText(getApplicationContext(), "ACTION_ACL_CONNECTED" + device, Toast.LENGTH_LONG).show();
}
}
};
My question is how I can detect button press/click event of this connected peripheral device?
Help in form of code snippet/tutorial/comments is highly appreciable.
Thanks!!!
EDIT:
When I press button of Selfie stick Volume + button listen to the event
I found the answer. It was very simple. Just override onKeyDown() method of Activity.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
return super.onKeyDown(keyCode, event);
}
Here keyCode is the event name returned.
I have made slight modifications in your code. Please see if its helpful
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter a_filter = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
this.registerReceiver(mReceiver, a_filter);
IntentFilter b_filter = new IntentFilter(Intent.ACTION_CAMERA_BUTTON);
**b_filter.setPriority(1000);**
this.registerReceiver(mReceiver, b_filter);
}
//The BroadcastReceiver that listens for bluetooth broadcasts
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
//Device is now connected
Toast.makeText(getApplicationContext(), "ACTION_ACL_CONNECTED" + device, Toast.LENGTH_LONG).show();
}
if (!Intent.ACTION_CAMERA_BUTTON.equals(action)) {
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();
}
};
Using accessibility service, You can achieve auto-click.
Answer explained here, May be it's helpful to you !
Related
I'm writing a service that must accept and react on ACTION_BATTERY_LOW broadcast. I'm using next code:
public class MyService extends Service {
...
private final BroadcastReceiver batteryBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d(LOG_TAG, "batteryBroadcastReceiver.onReceive()->intent="+intent.toString());
if(intent.getAction().equals(Intent.ACTION_BATTERY_LOW))
Log.d(LOG_TAG, "intent.getAction() == Intent.ACTION_BATTERY_LOW!");
}
};
public void onCreate() {
super.onCreate();
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_BATTERY_LOW);
registerReceiver(batteryBroadcastReceiver,intentFilter);
}
public void onDestroy() {
super.onDestroy();
unregisterReceiver(batteryBroadcastReceiver);
}
}
When battery charge level goes to low (~15%) Android sends an intent with action ACTION_BATTERY_LOW and then sends it again every 10 seconds which I'm receiving in MyServive. Why does it happen? What can I do or what I'm doing wrong? Tested on real device.
The period to send Intent.ACTION_BATTERY_LOW is up to the OS and the manufacturer. It's informed periodically so you have updated information through time and you can make better decisions.
I don't know what do you want to accomplish but if you are getting the action repeated you can monitor also Intent.ACTION_BATTERY_OKAY and have a flag indicating whether the action for the low battery has been made. That flag changes its value depending on the action the broadcastReceiver receives, e.g.
public class MyService extends Service {
...
private final BroadcastReceiver batteryBroadcastReceiver = new BroadcastReceiver() {
private bool mBatteryLowActionHasBeenMade = false;
#Override
public void onReceive(Context context, Intent intent) {
Log.d(LOG_TAG, "batteryBroadcastReceiver.onReceive()->intent="+intent.toString());
if(intent.getAction().equals(Intent.ACTION_BATTERY_LOW) && !this.mBatteryLowActionHasBeenMade ) {
Log.d(LOG_TAG, "intent.getAction() == Intent.ACTION_BATTERY_LOW!");
this.mBatteryLowActionHasBeenMade = true;
}
if(intent.getAction().equals(Intent.ACTION_BATTERY_OKAY)) {
this.mBatteryLowActionHasBeenMade = false;
}
}
};
public void onCreate() {
super.onCreate();
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_BATTERY_LOW);
intentFilter.addAction(Intent.ACTION_BATTERY_OKAY);
registerReceiver(batteryBroadcastReceiver,intentFilter);
}
public void onDestroy() {
super.onDestroy();
unregisterReceiver(batteryBroadcastReceiver);
}
}
If that doesn't fit your requirements try monitoring the battery level with Intent.ACTION_BATTERY_CHANGED
I made an application to turn on/off bluetooth. To turn on/off i used a switch button. Here is the code:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private final static int REQUEST_ENABLE_BT = 1;
BluetoothAdapter mBluetoothAdapter = null;
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(mBluetoothAdapter.ACTION_STATE_CHANGED)) {
final int state = intent.getIntExtra(mBluetoothAdapter.EXTRA_STATE, mBluetoothAdapter.ERROR);
switch (state){
case BluetoothAdapter.STATE_OFF:
break;
case BluetoothAdapter.STATE_ON:
break;
}
}
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Switch bluetoothSwitch = (Switch) findViewById(R.id.bluetoothSwitch);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null){
//Device does not support bluetooth
}
else{
bluetoothSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(bluetoothSwitch.isChecked() && !mBluetoothAdapter.isEnabled()){
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mReceiver, BTIntent);
}
if(!bluetoothSwitch.isChecked() && mBluetoothAdapter.isEnabled()){
mBluetoothAdapter.disable();
IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mReceiver, BTIntent);
}
}
});
}
}
}
The problem is that if i turn on/off bluetooth from settings and not from my app the switch doesn't change.
I have implemented a broadcast receiver but i can't access the switch from it.
I tried:
bluetoothSwich.setChecked(true)
inside the broadcast receiver but it doesn't work.
Ty
EDIT :
I partially solved the problem with the global switch but to catch the on/off action from settings first i have to click the on/off button from my app at least one time. any suggestion?
To detect the state change of bluetooth you need to add following permission to your AndroidManifest.xml.
<uses-permission android:name="android.permission.BLUETOOTH" />
Use a Local broadcast preferably. You do not need to register it in Manifest . register it at runtime where you need it.(If need throughout the app then register it in Manifest)
private final BroadcastReceiver bStateReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) {
// Bluetooth is disconnected, do handling here
}
}
}
};
Runtime register:
LocalBroadcastManager.getInstance(this).registerReceiver(bStateReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
Runtime unregister : Don't forget to unregister the broadcast.
LocalBroadcastManager.getInstance(this).unregisterReceiver(bStateReceiver);
Static Register:
<receiver
android:name=".FullyQualifiedBroadcastReceiverClassName"
android:enabled="true">
<intent-filter>
<action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
</intent-filter>
I am trying to detect Headset button click referring to this
http://android.amberfog.com/?p=415
This is the code I haave tried
public class MainActivity extends Activity {
private IntentFilter mIntentFilter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.actvity_main);
mIntentFilter = new IntentFilter();
mIntentFilter.addAction(Intent.ACTION_MEDIA_BUTTON);
mIntentFilter.setPriority(2147483647);
registerReceiver(mReceiver, mIntentFilter);
}
#Override
public void onResume() {
super.onResume();
registerReceiver(mReceiver, mIntentFilter);
}
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
#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) {
Toast.makeText(MainActivity.this,"Your Message", Toast.LENGTH_LONG).show();
}
abortBroadcast();
}
};
#Override
protected void onPause() {
unregisterReceiver(mReceiver);
super.onPause();
}
All I am trying to do is get a toast message for the button click received , but it is just not getting detected What exactly should be done to detect the button click .
I am testing on a real android device with Android 4.2.1
Have you registered your broadcast receiver in AndroidManifest.xml?
<receiver android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
i'm trying to create an application and wish it detects the device which is already connected with the phone if it is near to it that's because i want to unlock the phone when the device is too close with the phone!
I've already searched on Google on the rssi but the problem is that it works only if the device doesn't have any connection with the phone !
this is the code wish it have tryed
public class RSSIActivity extends Activity {
private BluetoothAdapter BTAdapter = BluetoothAdapter.getDefaultAdapter();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rssi);
registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
Button boton = (Button) findViewById(R.id.button1);
boton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
BTAdapter.startDiscovery();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_rssi, menu);
return true;
}
private final BroadcastReceiver receiver = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)) {
int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
String name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
TextView rssi_msg = (TextView) findViewById(R.id.textView1);
rssi_msg.setText(rssi_msg.getText() + name + " => " + rssi + "dBm\n");
}
}
};
}
but in my case the device is already connected with the phone using Bluetooth.
any suggestions ?
thank you
I want to use an extra physical button in my app. It is connected to the ear phones plug. I searched the web and didn't find specific answers. Maybe i didnt use the correct search terms?
Are there any tutorials available?
What events are triggered when such a button is pressed?
Is there any difference (in coding) between the original pressy and the cheap china things?
Is it possible to emulate this thing in the adk emulator?
I really want to use this thing in my app, but i dont have a clue how.
I need some help to get started
Any ideas?
Youre answer's helped me to get startet.
Now im am facing new problems, as the toasts i'll try to make to check the responses dont appear. Button press will just play and pause music with the samsung music player on the phone.
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.widget.Toast;
public class MediaButtonIntentReceiver extends BroadcastReceiver {
public MediaButtonIntentReceiver() {
super();
}
#Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
Toast.makeText(context, "onRecieve triggered", Toast.LENGTH_SHORT).show();
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();
Toast.makeText(context, "Action lauchched", Toast.LENGTH_SHORT).show();
if (action == KeyEvent.ACTION_DOWN) {
// do something
Toast.makeText(context, "BUTTON PRESSED!", Toast.LENGTH_SHORT).show();
}
if (action == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) {
// do something
Toast.makeText(context, "Play Pause pressed!", Toast.LENGTH_SHORT).show();
}
abortBroadcast();
}
}
so it seems that the reciever code isnt reached.
in my activity there are following lines:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ZaehlenActivity.this);
setContentView(R.layout.zaehlen);
IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
MediaButtonIntentReceiver r = new MediaButtonIntentReceiver();
registerReceiver(r, filter);
am i forgettig something? do i have to disable the standard earphone recognition somehow?
I think that earphone buttons are considered as MEDIA_BUTTONS so , you have to create a BroadcastReceiver which listen to android.intent.action.MEDIA_BUTTON and make a test statment to test if the action is an ACTION_DOWN , here's a suite of code it may helps :
This is the BroadcastReceiverwhich you have to declare it in AndroidManifest.xml , and make as Intent-filter : android.intent.action.MEDIA_BUTTON
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();
}
}
And this is an Activity which listen to the BroadcastReceiverand then make a Toastif one of the buttons was clicked :
public class mainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
MediaButtonIntentReceiver r = new MediaButtonIntentReceiver();
registerReceiver(r, filter);
}
}
Hope it helps !
Seeing as Pressy is an external controller (so to speak), you'll need to use a BroadcastReceiver with an intent-filter for: android.intent.action.MEDIA_BUTTON So, for example:
Inside your AndroidManifest:
<receiver android:name="RemoteControlReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
And your new receiver class:
public class RemoteControlReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
// handle media button here
KeyEvent key = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
// handle keydown etc
}
}
}
If you want to check single buttons (maybe on a cabled device, such as headphones, or wireless like Bluetooth), you could also use the onKeyDown and check for KeyEvent.KEYCODE_MEDIA_SOMEKEYIDENTIFIER Like so:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event){
switch(keyCode){
case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
return true;
case KeyEvent.KEYCODE_MEDIA_NEXT:
return true;
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
return true;
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
return true;
case KeyEvent.KEYCODE_MEDIA_REWIND:
return true;
case KeyEvent.KEYCODE_MEDIA_STOP:
return true;
// etc etc...
}
return false;
}
Hope this at least gets you started. Cheers.