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

时间:2015-01-06 15:09:17

标签: android hid

我正在尝试实现一个应用程序(误)使用蓝牙相机快门释放小工具,以达到完全不同的目的。这是有问题的小工具:

http://www.amazon.co.uk/iPazzPort-Bluetooth-Shutter-Android-Smartphones/dp/B00MRTFB4M

据我所知,这使用蓝牙v3并且是HID设备。它显然是通过模拟“音量增大”(或者可能是“音量减小”?)来激活相机应用程序快门。无论如何,它似乎确实运作得很好,虽然有时你必须按两次按钮 - 我想可能是第一次按下重新建立蓝牙连接而第二次按下然后按下然后工作。

我已经使用运行Android 2.3的两个不同设备对其进行了测试。我确实希望向后兼容该版本的Android。

我想要做的是以某种方式监视来自此设备的所有输入,因此我的应用程序可以检测按钮何时被按下,然后执行它想要使用该设备的内容。 (这是一种紧急报警系统,所以你可以按下按钮表示你需要帮助。)

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

我已经尝试重写onKeyDown()和onKeyUp()以及dispatchKeyEvent()。有时他们会被召唤,有时他们却没有。当他们被调用时,我会看到意外的键码,如66(回车)和8(“1”)。

我要问的是,是否有某种方法可以监控来自此蓝牙HID设备的所有输入,而无需参与蓝牙HID协议支持?

1 个答案:

答案 0 :(得分:1)

我从来没有找到我的问题的真实答案,但幸运的是我找到了解决办法。

此特定蓝牙小工具始终连接到配对设备,发送一些文本,然后断开连接。所以我正在做的是创建一个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;
      }
   }

这是受这个问题和答案的启发:How to receive intents for Bluetooth devices working?