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

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

问题描述

我的蓝牙棒code扫描仪连接到我的Andr​​oid平板电脑。酒吧code扫描仪结合了Android设备作为输入设备 - HID配置文件。它示出了在系统蓝牙管理键盘或鼠标。我发现蓝牙配置文件的输入设备类存在,但处于隐藏状态。类btprofile常量在android的文档@hide annotaions。

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.

隐藏类:

<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.3.1_r1/android/bluetooth/BluetoothInputDevice.java\" rel=\"nofollow\">http://grep$c$c.com/file/repository.grep$c$c.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#HEADSET

就像

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

这是常量是通过反射简单访问。
我需要实现什么,是HID模式(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:

<一个href=\"http://developer.android.com/reference/android/bluetooth/BluetoothA2dp.html#getConnectedDevices()\" rel=\"nofollow\">developer.android.com/reference/android/bluetooth/BluetoothA2dp.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")

将不会工作。
任何想法如何,我应该解决问题的方法?我只需要HID设备的列表

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

推荐答案

我想出如何解决我的问题。
是非常有益的。
首先,我需要prepare反射方法,它返回input_device HID模式的隐藏常量:

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()方法里,把returs隐藏类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天全站免登陆