Android 蓝牙连接 HID 设备

Android 蓝牙连接 HID 设备

我连接的是蓝牙拍照遥控器。

最开始按照网上的思路,以为需要使用 BLE 方式去连接设备,然后接收设备的数据。连接成功后,一直收不到消息。

最后,机缘巧合下,更换连接方式,使用蓝牙 HID 方式去配对设备,但是没有给出如何收消息。

重写了 dispatchKeyEvent 方法,发现按蓝牙遥控器,有按键回调消息。

原来这样的 HID 设备,我们可以不用自己去连接,直接使用系统的蓝牙连接后,然后在 dispatchKeyEvent 中对按键事件处理即可。

java 复制代码
    private void getDeviceInfo(int id) {
        InputManager im = (InputManager) getSystemService(Context.INPUT_SERVICE);
        InputDevice device = im.getInputDevice(id);
        Log.e("WillWolf", "getDeviceInfo-->" + device.toString());
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        Log.e("WillWolf", "dispatchKeyEvent-->");
        Toast.makeText(this, "按钮点击", Toast.LENGTH_SHORT).show();

		// 这里通过事件的 device id,可以获取到输入的设备信息,比如我的是绿联的蓝牙遥控器,那么打印的信息中,设备名字就是 UGREEN,可以根据这个对事件来源做区分。
        getDeviceInfo(event.getDeviceId());

		// 这里的事件来源不一定是 MOUSE 也可能是 SOURCE_KEYBOARD
        if (event.getSource() == InputDevice.SOURCE_MOUSE) {
            Log.e("WillWolf", "dispatchKeyEvent--> SOURCE_MOUSE");
        }
        return true;
    }

哎,折腾了半天,才发现原来某些 HID 类型的设备,或许不需要使用 BLE 方式去获取信息。

相关推荐
HealthScience1 小时前
【Bib 2026】基因最新综述(有什么任务、benchmark、代表性模型)
android·开发语言·kotlin
夏沫琅琊2 小时前
Android拨打电话技术文档
android·kotlin
a2591748032-随心所记2 小时前
android studio gradle快速编译配置
android·android studio
一块小土坷垃3 小时前
# 《电影猎手》观影伴侣:一款支持iOS/安卓/电视盒子的全平台影视工具“电影猎手”(附自用评价)
android·ios·电视盒子
敲代码的鱼哇5 小时前
发送短信/拨打电话/获取联系人能力 UTS 插件(cz-sms)
android·前端·ios·uni-app·安卓·harmonyos·鸿蒙
用户5052372099155 小时前
Android 13/14 通知权限与前台服务适配指南
android
用户5052372099155 小时前
Android 12 适配指南:SplashScreen API 与 PendingIntent 变更
android
用户5052372099155 小时前
一张表看懂 Android 8-15 所有适配要点
android
_祝你今天愉快6 小时前
Android 12 (AOSP) 添加自定义系统服务
android
程序员陆业聪8 小时前
AI编码提效实战:Skill、Rule与上下文工程
android