3

I am writing an Bluetooth game with blue tooth part based on android Bluetooth chat sample. And I have two phones to test. Here is the problem, when I connect one phone to the other, it sometimes shows the "Unable to connect device" bundle, but when I run the Bluetooth chat sample, it never shows this, so I think it is not the problem of device. Is there anyone who has studied the Bluetooth chat sample and has the same problem that would give me some help?

I try to print the exception, it is like "java.io.IOException: Service discovery failed". And here is the code that cause the exception.

private class ConnectThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;

    public ConnectThread(BluetoothDevice device) {
        mmDevice = device;
        BluetoothSocket tmp = null;

        // Get a BluetoothSocket for a connection with the
        // given BluetoothDevice
        try {
            tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) {
            Log.e(TAG, "create() failed", e);
        }
        mmSocket = tmp;
    }

    public void run() {
        Log.i(TAG, "BEGIN mConnectThread");
        setName("ConnectThread");

        // Always cancel discovery because it will slow down a connection
        mAdapter.cancelDiscovery();

        // Make a connection to the BluetoothSocket
        try {
            // This is a blocking call and will only return on a
            // successful connection or an exception
            mmSocket.connect();
        } catch (IOException e) {
            Log.e("error", e.toString());
            connectionFailed();
            // Close the socket
            try {
                mmSocket.close();
            } catch (IOException e2) {
                Log.e(TAG, "unable to close() socket during connection failure", e2);
            }
            // Start the service over to restart listening mode
            BluetoothChatService.this.start();
            return;
        }

        // Reset the ConnectThread because we're done
        synchronized (BluetoothChatService.this) {
            mConnectThread = null;
        }

        // Start the connected thread
        connected(mmSocket, mmDevice);
    }

    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) {
            Log.e(TAG, "close() of connect socket failed", e);
        }
    }
} 

The exact position is
mmSocket.connect();

You
  • 22,800
  • 3
  • 51
  • 64
flexwang
  • 625
  • 6
  • 16

1 Answers1

2

I know that you would find the answer weird, to be very honest I am not very good at UUIDs. But I was having the same issue and got it resolved using this.

Answer
I think you are using the UUIDs which are hard coded in the BluetoothChat app. When I changed those UUIDs to a Well Known UUID i.e.; "00001105-0000-1000-8000-00805F9B34FB" it solved my problem and I no longer get that IOException--Service Discovery Failed.

So, I recommend you to change :

From:

private static final UUID MY_UUID_SECURE =
    UUID.fromString("ea87c0d0-afac-11de-8a39-0800200c9a66");
private static final UUID MY_UUID_INSECURE =
    UUID.fromString("7ce255c0-200a-11e0-ac64-0800200c9a66");


To

private static final UUID MY_UUID_SECURE = 
    UUID.fromString("00001105-0000-1000-8000-00805F9B34FB");
private static final UUID MY_UUID_INSECURE = 
    UUID.fromString("00001105-0000-1000-8000-00805F9B34FB");

Source to this answer.

Community
  • 1
  • 1
Anas Azeem
  • 2,820
  • 3
  • 24
  • 37
  • @ flexwang Did you find the answer helpful, or you want some other assistance on this. If the answer is the solution to your problem, please accept it as an aswer. – Anas Azeem Oct 10 '13 at 10:59