按下蓝牙设备按钮应触发应用程序中的 Onclick 侦听器 [英] Bluetooth Device Button Press Should Trigger Onclick Listener in the app

查看:27
本文介绍了按下蓝牙设备按钮应触发应用程序中的 Onclick 侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个应用程序,当在配对的蓝牙设备上按下按钮时,该应用程序会触发应用程序中的点击侦听器.谷歌搜索几个小时后,我想我无法将蓝牙设备按钮的键码发送到广播接收器,如果键码匹配,那么我可以调用点击监听器,或者我的方法和理解是错误的,所以有人可以指导我或者指出我正确的方法?提前致谢

蓝牙设备:蓝牙自拍遥控AB快门3

我想要这样的东西http://www.barbatricks.com/en/android-en/remap-ab-shutter-3-selfie-remote/

我尝试了以下链接以供参考,但没有成功

如何从蓝牙耳机捕获关键事件安卓

ACTION_MEDIA_BUTTON 的广播接收器不工作>

如何检测蓝牙通话/媒体在Android应用程序中按下按钮

http://blog.phonedeveloper.com/2015/04/how-to-receive-bluetooth-broadcast.html

解决方案

好吧,Android - 我的应用程序中使用的音量按钮 &Markus Kauppinen 的解释解决了我的问题.MainActivity 中的以下代码可以帮助您在点击 AB Shutter 3 自拍遥控器上的按钮时触发应用内的功能.在我的应用程序中检测到遥控器按钮为 Android 按钮 ENTER Keycode 66 &iOS 按钮 VOLUME_UP 键码 24

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);采取KeyEvents(真);}@覆盖公共布尔 dispatchKeyEvent(KeyEvent 事件){int action = event.getAction();int keyCode = event.getKeyCode();开关(键码){案例 KeyEvent.KEYCODE_VOLUME_UP:如果(动作== KeyEvent.ACTION_DOWN){Log.i("VOL_UP_pressed", String.valueOf(event.getKeyCode()));Toast.makeText(getApplication(), "IOS 按钮被点击", Toast.LENGTH_SHORT).show();}返回真;案例 KeyEvent.KEYCODE_ENTER:if(action==KeyEvent.ACTION_DOWN){Log.i("ENTER_pressed", String.valueOf(event.getKeyCode()));Toast.makeText(getApplication(), "ANDROID 按钮被点击", Toast.LENGTH_SHORT).show();}默认:返回 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆