简体   繁体   English

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

[英]How to programmatically connect to a Bluetooth device after it's already paired (bonded) in Android

I have been working on trying to get a Bluetooth device like a keyboard or a remote to connect to an android device.我一直在努力尝试让蓝牙设备(如键盘或遥控器)连接到 android 设备。 More specifically when this program runs for the first time it would scan for Bluetooth devices and attempt to pair and connect with one that it finds.更具体地说,当这个程序第一次运行时,它会扫描蓝牙设备并尝试与它找到的设备配对和连接。 I have tried seemingly every possible way to accomplish this but I am only able to pair the device, not connect it completely.我已经尝试了似乎所有可能的方法来实现这一点,但我只能配对设备,不能完全连接它。 I have tried the examples in the Android Bluetooth guide and many others.我已经尝试过 Android 蓝牙指南和许多其他指南中的示例。 One consistency is the javi.io error I get when the BluetoothSocket is calling connect.一种一致性是当 BluetoothSocket 调用连接时出现 javi.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)

I have tried different UUIDs.我尝试了不同的 UUID。 Some I generated myself others I pulled from the devices.有些是我自己生成的,有些是我从设备中提取的。 I also tried writing code assuming both are acting as servers that mirrors mostly what I am doing here and what is in the Android Bluetooth guide.我还尝试编写代码,假设两者都充当服务器,主要反映我在这里所做的事情以及 Android 蓝牙指南中的内容。 have tried all variations of calling createBond() on the device.已尝试在设备上调用 createBond() 的所有变体。 All attempts leave the device paired/bonded but not connected.所有尝试都会使设备配对/绑定但未连接。 Any help is greatly appreciated.任何帮助是极大的赞赏。 ` public BTConnectThread(BluetoothDevice bluetoothDevice) { `公共BTConnectThread(蓝牙设备蓝牙设备){

    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) {

    }
}`

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

Kotlin科特林

Connect Function连接功能

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

Call this in main thread在主线程中调用它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android蓝牙连接到配对的设备 - Android bluetooth connect to paired device 如何在 Android Studio 中的应用程序启动时连接配对的蓝牙设备? - How to connect paired bluetooth device on app startup in Android Studio? 自动连接到Android上预定的蓝牙配对设备 - Automatically connect to predetermined bluetooth paired device on Android 如何通过配对/绑定设备列表中的名称检索蓝牙地址? - How to retrieve Bluetooth Address through Name from Paired/bonded device list? 连接到Android上已配对的设备时提示输入蓝牙PIN码 - Prompted for Bluetooth PIN when connecting to already paired device on Android 如何通过单击项目连接到列表视图中的配对蓝牙设备 - How to connect to a paired Bluetooth device in a List View by clicking the item 以编程方式连接到配对的蓝牙扬声器并播放音频 - Programmatically connect to paired Bluetooth speaker and play audio 如何在 Android 中断开但不取消配对已配对的蓝牙耳机 - How to disconnect but not unpair an already paired bluetooth headphone in Android 如何检查配对的蓝牙设备是打印机还是扫描仪(Android) - How to Check if an Paired Bluetooth device is a Printer or a Scanner (Android) Android 上的蓝牙 - 如何连接到正确的蓝牙设备? - Bluetooth on Android - how to connect to the CORRECT bluetooth device?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM