irobotxx
irobotxx

Reputation: 6063

Having troubles connecting to android device through Bluetooth UUID

Good day, I am trying to create an Bluetooth application and I am failing to connect to another Android device. The problem which seems to be occurring is in the outgoing connection createRfcommSocketToServiceRecord(UUID) - in which I believe the UUID is not correct for both devices. The documentation says:

if you are connecting to an Android peer then please generate your own unique UUID.

My question is - if the other android device does not have my application installed, how do I connect to it with the proper UUID? I have tried simply using the generic UUID 00001101-0000-1000-8000-00805F9B34FB, but that does not seem to resolve the issue.

Here is a part of the code sample:

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

private class ConnectThread extends Thread {
    BluetoothDevice mdevice;
    BluetoothSocket mclientSocket;

    //private String mSocketType;
    private Handler handler;
    private ProgressDialog dialog;

    public ConnectThread(BluetoothDevice device) {
        mdevice = device;
        BluetoothSocket temp = null;

        try {
            System.out.println("making connection to remote device");
            temp = mdevice.createRfcommSocketToServiceRecord(MY_UUID_SECURE);

        } catch (IOException e) {
            e.printStackTrace();
        }

        Log.i(TAG, "Bluetooth Socket" + temp.toString() + "obtained");
        mclientSocket = temp;
    }

    public synchronized void run() {
        try {
            Log.i(TAG, "starting to connect");
            mclientSocket.connect();
        } catch (IOException e) {
            Log.e(TAG, "connection Failed");

            try {
                mclientSocket.close();
            } catch (IOException e2) {
                ; // Do nothing.
            }
        }
    }

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

Note: I have not yet implemented the Bluetooth Server socket, but am instead trying to understand how you connect to another Android device that does not have my application installed. Help appreciated as always. Thank you.

Upvotes: 4

Views: 9158

Answers (1)

Dyonisos
Dyonisos

Reputation: 3539

From: http://developer.android.com/guide/topics/wireless/bluetooth.html

In order to create a connection between your application on two devices, you must implement both the server-side and client-side mechanisms, because one device must open a server socket and the other one must initiate the connection (using the server device's MAC address to initiate a connection). The server and client are considered connected to each other when they each have a connected BluetoothSocket on the same RFCOMM channel. At this point, each device can obtain input and output streams and data transfer can begin, which is discussed in the section about Managing a Connection. This section describes how to initiate the connection between two devices.

The server device and the client device each obtain the required BluetoothSocket in different ways. The server will receive it when an incoming connection is accepted. The client will receive it when it opens an RFCOMM channel to the server.

Upvotes: 4

Related Questions