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 方式去获取信息。

相关推荐
Lei活在当下2 小时前
【Perfetto从入门到精通】2. 使用 Perfetto 追踪/分析 APP 的 Native/Java 内存
android·性能优化·架构
愤怒的代码2 小时前
🔗 深度解析 SystemUI 进程间通信机制(一)
android·操作系统·app
RainyJiang3 小时前
聊聊协程里的 Semaphore:别让协程挤爆门口
android·kotlin
Dev7z5 小时前
在MySQL里创建数据库
android·数据库·mysql
invicinble5 小时前
mysql建立存数据的表(一)
android·数据库·mysql
似霰6 小时前
传统 Hal 开发笔记1----传统 HAL简介
android·hal
Zender Han7 小时前
Flutter Gradients 全面指南:原理、类型与实战使用
android·flutter·ios
火柴就是我7 小时前
Flutter Path.computeMetrics() 的使用注意点
android·flutter
モンキー・D・小菜鸡儿8 小时前
Android 系统TTS(文字转语音)解析
android·tts
2501_915909068 小时前
iOS 反编译防护工具全景解析 从底层符号到资源层的多维安全体系
android·安全·ios·小程序·uni-app·iphone·webview