简体   繁体   中英

Bluetooth Unable to connect to OBD2

I'm using one app that I found on github to be able to test and see how bluetooth chat works with my ELM327 but when I pair and try to connect with it the connection fails. After that I tried getting my old phone and setting the connection with it and it worked. I could even send data without problems. I think it could be something with Bluetooth Low Energy (I'm new with Java and coding so its just a bet) but I don't really know how to figure it out since logcat doesn't give me an error, the app just say it's unable to connect device.

Thats the source of the app im using to test and learn about it: https://github.com/DevExchanges/BluetoothChatAppAndroid

 // runs while listening for incoming connections
private class AcceptThread extends Thread {
    private final BluetoothServerSocket serverSocket;

    public AcceptThread() {
        BluetoothServerSocket tmp = null;
        try {
            tmp = bluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord(APP_NAME, MY_UUID);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        serverSocket = tmp;
    }

    public void run() {
        setName("AcceptThread");
        BluetoothSocket socket;
        while (state != STATE_CONNECTED) {
            try {
                socket = serverSocket.accept();
            } catch (IOException e) {
                break;
            }

            // If a connection was accepted
            if (socket != null) {
                synchronized (ChatController.this) {
                    switch (state) {
                        case STATE_LISTEN:
                        case STATE_CONNECTING:
                            // start the connected thread.
                            connected(socket, socket.getRemoteDevice());
                            break;
                        case STATE_NONE:
                        case STATE_CONNECTED:
                            // Either not ready or already connected. Terminate
                            // new socket.
                            try {
                                socket.close();
                            } catch (IOException e) {
                            }
                            break;
                    }
                }
            }
        }
    }

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

// runs while attempting to make an outgoing connection
private class ConnectThread extends Thread {
    private final BluetoothSocket socket;
    private final BluetoothDevice device;

    public ConnectThread(BluetoothDevice device) {
        this.device = device;
        BluetoothSocket tmp = null;
        try {
            tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) {
            e.printStackTrace();
        }
        socket = tmp;
    }

    public void run() {
        setName("ConnectThread");

        // Always cancel discovery because it will slow down a connection
        bluetoothAdapter.cancelDiscovery();

        // Make a connection to the BluetoothSocket
        try {
            socket.connect();
        } catch (IOException e) {
            try {
                socket.close();
            } catch (IOException e2) {
            }
            connectionFailed();
            return;
        }

        // Reset the ConnectThread because we're done
        synchronized (ChatController.this) {
            connectThread = null;
        }

        // Start the connected thread
        connected(socket, device);
    }

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

From Android 6 upwards, you have to enable location on the phone for BLE to work. It's not enough to grant the app location permission. You can read more about it here .

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