Android的蓝牙发送消息的工作只有一次 [英] Android bluetooth send message working first time only
问题描述
我需要从树莓PI串发送消息到Android设备。我只得到消息第一次。之后,它不在所有工作。我使用的树莓PI PYTHON code。后第一次,这是无法搜索蓝牙设备,它正在运行的UUID。不过,如果我重新启动Android应用 - 再正常工作的第一次。我使用AcceptThread的建议这里。我没有用ConnectThread或ConnectedThread在我的应用程序,因为我只需要进入的消息。我是否需要关闭一些关于暂停或销毁。或者,做我需要做的是不要在网页中提到的东西吗?
I need to send string message from Raspberry PI to Android device. I am getting message first time only. After that it does not work at all. I am using PYTHON code in Raspberry PI. After first time, it is unable to search for a bluetooth device, which is running that UUID. However, if I restart Android application - again it works fine for first time. I am using AcceptThread as suggested here. I have not used ConnectThread or ConnectedThread in my application, as I need only incoming messages. Do I need to close something on Pause or Destroy. Or, do I need to do something which is not mentioned in that page?
下面是code:
private UUID MY_UUID = UUID.fromString("1e0ca4ea-299d-4335-93eb-27fcfe7fa848");
private AcceptThread acceptThread;
private class AcceptThread extends Thread {
private final BluetoothServerSocket mmServerSocket;
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 = bluetoothAdapter.listenUsingRfcommWithServiceRecord(TAG, 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 {
socket = mmServerSocket.accept();
} catch (IOException e) {
break;
}
// If a connection was accepted
if (socket != null) {
// Do work to manage the connection (in a separate thread)
manageConnectedSocket(socket);
try {
mmServerSocket.close();
}
catch(IOException e){
}
break;
}
}
}
/** Will cancel the listening socket, and cause the thread to finish */
public void cancel() {
try {
mmServerSocket.close();
} catch (IOException e) { }
}
}
我不使用PI信息,只需登录一个字符串。如上所述,它工作第一时间:
I am not using PI message, just logging a string. As mentioned above, it works first time:
private void manageConnectedSocket(BluetoothSocket socket) {
Log.i(TAG, "Hurray!! I am here");
//acceptThread.cancel();
}
下面是树莓PI蟒蛇code:
Here is the PYTHON code in Raspberry PI:
import sys
import bluetooth
uuid = "1e0ca4ea-299d-4335-93eb-27fcfe7fa848"
service_matches = bluetooth.find_service( uuid = uuid )
if len(service_matches) == 0:
print "couldn't find the BluetoothWithPi service"
sys.exit(0)
first_match = service_matches[0]
port = first_match["port"]
name = first_match["name"]
host = first_match["host"]
print "connected to \"%s\" on %s" % (name, host)
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((host, port))
sock.send("Hello from Raspberry PI!!")
sock.close()
它显示已连接到......的消息第一次。第二次它显示找不到......消息。
First time it displays 'Connected to ...' message. Second time it displays 'Couldn't find...' message.
推荐答案
我们只需要添加一些code到 manageConnectedSocket 块。基本上,我们需要取消acceptThread如果不为空,然后重新启动该服务:
We just need to add some code into manageConnectedSocket block. Basically we need to cancel the acceptThread if not null and restart the service:
private void manageConnectedSocket(BluetoothSocket socket) {
Log.i(TAG, "Hurray!! I am here");
//
if (acceptThread != null) {
acceptThread.cancel();
acceptThread = null;
}
//
if (acceptThread == null) {
acceptThread = new AcceptThread();
acceptThread.start();
}
}
这篇关于Android的蓝牙发送消息的工作只有一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!