gpt4 book ai didi

android - 基于安卓蓝牙聊天示例的应用程序服务发现失败

转载 作者:太空狗 更新时间:2023-10-29 13:28:25 24 4
gpt4 key购买 nike

我正在基于安卓蓝牙聊天示例编写一个带有蓝牙部分的蓝牙游戏。我有两部手机要测试。这是问题所在,当我将一部手机连接到另一部手机时,它有时会显示“无法连接设备”包,但是当我运行蓝牙聊天示例时,它从未显示过,所以我认为这不是设备的问题.有没有研究过蓝牙聊天示例并且有同样问题的人可以给我一些帮助?

我尝试打印异常,它类似于“java.io.IOException:服务发现失败”。这是导致异常的代码。

private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;

public ConnectThread(BluetoothDevice device) {
mmDevice = device;
BluetoothSocket tmp = null;

// Get a BluetoothSocket for a connection with the
// given BluetoothDevice
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Log.e(TAG, "create() failed", e);
}
mmSocket = tmp;
}

public void run() {
Log.i(TAG, "BEGIN mConnectThread");
setName("ConnectThread");

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

// Make a connection to the BluetoothSocket
try {
// This is a blocking call and will only return on a
// successful connection or an exception
mmSocket.connect();
} catch (IOException e) {
Log.e("error", e.toString());
connectionFailed();
// Close the socket
try {
mmSocket.close();
} catch (IOException e2) {
Log.e(TAG, "unable to close() socket during connection failure", e2);
}
// Start the service over to restart listening mode
BluetoothChatService.this.start();
return;
}

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

// Start the connected thread
connected(mmSocket, mmDevice);
}

public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
}

准确位置是
mmSocket.connect();

最佳答案

我知道您会觉得答案很奇怪,老实说,我不太擅长 UUID。但我遇到了同样的问题,并使用它解决了这个问题。

回答
我认为您正在使用 BluetoothChat 应用程序中硬编码的 UUID。当我将这些 UUID 更改为 Well Known UUID 即; "00001105-0000-1000-8000-00805F9B34FB" 它解决了我的问题,我不再遇到 IOException--Service Discovery Failed

所以,我建议你改成:

来自:

private static final UUID MY_UUID_SECURE =
UUID.fromString("ea87c0d0-afac-11de-8a39-0800200c9a66");
private static final UUID MY_UUID_INSECURE =
UUID.fromString("7ce255c0-200a-11e0-ac64-0800200c9a66");


private static final UUID MY_UUID_SECURE = 
UUID.fromString("00001105-0000-1000-8000-00805F9B34FB");
private static final UUID MY_UUID_INSECURE =
UUID.fromString("00001105-0000-1000-8000-00805F9B34FB");

Source这个答案。

关于android - 基于安卓蓝牙聊天示例的应用程序服务发现失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18977399/

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