簡體   English   中英

藍牙無法連接到OBD2

[英]Bluetooth Unable to connect to OBD2

我正在使用在github上找到的一個應用程序,該應用程序可以測試並查看藍牙聊天如何與我的ELM327配合使用,但是當我配對並嘗試與其連接時,連接失敗。 之后,我嘗試拿起舊手機並設置與它的連接,然后它開始工作了。 我什至可以毫無問題地發送數據。 我認為這可能與低功耗藍牙技術有關(我是Java和編碼技術的新手,所以這只是一個賭注),但我真的不知道如何解決它,因為logcat不會給我錯誤,該應用程序只是說它無法連接設備。

那就是我用來測試和了解它的應用程序的來源: 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) {
        }
    }
}

從Android 6或更高版本開始,您必須啟用手機上的位置才能使BLE正常工作。 僅僅授予應用位置權限是不夠的。 您可以在此處了解更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM