我正在尝试实现一个应用程序,该应用程序(错误)使用蓝牙相机快门释放小工具来实现完全不同的目的。这是有问题的小工具:
http://www.amazon.co.uk/iPazzPort-Bluetooth-Shutter-Android-Smartphones/dp/B00MRTFB4M
据我所知,它使用蓝牙 v3 并且是 HID 设备。它显然是通过模拟“音量增大”(或者可能是“音量减小”?)来触发相机应用程序快门。不管怎样,它似乎工作得很好,尽管有时你必须按两次按钮 - 我认为也许第一次按会重新建立蓝牙连接,第二次和随后的按然后就可以工作了。
我已经使用两台运行 Android 2.3 的不同设备对其进行了测试。我确实希望向后兼容该版本的 Android。
我想要做的是以某种方式监视来自该设备的所有输入,以便我的应用程序可以检测何时按下按钮,然后执行它想要使用该设备的操作。 (这是一种紧急警报系统,因此您可以按下按钮来表明您需要帮助。)
我不想尝试通过蓝牙与设备进行通信。 Android 已经这样做了,而且它正在发挥作用,而且我所读到的有关蓝牙和 HID 协议(protocol)的内容让我希望尽可能避免使用它。)
我尝试重写 onKeyDown() 、 onKeyUp() 和dispatchKeyEvent() 。有时他们会接到电话,有时则不会。当他们接到电话时,我看到了意想不到的键码,例如 66(Enter)和 8(“1”)。
我要问的是,是否有某种方法可以监控来自该蓝牙 HID 设备的所有输入,而无需参与蓝牙 HID 协议(protocol)支持?
最佳答案
我从来没有找到我的问题的真正答案,但幸运的是我找到了解决方法。
这个特殊的蓝牙小工具总是连接到配对的设备,发送一些文本,然后断开连接。所以我正在做的是创建一个 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?
关于android - 如何监控蓝牙 HID 小工具的所有输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27801452/