按下蓝牙设备按钮应在应用程序中触发Onclick监听器 [英] Bluetooth Device Button Press Should Trigger Onclick Listener in the app
问题描述
我正在尝试制作一个在配对的蓝牙设备上按下按钮时触发该应用程序中的点击监听器的应用程序.搜寻了几个小时后,我认为我无法将蓝牙设备按钮的键码发送到广播接收器,如果该键码匹配,那么我可以呼叫点击监听器,或者我的方法和理解是错误的,所以有人可以指导我吗或指出正确的方法?预先感谢
蓝牙设备:Bluetooth Selfie Remote AB快门3
我想要这样的东西 http://www.barbatricks.com/zh/android-en/remap-ab-shutter-3-selfie-remote/
我尝试了以下链接作为参考,但没有成功
用于ACTION_MEDIA_BUTTON的广播接收器无法正常工作 >
http://blog.phonedeveloper.com/2015/04/how-to-receive-bluetooth-broadcast.html
好, Android-我的应用程序中使用的音量按钮& Markus Kauppinen的解释解决了我的问题.单击 AB Shutter 3自拍遥控器上的按钮时,MainActivity中的以下代码可帮助您触发应用程序中的功能.在我的应用中将遥控器的按钮检测为Android按钮ENTER
键盘代码66
& iOS按钮VOLUME_UP
键码24
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
takeKeyEvents(true);
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_DOWN) {
Log.i("VOL_UP_pressed", String.valueOf(event.getKeyCode()));
Toast.makeText(getApplication(), "IOS button clicked", Toast.LENGTH_SHORT).show();
}
return true;
case KeyEvent.KEYCODE_ENTER:
if(action==KeyEvent.ACTION_DOWN){
Log.i("ENTER_pressed", String.valueOf(event.getKeyCode()));
Toast.makeText(getApplication(), "ANDROID button clicked", Toast.LENGTH_SHORT).show();
}
default:
return super.dispatchKeyEvent(event);
}
}
但请注意:它将仅在您的应用程序中检测到KeyEvents
,而不会在应用程序未运行时检测到.我的下一步是查找如何在配对的AB Shutter 3自拍遥控器上按下按钮时启动该应用程序以及该应用程序中的点击侦听器.有人提出建议,指导或任何帮助吗?预先感谢
I'm trying to make an app which triggers an on click listener in the app when a button is pressed on the paired bluetooth device. After googling for several hours I think I am unable to send keycode of the button of the bluetooth device to broadcast receiver where if the keycode matchs then i can call the on click listener or maybe my approach and understanding is wrong so Could anyone please guide me or point me towards the right approach? Thanks in advance
Bluetooth device: Bluetooth Selfie Remote AB shutter 3
I want something like this http://www.barbatricks.com/en/android-en/remap-ab-shutter-3-selfie-remote/
I have tried the following links for reference but no success
How to capture key events from bluetooth headset with android
BroadcastReceiver for ACTION_MEDIA_BUTTON not working
How to detect bluetooth call/media button press in android app
http://blog.phonedeveloper.com/2015/04/how-to-receive-bluetooth-broadcast.html
Well, Android - Volume Buttons used in my application & Markus Kauppinen's explanation solved my problem. The below code in the MainActivity can help you trigger a function within your app when a button on a AB Shutter 3 selfie remote is clicked. Buttons of the remote are detected in my app as Android button ENTER
Keycode 66
& iOS button VOLUME_UP
Keycode 24
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
takeKeyEvents(true);
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_DOWN) {
Log.i("VOL_UP_pressed", String.valueOf(event.getKeyCode()));
Toast.makeText(getApplication(), "IOS button clicked", Toast.LENGTH_SHORT).show();
}
return true;
case KeyEvent.KEYCODE_ENTER:
if(action==KeyEvent.ACTION_DOWN){
Log.i("ENTER_pressed", String.valueOf(event.getKeyCode()));
Toast.makeText(getApplication(), "ANDROID button clicked", Toast.LENGTH_SHORT).show();
}
default:
return super.dispatchKeyEvent(event);
}
}
BUT PLEASE NOTE: It will detect KeyEvents
only within your app, not while the app is not running. My next step is to find how to start the app and an on clicklistener within that app when a button is pressed on the paired AB Shutter 3 selfie remote Anyone with suggestions, guidance or any sort of help? Thanks in advance
这篇关于按下蓝牙设备按钮应在应用程序中触发Onclick监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!