当前位置: 首页 > 知识库问答 >
问题:

如何在Android中配对(绑定)蓝牙设备后以编程方式连接到该设备

单于扬
2023-03-14

我一直在努力让键盘或遥控器等蓝牙设备连接到Android设备。更具体地说,当这个程序第一次运行时,它会扫描蓝牙设备,并尝试与找到的设备配对和连接。我尝试了所有可能的方法来实现这一点,但我只能配对设备,而不能完全连接。我已经在Android Bluetooth guide和其他许多手册中尝试了这些例子。一个一致性是javi。BluetoothSocket调用connect时发生io错误。

java.io.IOException: read failed, socket might closed or timeout, read ret: -1
at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:505)
at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:482)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:324)
at BTConnectThread.run(BTConnectThread.java:61)

我尝试了不同的UUID。有些是我自己生成的,有些是我从设备中提取的。我还尝试编写代码,假设两者都充当服务器,主要反映我在这里所做的事情以及Android蓝牙指南中的内容。已经尝试了在设备上调用createBond()的所有变体。所有尝试都使设备配对/绑定但未连接。非常感谢任何帮助。'html" target="_blank">public BTConnectThread(BluetoothDevice BluetoothDevice){

    BluetoothSocket tempSocket = null;

    try {
       // tempSocket =        bluetoothDevice.createRfcommSocketToServiceRecord(WELL_KNOWN_UUID);
      //  tempSocket = bluetoothDevice.createInsecureRfcommSocketToServiceRecord(WELL_KNOWN_UUID);

        //Magic?
        Method method = bluetoothDevice.getClass().getMethod("createRfcommSocket",
            new Class[]{int.class});
        tempSocket = (BluetoothSocket) method.invoke(bluetoothDevice, 1);

    } catch (Exception e) {
        e.printStackTrace();
    }

    m_bluetoothSocket = tempSocket;

}

public void run() {

    //cancel discovery
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (bluetoothAdapter != null)
        bluetoothAdapter.cancelDiscovery();

    //TODO: Try brute force approach. Loop until it connects.
    //TODO: Try a fallback socket.
    try {
        m_bluetoothSocket.connect();
        Log.d(TAG, "Connection Established");

    } catch (IOException connectException) {
        // Unable to connect; close the socket and get out
        Log.d(TAG, "Fail to connect!", connectException);
        try {
            m_bluetoothSocket.close();
        } catch (IOException closeException) {
            Log.d(TAG, "Fail to close connection", closeException);
        }
        return;
    }
}

public void cancel() {

    try {
        m_bluetoothSocket.close();
    } catch (IOException e) {

    }
}`

共有2个答案

卢元龙
2023-03-14

连接函数

    fun connect(btDevice: BluetoothDevice?){
        val id: UUID = btDevice?.uuids?.get(0)!!.uuid
        val bts = btDevice.createRfcommSocketToServiceRecord(id)
        bts?.connect()
    }

在主线程中调用此

    val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
    val device = bluetoothAdapter.getRemoteDevice("your mac address")
    connect(device)

宗晟
2023-03-14

蓝牙连接需要创建3个以上的线程,因此您可以尝试使用https://android-arsenal.com/details/1/1859.

 类似资料:
  • 我能够配对蓝牙键盘,但无法连接,使其成为输入设备。我查阅了开发商网站上提供的文件-http://developer.android.com/guide/topics/connectivity/bluetooth.html#Profiles 它说Android Bluetooth API提供了以下蓝牙配置文件的实现,但您可以实现接口BluetoothProfile来编写自己的类来支持特定的蓝牙配置文

  • 我如何获得Android所有已连接蓝牙设备的列表,而不考虑配置文件? 或者,我看到您可以通过BluetoothManager获取特定配置文件的所有连接设备。获取连接的设备。 我想我可以通过ACTION_ACL_CONNECTED/ACTION_ACL_DISCONNECTED监听连接/断开来查看哪些设备连接...似乎容易出错。 但我想知道是否有更简单的方法来获取所有已连接蓝牙设备的列表。

  • 我想连接第三方蓝牙设备到我的Android Wear手表(三星Gear Live)。我试图找到留档如何做到这一点,但我没有任何运气。所有的搜索,我似乎都认为我想连接到手机上。 有谁知道一个很好的例子来演示如何将蓝牙心率监视器(或其他设备)连接到Android Wear,以便我在手机不存在时保存历史记录?这可能吗?它是否与从手机/平板电脑上执行相同的协议?

  • 我在连接时遇到问题。起初它有效,而不是不起作用,除非我取消配对设备。我已经得到了所有可能发生的异常,套接字关闭,管道关闭,连接被拒绝,端口已经在使用中,等等。 我知道android 4.2之前版本的蓝牙存在问题(https://code.google.com/p/android/issues/detail?id=37725). 我在连接这些设备时遇到问题的设备: Htc one(Android4.

  • 问题内容: 我正在创建一个应通过蓝牙连接到特定设备的应用程序。 无论设备是否已配对,我都希望我的应用程序与此设备连接。 现在我有这个 但是此功能仅连接到配对的设备。如果我的设备尚未配对,我想将其配对。不知道该怎么做。 有人可以给我任何建议吗? 问题答案: 首次请求权限。 然后使您的设备可发现: 然后创建一个 BroadcastReceiver 来监听系统中的动作: 并通过注册以下 Boardcas

  • 连接设备 接口说明 用于连接 扫描出来的蓝牙设备。 参数说明 字段 类型 必须? 说明 device RKBLEDevice 是 蓝牙设备 示例代码 Swift: RokidMobileSDK.binder.connect(device: RKBLEDevice) Objc: [RokidMobileSDK.binder connect:device]; 断开设备 接口说明 用于断开已经连接的