问题
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
回答1:
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.
回答2:
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();
}
};
回答3:
Using accessibility service, You can achieve auto-click.
Answer explained here, May be it's helpful to you !
来源:https://stackoverflow.com/questions/27440274/how-to-detect-click-event-of-connected-bluetooth-peripheral-device-selfie-stick