Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android bluetooth UUID connecting APP to ANDROID

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

like image 897
frankelot Avatar asked Jan 30 '14 12:01

frankelot


Video Answer


1 Answers

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) { }
like image 60
Braunster Avatar answered Oct 26 '22 22:10

Braunster