繁体   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