3

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.

Troy Alford
  • 26,660
  • 10
  • 64
  • 82
irobotxx
  • 5,963
  • 11
  • 62
  • 91
  • how do you connect with the generic UUID? Can you post a code sample? – Dyonisos Sep 05 '11 at 10:48
  • @Dyonisos, i have added the code sample and the UUID i use. – irobotxx Sep 05 '11 at 11:18
  • Maybe you try to generate an own UUID using: `http://www.uuidgenerator.com` instead of using the generic. – Dyonisos Sep 05 '11 at 11:27
  • @Dyonisos, ok i would try that, but how will the other android device know the uuid if it does not have my application installed. I am guessing that not all android devices support the SDP protocol and maybe the device am testing is included. – irobotxx Sep 05 '11 at 11:33
  • In order to establish a connection, one of the android devices must run a bloototh server listener which provides a service. The other device then can connect to this server listener using its UUID and the service name. – Dyonisos Sep 05 '11 at 11:52
  • In order to create/generate your own UUID in Android, [as mentioned from this link here](http://developer.android.com/reference/java/util/UUID.html), it said to use the static function [randomUUID()](http://developer.android.com/reference/java/util/UUID.html#randomUUID()). It is available since Android API Level 1. – tom_mai78101 Feb 01 '13 at 13:36

1 Answers1

4

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.

Dyonisos
  • 3,541
  • 2
  • 22
  • 25