【问题标题】:How to detect click event of connected Bluetooth peripheral device (Selfie stick)?如何检测连接的蓝牙外围设备(自拍杆)的点击事件?
【发布时间】:2018-11-27 19:40:40
【问题描述】:

我的手机已连接自拍杆。我可以使用以下代码找到设备 ID:

    @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();
            }
         }
    };

我的问题是如何检测此连接的外围设备的按钮按下/单击事件?

以代码 sn-p/tutorial/cmets 形式提供的帮助非常值得赞赏。 谢谢!!!

编辑:

当我按下自拍杆音量+按钮时,听事件

【问题讨论】:

  • 你有解决办法吗,如果有请分享一下
  • @YogeshSeralia,是的,我找到了解决方案。请参阅我发布的答案。

标签: android bluetooth bluetooth-lowenergy android-bluetooth


【解决方案1】:

我找到了答案。这很简单。只需覆盖 Activity 的onKeyDown() 方法。

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    return super.onKeyDown(keyCode, event);
}

这里keyCode是返回的事件名称。

【讨论】:

  • 您的应用程序是使用默认相机应用程序来拍摄图像还是您已经实现了自定义相机来处理它?请告诉我。
  • 它使用的是默认相机应用,但我认为这两种情况都可以使用
  • 我已经为此实现了自定义相机应用程序,但您能否指导我如何使用默认相机应用程序?
【解决方案2】:

我对您的代码做了些微修改。看看有没有用

@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);
}

//监听蓝牙广播的BroadcastReceiver

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】:

使用accessibility service,可以实现自动点击。

Answer explained here,可能对你有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-13
    • 1970-01-01
    • 2016-12-28
    相关资源
    最近更新 更多