我是蓝牙开发的新手。我已经开发了一个Android应用,即 rfcomm服务器:
public AcceptThread() {
// Use a temporary object that is later assigned to mmServerSocket,
// because mmServerSocket is final
BluetoothServerSocket tmp = null;
try {
// MY_UUID is the app's UUID string, also used by the client code
tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
} catch (IOException e) {
}
mmServerSocket = tmp;
}
public void run() {
BluetoothSocket socket = null;
// Keep listening until exception occurs or a socket is returned
while (true) {
try {
Log.i("BluetoothServer","Connecting...");
socket = mmServerSocket.accept();
} catch (IOException e) {
break;
}
Log.i("BluetoothServer","Connected");
// If a connection was accepted
if (socket != null) {
Log.i("BluetoothServer","Connected OK");
// Do work to manage the connection (in a separate thread)
manageConnectedSocket(socket);
try {
mmServerSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
和一个带有pybluez脚本的 rfcomm客户端:
service_matches = find_service( uuid = uuid, address = addr )
if len(service_matches) == 0:
print("couldn't find the SampleServer service =(")
sys.exit(0)
first_match = service_matches[0]
port = first_match["port"]
name = first_match["name"]
host = first_match["host"]
print("connecting to \"%s\" on %s" % (name, host))
# Create the client socket
sock=BluetoothSocket( RFCOMM )
sock.connect((host, port))
两个设备都已同步。 android应用程序正在运行并在socket = mmServerSocket.accept()中等待,执行脚本时结果始终相同:
bluetooth.btcommon.BluetoothError: (104, 'Connection reset by peer')
android应用不会继续执行,而是继续在同一mmServerSocket.accept()中等待
我用不同的UUID进行了测试,但是当前是:
private static final UUID MY_UUID = UUID.fromString("94f39d29-7d6d-437d-973b-fba39e49d4ee");
private static final String NAME = "BluetoothTest";
有人可以帮助我吗?我不知道。
谢谢。