4

I am developing an Android bluetooth application based on the BluetoothChat exemple. i am starting a bluetooth server and listening for a device(not a phone) to connect to mine app on an insecure rfcomm connection.

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

    public AcceptThread(boolean secure) {
        BluetoothServerSocket tmp = null;

        // Create a new listening server socket
        try {
            tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord(mServiceName, MY_UUID_INSECURE);
        } catch (Exception e) {
            Log.e(TAG, ".AcceptThread # listen() failed", e);
        } 
        mmServerSocket = tmp;
    }

    public void run() {
        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
                Log.d(TAG, ".AcceptThread.run # ...accepting server socket conn");

                socket = mmServerSocket.accept(); //FIXME: it blocks here

                Log.d(TAG, ".AcceptThread.run # server socket connection accepted");
            } catch (Exception e) {
                MMLog.e(TAG, ".run # accept() failed: "+e);
                connectionFailed();
                break;
            }

            // If a connection was accepted
            if (socket != null) {
                synchronized (BluetoothService.this) {
                    switch (mState) {
                    case STATE_LISTEN:
                    case STATE_CONNECTING:
                        // starting the thread where i will receive input
                        // streams from the other device
                        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;
                    }
                }
            }
        }

    }

    public void cancel() {
        try {
            if(mmServerSocket != null) {
                mmServerSocket.close();
            }
        } catch (IOException e) {
            Log.e(TAG, ".cancel # Could not close server socket: ", e);
        }
    }
}

I am using a HTC Desire S, android 2.3.5. The device gets paired, but i don't receive data, because the connection gets blocked in the '.accept()' method. It just keeps on waiting.

socket = mmServerSocket.accept(); //...and waiting

  • Why does it still wait, if the device is paired?
  • How can i establish the connection, because i also tried reflection, and still no result
  • Is there a problem with HTC's Bluetooth stack? Has anyone established the connection maybe using another android phone?
user975349
  • 173
  • 2
  • 6

2 Answers2

0

Chances are, this has to do with your other device (this is happening to me). Your Android already does its job which is listing for incoming connections. There are many reason why your special device won't initiate connections properly to your Android phone:

  • The device mysteriously switch to other Bluetooth profile e.g. HDP instead of SPP
  • The device somehow remembers a different Android phone in its memory (it was last connected to or something like that) and keeps trying to connect to that phone but not the one you are using right now.

I suppose your best chance is to query the manufacturer/ seller of the special device for detailed specifications and/or software/ driver to configure/ test it.

ericn
  • 12,476
  • 16
  • 84
  • 127
0

I think there is something wrong with your code, shouldn't it be socket = tmp.accept(); here is what I have to make a socket server connection:

BluetoothServerSocket serverSocket = null;
BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter();
try {
    serverSocket = bta.listenUsingInsecureRfcommWithServiceRecord("BluetoothChatInsecure", UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
} catch (IOException e) { e.printStackTrace(); }

while(!Thread.interrupted()) {
    try {
        socket = serverSocket.accept();
        if (socket != null) {
            Log.d("CONNECTED", "Connected bluetooth");

        /// do your stuff
JPM
  • 9,077
  • 13
  • 78
  • 137
  • thanks for the replay. Right now i don't have to the device to test it but i will get back to you on monday. – user975349 Feb 10 '12 at 18:17
  • Meanwhile, i added in the original question the entire code i tested on. – user975349 Feb 10 '12 at 18:28
  • I noticed this and maybe it has something to do with the special version of Motorola's android of 2.3.5 and auto-pairing which with createInsecureRfcommSocketToServiceRecord doesn't work so maybe listenUsingInsecureRfcommWithServiceRecord doesn't work too. – JPM Feb 10 '12 at 23:08
  • I still can't connect to the device. I will try reflection: `Method m = mAdapter.getClass().getMethod("listenUsingRfcommOn", new Class[] { int.class }); int port = getRfcommPort(tmp);tmp = (BluetoothServerSocket) m.invoke(mAdapter, port);` – user975349 Feb 13 '12 at 08:28