Android 如何监视蓝牙HID小工具的所有输入

Android 如何监视蓝牙HID小工具的所有输入,android,hid,Android,Hid,我正在尝试实现一个应用程序,它(mis-)使用一个蓝牙相机快门释放小工具来实现一个完全不同的目的。下面是有问题的小工具: 据我所知,它使用Bluetooth v3,是一款HID设备。它显然是通过模拟“音量增大”(或者“音量减小”)来启动相机应用程序快门的。无论如何,它似乎工作得很好,尽管有时你必须按两次按钮——我认为第一次按下按钮可能会重新建立蓝牙连接,第二次,以及随后的几次,按下按钮就可以工作了 我已经用两台运行安卓2.3的不同设备对其进行了测试。我确实想向后兼容那个版本的Android 我

我正在尝试实现一个应用程序,它(mis-)使用一个蓝牙相机快门释放小工具来实现一个完全不同的目的。下面是有问题的小工具:

据我所知,它使用Bluetooth v3,是一款HID设备。它显然是通过模拟“音量增大”(或者“音量减小”)来启动相机应用程序快门的。无论如何,它似乎工作得很好,尽管有时你必须按两次按钮——我认为第一次按下按钮可能会重新建立蓝牙连接,第二次,以及随后的几次,按下按钮就可以工作了

我已经用两台运行安卓2.3的不同设备对其进行了测试。我确实想向后兼容那个版本的Android

我想做的是以某种方式监控该设备的所有输入,这样我的应用程序就可以检测到按钮何时被按下,然后执行它想使用该设备的操作。(这是一种紧急报警系统,您可以按下按钮表示需要帮助。)

我不想参与尝试通过蓝牙与设备通信。Android已经在这样做了,而且它正在工作,我读到的关于蓝牙和HID协议的内容让我想尽可能避免使用它。)

我已尝试重写onKeyDown()和onKeyUp()以及dispatchKeyEvent()。有时他们会接到电话,有时他们不会。当他们被呼叫时,我会看到意想不到的键码,如66(回车)和8(“1”)


我想问的是,是否有某种方法可以监视此蓝牙HID设备的所有输入,而不必参与蓝牙HID协议支持?

我从未找到我的问题的真正答案,但幸运的是,我找到了解决方法

这个特殊的蓝牙小工具总是连接到配对设备,发送一些文本,然后断开连接。所以我要做的是创建一个BroadcastReceiver来获取蓝牙连接(和断开连接)事件,并使用它来激活警报

   // Class used to receive Bluetooth connection and disconnect events. This checks the action is as
   // expected (probably unnecessary) and that a request type has been selected, and sends the
   // activation message to OutBack Server if so. (Monitoring the disconnect events is probably
   // unnecessary, but is done just in case that saves a situation where a connection has been
   // missed, or something.)
   public class BluetoothReceiver extends BroadcastReceiver {

      @Override
      public void onReceive(Context androidContext, Intent androidIntent) {

         String actionOrNull = androidIntent.getAction();
         if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(actionOrNull) ||
             BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(actionOrNull)) {
            Log.d(TAG, "BluetoothReceiver.onReceive() " + actionOrNull);
            if (_btnActivate.isEnabled()) {
               sendRequestActivationToServer();
            }
         }
      }
   }

这个问题和答案启发了我们:

   // Reference to the object used to monitor Bluetooth connections and disconnections
   private BluetoothReceiver _bluetoothReceiver = null;
   // Last lifecycle method called before fragment becomes active. This is apparently the
   // recommended place to register a receiver object, and is used to register a receiver object to
   // monitor Bluetooth connections and disconnections.
   @Override
   public void onResume() {
      super.onResume();

      _bluetoothReceiver = new BluetoothReceiver();

      IntentFilter intentFilter = new IntentFilter();
      intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
      intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);

      getActivity().registerReceiver(_bluetoothReceiver, intentFilter);
   }
   // First lifecycle method called when a fragment is on its way to being paused or destroyed. This
   // is apparently the recommended place to unregister a receiver object, and is used to unregister
   // the receiver object that monitors Bluetooth connections and disconnections.
   @Override
   public void onPause() {
      super.onPause();

      if (_bluetoothReceiver != null) {
         getActivity().unregisterReceiver(_bluetoothReceiver);
         _bluetoothReceiver = null;
      }
   }