连接到特定的 HID 配置文件蓝牙设备 [英] Connection to specific HID profile bluetooth device

查看:24
本文介绍了连接到特定的 HID 配置文件蓝牙设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将蓝牙条码扫描器连接到我的安卓平板电脑.条码扫描器与安卓设备结合作为输入设备 - HID 配置文件.它在系统蓝牙管理器中显示为键盘或鼠标.我发现蓝牙配置文件输入设备类存在但被隐藏.class 和 btprofile 常量在 android 文档中有 @hide 注释.

I connect bluetooth barcode scanner to my android tablet. barcode scanner is bonded with android device as a input device - HID profile. it shows as keyboard or mouse in system bluetooth manager. i discovered that bluetooth profile input device class exist but is hidden. class and btprofile constants have @hide annotaions in android docs.

隐藏类:

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.3.1_r1/android/bluetooth/BluetoothInputDevice.java

这里它们也应该是另外 3 个常量

here they should be also 3 other constants

developer.android.com/reference/android/bluetooth/BluetoothProfile.html#耳机

就像

public static final int INPUT_DEVICE = 4;
public static final int PAN = 5;
public static final int PBAP = 6;

常量很容易通过反射访问.我需要实现的是隐藏配置文件(INPUT_DEVICE)的设备列表.使用方法进行小的更改应该很简单:

that constants are simple accessible by reflection. What i need to achieve, is list of devices by hid profile(INPUT_DEVICE). it should be simple with small changes using method:

developer.android.com/reference/android/bluetooth/蓝牙A2dp.html#getConnectedDevices()

不是用于 A2dp 配置文件,而是用于也可以通过反射方法访问的 hid 配置文件.遗憾

not for A2dp profile, but for hid profile accessed also by reflection methods. sadly

Class c = Class.forName("android.bluetooth.BluetoothInputDevice")

不会工作..我应该如何解决这个问题的任何想法?我只需要隐藏设备列表

won't work.. any ideas how i should approach to the problem ? i need only list of hid devices

推荐答案

我想出了如何解决我的问题.很有帮助.首先,我需要准备反射方法,该方法返回 hid 配置文件的 input_device 隐藏常量:

I figured out how to solve my problem. That was very helpful. First of all I needed to prepare reflection method which return input_device hidden constants of hid profile:

public static int getInputDeviceHiddenConstant() {
    Class<BluetoothProfile> clazz = BluetoothProfile.class;
    for (Field f : clazz.getFields()) {
        int mod = f.getModifiers();
        if (Modifier.isStatic(mod) && Modifier.isPublic(mod) && Modifier.isFinal(mod)) {
            try {
                if (f.getName().equals("INPUT_DEVICE")) {
                    return f.getInt(null);
                }
            } catch (Exception e) {
                Log.e(LOG_TAG, e.toString(), e);
            }
        }
    }
    return -1;
}

代替那个函数,我可以使用值 4,但我想做得优雅.

Instead of that function, I could use value 4, but i want to do it elegant.

第二步是定义特定配置文件的侦听器:

Second step was to define listener of specific profile:

BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
        @Override
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
            Log.i("btclass", profile + "");
            if (profile == ConnectToLastBluetoothBarcodeDeviceTask.getInputDeviceHiddenConstans()) {
                List<BluetoothDevice> connectedDevices = proxy.getConnectedDevices();
                if (connectedDevices.size() == 0) {
                } else if (connectedDevices.size() == 1) {
                    BluetoothDevice bluetoothDevice = connectedDevices.get(0);
                    ...
                } else {
                    Log.i("btclass", "too many input devices");
                }
            }

        }

        @Override
        public void onServiceDisconnected(int profile) {

        }
    };

在第三步我调用了

mBluetoothAdapter.getProfileProxy(getActivity(), mProfileListener,
            ConnectToLastBluetoothBarcodeDeviceTask.getInputDeviceHiddenConstant());

一切正常,mProfileListener 返回特定配置文件蓝牙设备/-es 的列表.最有趣的事情发生在 onServiceConnected() 方法中,该方法返回隐藏类 BluetoothInputDevice 的对象 :)

Everything clearly works and mProfileListener returns me list of specific profile bluetooth device/-es. Most interesting thing takes place in onServiceConnected() method, which returs object of hidden class BluetoothInputDevice :)

这篇关于连接到特定的 HID 配置文件蓝牙设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆