4

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

隐藏类:

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

在这里,它们也应该是其他 3 个常量

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;

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

developer.android.com/reference/android/bluetooth/BluetoothA2dp.html#getConnectedDevices()

不是用于 A2dp 配置文件,而是用于也通过反射方法访问的隐藏配置文件。可悲的是

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

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

4

1 回答 1

6

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

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,但我想优雅地做到这一点。

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

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 的对象:)

于 2014-10-17T12:24:26.513 回答