Service discovery failed with app based on android bluetooth chat sample

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


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 ie; "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.

链接地址: http://www.djcxy.com/p/82016.html

上一篇: 带有Android服务发现的蓝牙失败

下一篇: 基于Android蓝牙聊天示例的应用发现服务发现失败