简体   繁体   中英

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. 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. One consistency is the javi.io error I get when the BluetoothSocket is calling connect.

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. 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. have tried all variations of calling createBond() on the device. All attempts leave the device paired/bonded but not connected. Any help is greatly appreciated. ` 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) {

    }
}`

蓝牙连接需要创建 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM