Nih*_*s13 3 profile android input-devices bluetooth hid
我将蓝牙条码扫描器连接到我的Android平板电脑.条码扫描器与Android设备绑定作为输入设备 - HID配置文件.它在系统蓝牙管理器中显示为键盘或鼠标.我发现蓝牙配置文件输入设备类存在但是隐藏了.class和btprofile常量在android文档中有@hide annotaions.
隐藏类:
这里它们也应该是其他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;
Run Code Online (Sandbox Code Playgroud)
通过反射可以很容易地访问这些常量.我需要实现的是隐藏配置文件(INPUT_DEVICE)的设备列表.使用方法进行小改动应该很简单:
developer.android.com/reference/android/bluetooth/BluetoothA2dp.html#getConnectedDevices()
不适用于A2dp配置文件,但也适用于通过反射方法访问的hid配置文件.可悲的是
Class c = Class.forName("android.bluetooth.BluetoothInputDevice")
Run Code Online (Sandbox Code Playgroud)
将无法工作..任何想法我应该如何处理问题?我只需要隐藏设备列表
我想出了如何解决我的问题. 这非常有帮助.首先,我需要准备一个返回隐藏配置文件的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;
}
Run Code Online (Sandbox Code Playgroud)
而不是那个功能,我可以使用值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) {
}
};
Run Code Online (Sandbox Code Playgroud)
我调用了第三步
mBluetoothAdapter.getProfileProxy(getActivity(), mProfileListener,
ConnectToLastBluetoothBarcodeDeviceTask.getInputDeviceHiddenConstant());
Run Code Online (Sandbox Code Playgroud)
一切都清楚地工作,mProfileListener返回特定配置文件蓝牙设备列表/ -es.最有趣的事情发生在onServiceConnected()方法中,它返回隐藏类BluetoothInputDevice的对象:)