gpt4 book ai didi

java - 蓝牙无法连接到OBD2

转载 作者:行者123 更新时间:2023-11-30 05:07:57 25 4
gpt4 key购买 nike

我正在使用我在 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。仅授予应用位置权限是不够的。您可以阅读更多相关信息 here .

关于java - 蓝牙无法连接到OBD2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54200271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com