安卓蓝牙发送消息只在第一次工作

2024-10-01 02:21:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要从Raspberry PI向Android设备发送字符串消息。我只是第一次收到信息。在那之后,它就不起作用了。我在Raspberry PI中使用PYTHON代码。第一次之后,它将无法搜索正在运行UUID的蓝牙设备。不过,如果我重启Android应用程序-同样,它第一次运行良好。我按照建议使用AcceptThreadhere。我没有在我的应用程序中使用ConnectThread或ConnectedThread,因为我只需要传入消息。我是否需要在暂停或销毁时关闭某些内容。或者,我需要做一些在那页没有提到的事情吗?在

这是密码:

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消息,只是记录一个字符串。如上所述,它第一次起作用:

^{pr2}$

下面是Raspberry PI中的PYTHON代码:

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()

第一次显示“连接到…”消息。第二次显示“找不到…”消息。在


Tags: theto消息uuidisservicepisocket
1条回答
网友
1楼 · 发布于 2024-10-01 02:21:03

我们只需要在manageConnectedSocket块中添加一些代码。基本上,我们需要取消acceptThread,如果不是null,然后重新启动服务:

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();
    }
}

相关问题 更多 >