Mulgard
Mulgard

Reputation: 10589

Bluetooth: How to connect to any device

The Bluetooth tutorials i read all mentioned that i need to have the same UUID on both sides (Server and Client) to establish a connection between two devices. But what if i dont know the UUID of my Client and if i dont care?

Background information: I have over 1000 microcontrollers with bluetooth. Each microcontroller has a fix and unchangeable UUID. Smartphones should be able to send string messages to that micrcontrollers (single connection, one smartphone is controlling one microcontroller). It should not matter which Smartphone is controlling which microcontroller. So in fact i really dont care about the UUID of the Client.

So my Smartphone is the Server and is opening a listening thread for incoming Bluetooth connections but i have to put in a UUID here:

tempBluetoothServerSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);

But when i have thousand different UUID's and i really dont care about the UUID what should i put in there? Also the BluetoothSocket:

tempBluetoothSocket = this.bluetoothDevice.createRfcommSocketToServiceRecord(MY_UUID);

How to know which UUID?

So the core question is: How can i connect to any microcontroller?

Upvotes: 0

Views: 404

Answers (1)

laminatefish
laminatefish

Reputation: 5246

I've been using this:

// Unique UUID for this application
private static final UUID UUID_ANDROID_DEVICE =
        UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");
private static final UUID UUID_OTHER_DEVICE =
        UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

And it's uses:

    public AcceptThread(boolean isAndroid) {
        BluetoothServerSocket tmp = null;

        // Create a new listening server socket
        try {
            if(isAndroid)
                tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE, UUID_ANDROID_DEVICE);
            else
                tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE, UUID_OTHER_DEVICE);
        } catch (IOException e) { }
        mmServerSocket = tmp;
    }

    public ConnectThread(BluetoothDevice device) {
        mmDevice = device;
        BluetoothSocket tmp = null;

        // Get a BluetoothSocket for a connection with the
        // given BluetoothDevice
        try {
            if(BluetoothService.this.isAndroid)
                tmp = device.createRfcommSocketToServiceRecord(UUID_ANDROID_DEVICE);
            else
                tmp = device.createRfcommSocketToServiceRecord(UUID_OTHER_DEVICE);
        } catch (IOException e) { }
        mmSocket = tmp;
    }

Which allows my devices to connect to any bluetooth device I've tested with. For the sake of testing, it has only been varying bluetooth barcode scanners. Although I believe this is a generic RFCOMM UUID.

It hasn't failed me yet.

Upvotes: 1

Related Questions