9

I'm building an android application that keeps tracks of the Bluetooth connection on a device and triggers an alarm when they get out of range.

The Android documentation asks for a UUID in order to establish a connection.

An 'uuid' is a Universally Unique Identifier (UUID) standardized 128-bit format for a string ID used to uniquely identify information. It's used to uniquely identify your application's Bluetooth service.

 public ConnectThread(BluetoothDevice device) {
    // Use a temporary object that is later assigned to mmSocket,
    // because mmSocket is final
    BluetoothSocket tmp = null;
    mmDevice = device;

    // Get a BluetoothSocket to connect with the given BluetoothDevice
    try {
        // MY_UUID is the app's UUID string, also used by the server code
        tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e) { }
    mmSocket = tmp;
}

I am not installing an app on both devices, so I don't get to set my own UUID, I want to use android's instead... but I can't find this in the docs anywhere.

Maybe I'm not approaching the problem correctly. Any help will be appreciated. Thanks in advance

frankelot
  • 13,666
  • 16
  • 54
  • 89

1 Answers1

27

You can get the UUID from the BluetoothDevice

    mmDevice = device;

    // Get a BluetoothSocket to connect with the given BluetoothDevice. This code below show how to do it and handle the case that the UUID from the device is not found and trying a default UUID.

    // Default UUID
    private UUID DEFAULT_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 

    try {
        // Use the UUID of the device that discovered // TODO Maybe need extra device object
        if (mmDevice != null)
        {
            Log.i(TAG, "Device Name: " + mmDevice.getName());
            Log.i(TAG, "Device UUID: " + mmDevice.getUuids()[0].getUuid());
            tmp = device.createRfcommSocketToServiceRecord(mmDevice.getUuids()[0].getUuid());

        }
        else Log.d(TAG, "Device is null.");
    }
    catch (NullPointerException e)
    {
        Log.d(TAG, " UUID from device is null, Using Default UUID, Device name: " + device.getName());
        try {
            tmp = device.createRfcommSocketToServiceRecord(DEFAULT_UUID);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
    catch (IOException e) { }
Braunster
  • 466
  • 4
  • 6
  • YOU! ARE! AWESOME! :D Thaaaaanks man, can't tell you how many hours I've wasted trying to figure this out.. I really appreciate it! :) – frankelot Jan 30 '14 at 13:38
  • 1
    With pleasure ive wasted to meny hours myself on this. You can check this https://github.com/itzikBraun/ArduinoCar its an app controlling and arduino via bluetooth there is two thread handling the connection maybe it would help you even more. – Braunster Jan 30 '14 at 13:58
  • It's so weird that android won't provide a method to pair two devices together!. (They only added one now, on API 19. When it seems something so basic and fundamental) – frankelot Feb 02 '14 at 20:15
  • 2
    Unfortunately, this only worked on SOME devices. :/ Not a definite solution. – frankelot Feb 04 '14 at 20:24
  • What was the problem maybe i can help. – Braunster Feb 04 '14 at 22:11
  • Well, your solution worked fine on some devices, but I got no answer whatsoever when tested against different android versions and phone models. I mean, in some cases, the devices just won't react to my connection attempt :( – frankelot Feb 05 '14 at 11:12
  • can you tell me which api did you use that caused you the problem? or to what device did you tried to connect? – Braunster Jun 04 '14 at 05:19
  • I have same problem I want to connect to android 5.1.1 device but I was unable.can you please give me solution to connect any bluetooth device – Siluni Upeksha Mar 28 '16 at 10:42
  • 2
    Checking for `null` explicitly is generally preferable to catching a `NullPointerException`. – Greg Brown Feb 23 '17 at 15:08
  • in my case getuuids() returns null and device does not connect with default uuid. is there any work around please? – Manpreet Singh Dhillon Mar 15 '17 at 21:30
  • 1
    where did you get this "00001101-0000-1000-8000-00805F9B34FB" string??????? – luke cross Sep 12 '20 at 17:38