无法将蓝牙键盘连接到Android设备 [英] Not able to connect a Bluetooth Keyboard to an android device

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

问题描述

我正在尝试以编程方式将Android手机连接至蓝牙设备(已配对).我在线程中使用 createRfcommSocketToServiceRecord(UUID)方法.

I am trying to programmatically connect an Android phone to a Bluetooth device (which is already paired). I am using createRfcommSocketToServiceRecord(UUID) method in a thread.

我知道蓝牙键盘是HID设备,所以我将UUID用作

I know the Bluetooth keyboard is a HID device so I am using UUID as

00001124-0000-1000-8000-00805f9b34fb

上面的方法返回一个BluetoothSocket对象,但是当调用bluetoothSocket.connect()时,它无法连接到HID设备.它给出了 IOException:发现失败

The above method returns a BluetoothSocket object, but it fails to connect to the HID device, when bluetoothSocket.connect() is called. It gives IOException: discovery failed

我对此进行了很多搜索,但不知道如何解决此问题.

I have searched about this a lot, but no clue how to fix this.

在Android中使用这种方法是否只能连接Rfcomm而不连接HID设备? 那么,那我应该用什么替代方法呢?我正在将Samsung Galaxy S3与 在此应用程序中的Android 4.1以及配对后(通过设置),我可以使用输入 一些内置应用程序(如SMS,电子邮件等)中的蓝牙键盘. 在我看来,连接到它应该是可能的,但我不是 确定我是否使用正确的方法.我想在我的应用程序中读取按键序列数据.

Is it only possible to connect Rfcomm and not HID devices using this approach in Android? If so, then what alternate approach should I use for this? I am using Samsung Galaxy S3 with Android 4.1 in this application and after pairing (from settings) I could type using the Bluetooth keyboard in some inbuilt apps like SMS, Email etc. It seems to me that connecting to it should be possible, but I am not sure if I am using the right approach. I want to read the keypress serial data in my app.

这是我正在使用的代码:

Here's the code which I am using:

private class ConnectThread extends Thread {
    private final BluetoothSocket mmSocket;

    public ConnectThread(BluetoothDevice device) {
        BluetoothSocket tmp = null;
        try {
            tmp = device.createRfcommSocketToServiceRecord(mUUID);
        } catch (IOException e) {
            e.printStackTrace();
        }
        mmSocket = tmp;
    }

    public void run() {
        try {
            mmSocket.connect();
        } catch (IOException connectException) {
            try {
                mmSocket.close();
            } catch (IOException closeException) { }
            return;
        }

        Log.d(TAG, "ConnectThread connection successful");
    }

    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) { }
    }
}

推荐答案

在Android中使用这种方法只能连接Rfcomm而不连接HID设备吗?

Is is only possible to connect Rfcomm and not HID devices using this approach in Android?

是的,很不幸. java方法的命名非常清楚地表明仅支持rfcomm. HID最有可能在原始的l2cap上运行.

Yes, unfortunately. The naming of the java methods makes it quite clear that only rfcomm is supported. HID most likely operates on raw l2cap.

尝试使用 InputDevice

Try using InputDevice or takeKeyEvents to get keypress data in your app.

这篇关于无法将蓝牙键盘连接到Android设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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