简体   繁体   English

如何在 Android 5.0+ 上运行蓝牙 HID 主机

[英]How to get Bluetooth HID host on Android 5.0+ working

I have a bluetooth HID device that I need to get working with my app running on a Nexus 7 2013 tablet running custom build forked from android 5.0.我有一个蓝牙HID设备,我需要得到与从的是Android 5.0分叉一台Nexus 7平板电脑2013运行自定义生成运行我的应用程序工作 so:所以:

  • @hide is not a constraint @hide不是约束
  • Changes to AOSP are permitted允许更改 AOSP
  • Any Android version 5.0* - 5.1* is fine任何 Android 版本 5.0* - 5.1* 都可以

Now my device has a keyboard, AND sends and accepts some vendor-specific HID reports.现在我的设备有一个键盘,并且发送和接受一些供应商特定的 HID 报告。 By working , I mean sending and receiving all HID reports通过工作,我的意思是发送和接收所有 HID 报告

Based on the AOSP source-code exploration I have done, it seems that:基于我所做的 AOSP 源代码探索,似乎:

  1. The HidService service broadcasts BluetoothInputDevice.ACTION_REPORT intents for all input HID reports [My app has a BroadcastReceiver to receive ACTION_REPORT intents] HidService服务为所有输入 HID 报告广播BluetoothInputDevice.ACTION_REPORT意图 [我的应用程序有一个BroadcastReceiver来接收ACTION_REPORT意图]

  2. BluetoothInputDevice provides other interfaces like setReport() , sendData() , connect() etc through HidService . BluetoothInputDevice提供其它接口等setReport() sendData() connect()等通过HidService

  3. BluetoothInputDevice uses HidService to provide HID device interface. BluetoothInputDevice使用HidService提供 HID 设备接口。

  4. HidService.java is a JNI wrapper over com_android_bluetooth_hid.cpp HidService.java是通过JNI包装com_android_bluetooth_hid.cpp

  5. com_android_bluetooth_hid.cpp , on initialization of HidService , will register its own callbacks to Bluedroid com_android_bluetooth_hid.cpp ,在初始化HidService ,将注册自己的回调Bluedroid

I have added logs to AOSP to trace calls and seems all the above is correct.我已将日志添加到 AOSP 以跟踪调用,似乎以上所有内容都是正确的。

BUT

The get_report_callback() in com_android_bluetooth_hid.cpp never gets called, and consequently, My app does not receive ACTION_REPORT intents.get_report_callback()com_android_bluetooth_hid.cpp不会被调用,因此,我的应用程序不会收到ACTION_REPORT意图。

If I send dummy ACTION_REPORT intents from my app, then my BroadcastReceiver receives those intents.如果我从我的应用程序发送虚拟ACTION_REPORT意图,那么我的BroadcastReceiver会收到这些意图。

My HID device is connected and working, as I do see keys received in edittext field within my app.我的 HID 设备已连接并正常工作,因为我确实在我的应用程序的 edittext 字段中看到了收到的密钥。

Is there any way I could receive my device's HID reports in my app?有什么办法可以在我的应用程序中接收设备的 HID 报告? through intents or through any other way?通过意图还是通过任何其他方式?

If I could receive through Intents, is there any step that I missed?如果我可以通过 Intents 接收,是否有遗漏的步骤? I basically just have BroadcastReceiver with intent filter and BLUETOOTH and BLUETOOTH_ADMIN permissions for receiving HID reports.我基本上只有带有意图过滤器的BroadcastReceiver以及用于接收 HID 报告的BLUETOOTHBLUETOOTH_ADMIN权限。

If any other method must be used, please suggest.如果必须使用任何其他方法,请提出建议。

[Update 2] [更新2]

I probably didn't mention it clearly, but devices I work with implement HID over GATT and are BLE devices.我可能没有明确提到它,但我使用的设备通过 GATT 实现 HID,并且是 BLE 设备。

On newer Android versions (7,8,9), I am able to receive HID reports by subscribing to HID report notifications, Adding BLUETOOTH_PRIVILEGGED permission, and installing as platform app.在较新的 Android 版本(7、8、9)上,我可以通过订阅 HID 报告通知、添加 BLUETOOTH_PRIVILEGGED 权限并安装为平台应用程序来接收 HID 报告。

No other Android code jugglery was necessary for these versions.这些版本不需要其他 Android 代码杂耍。 I didn't (and won't) test with older versions of Android, but this solution might already work there.我没有(也不会)用旧版本的 Android 进行测试,但是这个解决方案可能已经在那里工作了。


[Previous Answer] [上一个答案]

BluetoothInputDevice didn't work at all. BluetoothInputDevice 根本不起作用。

I used /dev/hidraw* from C code to access the bluetooth hidraw devices.我使用 C 代码中的 /dev/hidraw* 来访问蓝牙 hidraw 设备。

mac address is NOT reported by /dev/hidraw* nodes. /dev/hidraw* 节点不报告 mac 地址。

In Android 5.x they work and all set/send/recv report operations are working.在 Android 5.x 中,它们工作并且所有设置/发送/接收报告操作都在工作。 get not tested.不被测试。

On Android 6.x, only recv operation is working from /dev/hidraw.在 Android 6.x 上,只有 recv 操作从 /dev/hidraw 工作。 set/send are not working.设置/发送不起作用。 get not tested.不被测试。

I will post update when I have better answer.当我有更好的答案时,我会发布更新。


Update更新

I modified bluedroid and added a small unix-socket server within it.我修改了 bluedroid 并在其中添加了一个小型 unix-socket 服务器。 Bluedroid delivers all incoming HID reports to this server, and accepts outgoing HID reports from it. Bluedroid 将所有传入的 HID 报告传送到此服务器,并接受来自它的传出 HID 报告。 My app communicate with this server over unnamed socket.我的应用程序通过未命名的套接字与此服务器通信。 All is well.一切都很好。

I just needed to setup socket's permissions SELinux correctly for this to work.我只需要正确设置套接字的 SELinux 权限即可使其正常工作。

This works for all versions of Bluedroid I could test with (4.4 - 6.0), but every version of Bluedroid needs it's own version of patch.这适用于我可以用(4.4 - 6.0)测试的所有版本的 Bluedroid,但每个版本的 Bluedroid 都需要它自己的补丁版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM