如何在Android上以编程方式配对和连接HID蓝牙设备(蓝牙键盘) [英] How to programmatically pair and connect a HID bluetooth device(Bluetooth Keyboard) on Android

查看:1176
本文介绍了如何在Android上以编程方式配对和连接HID蓝牙设备(蓝牙键盘)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够配对蓝牙键盘,但无法连接以使其成为输入设备. 我浏览了开发人员站点上提供的文档- http://developer.android .com/guide/topics/connectivity/bluetooth.html#Profiles

I am able to pair a bluetooth keyboard but not able to connect so as to make it an input device. I went through the documentation provided at developer site - http://developer.android.com/guide/topics/connectivity/bluetooth.html#Profiles

它说Android蓝牙API提供了以下蓝牙配置文件的实现,但是您可以实现接口BluetoothProfile来编写自己的类以支持特定的蓝牙配置文件.

It says that the Android Bluetooth API provides implementations for the following Bluetooth profiles but you can implement the interface BluetoothProfile to write your own classes to support a particular Bluetooth profile.

  • 耳机
  • A2DP
  • 健康设备

没有文档说明如何为HID蓝牙设备(键盘)实现BluetoothProfile

There is no documentation how to implement BluetoothProfile for HID bluetooth device(Keyboard)

Android本身已为HID设备实现了蓝牙连接,但那些API却被隐藏了.我也尝试使用反射.我没有收到任何错误,但没有将键盘连接为输入设备.这就是我所做的-

Android has itself implemented bluetooth connection for HID devices but those API's are hidden. I tried reflection to use them too. I do not get any error but keyboard does not get connected as input device. This is what i have done -

private void connect(final BluetoothDevice bluetoothDevice) {
    if(bluetoothDevice.getBluetoothClass().getDeviceClass() == 1344){
        final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
                @Override
                public void onServiceConnected(int profile, BluetoothProfile proxy) {
                    Log.i("btclass", profile + "");

                    if (profile == getInputDeviceHiddenConstant()) {
                        Class instance = null;
                        try {
                            //instance = Class.forName("android.bluetooth.IBluetoothInputDevice");
                            instance = Class.forName("android.bluetooth.BluetoothInputDevice");
                            Method connect = instance.getDeclaredMethod("connect", BluetoothDevice.class);
                            Object value = connect.invoke(proxy, bluetoothDevice);
                            Log.e("btclass", value.toString());
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        } catch (NoSuchMethodException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }



                    }
                }

                @Override
                public void onServiceDisconnected(int profile) {

                }
            };

            mBluetoothAdapter.getProfileProxy(this, mProfileListener,getInputDeviceHiddenConstant());


    }

}

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("", e.toString(), e);
            }
        }
    }
    return -1;
}

推荐答案

由于安全原因,第三方应用程序无法连接到蓝牙键盘,因为该应用程序可以是键盘记录程序.因此只能由用户手动完成.

Due to security reasons, it is not possible for third party applications to connect to a bluetooth keyboard as the application can be a keylogger. So it can be only done manually by the user.

这篇关于如何在Android上以编程方式配对和连接HID蓝牙设备(蓝牙键盘)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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