Android bluetooth send message working first time

2019-09-20 06:37发布

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?

Here is that 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) { }
    }
}

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

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.

1条回答
劫难
2楼-- · 2019-09-20 07:25

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();
    }
}
查看更多
登录 后发表回答