views:

61

answers:

1

I am using code from the Google Bluetooth Chat Example to set up a bluetooth listening server. I want the app to listen always for an incoming connection. When the app sees a connection request it will accept the connection, read in the string that the remote device will send, and then respond by sending a file.

The problem is that the app never accepts the connection. It blocks at socket = mmServerSocket.accept(); and never moves on. See the code below after the line if(D) System.out.println("Waiting to connect************"); To test it I start the activity that starts the thread and then attempt to connect with my laptop and send a file. When I do this the overall Android bluetooth manager sees the file and downloads it effectively bypassing my Android device. Is this the only way to test it? I cant figure out if its a testing or coding problem.

private class AcceptThread extends Thread {
    // The local server socket
    private final BluetoothServerSocket mmServerSocket;

    public AcceptThread() {
        BluetoothServerSocket tmp = null;

        // Create a new listening server socket
        try {
            tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
        } catch (IOException e) {
            Log.e(TAG, "listen() failed", e);
        }
        mmServerSocket = tmp;
    }

    public void run() {
        if (D) Log.d(TAG, "BEGIN mAcceptThread" + this);
        setName("AcceptThread");
        BluetoothSocket socket = null;

        // Listen to the server socket if we're not connected
        while (mState != STATE_CONNECTED) {
            try {
                // This is a blocking call and will only return on a
                // successful connection or an exception
                if(D) System.out.println("Waiting to connect************");
                socket = mmServerSocket.accept();
                if(D) System.out.println("We have accepted connection and are connected***************");
            } catch (IOException e) {
                Log.e(TAG, "accept() failed", e);
                break;
            }

            // If a connection was accepted
            if (socket != null) {
                synchronized (BluetoothServer.this) {
                    switch (mState) {
                    case STATE_LISTEN:
                    case STATE_CONNECTING:
                        // Situation normal. Start the connected thread.
                        connected(socket, socket.getRemoteDevice());
                        break;
                    case STATE_NONE:
                    case STATE_CONNECTED:
                        // Either not ready or already connected. Terminate new socket.
                        try {
                            socket.close();
                        } catch (IOException e) {
                            Log.e(TAG, "Could not close unwanted socket", e);
                        }
                        break;
                    }
                }
            }
        }
        if (D) Log.i(TAG, "END mAcceptThread");
    }
+1  A: 

Hi, check if your client side is looking for same UUID as MY_UUID. You may want to change the UUID to generic UUID for RFCOMM.

private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

cwrx
I got my hands on another phone and using the same application can connect from there. I would assume that this is because both applications have the same UID set. Does the UID serve as a port number in bluetooth?
Mike
hi Mike, if you are talking about the same application (android-BluetoothChat)? yes, the UUID in android bluetooth chat is programmed to connect to another android bluetooth chat wiht the same UUID, see MY_UUID.
cwrx
So the end game here is to connect to a linux machine. The guy working on the linux machine is saying hes using a port number. How can we get the linux machine and the android device working with the same UUID so that the linux messages are delivered to my app instead of the overall android system?
Mike
if we can assume that the linux machine is running generic SPP, then you should UUID for generic SPP (see the above post) in your android app, i.e listenUsingRfcommWithServiceRecord("YourServiceName", UUID.fromString("00001101-0000-1000-8000-00805F9B34FB") );
cwrx