Search code examples
javaandroidbluetoothuuidheadset

Bluetooth android server client with a headset


I'm trying to create a server on android that will listen on incoming connections ,such as a specific headset i have.

I've read many tutorials, posts in StackOverflow and the one from Android|Developer , and i don't seem to understand a few things .

1) UUID , is it a specific address for each Bluetooth device ? or is it a shared key that need to be in the server and the client in order to create a connection ? my guess is the latter cause there the MAC address as well ...

2) When i pair my Headset with my phone , does the headset saves the MAC\UUID of the last paired device ?

3) Does the Bluetooth chips even works in that way ? The phone connects immediately to a paired device as it turned on, So my guess is that it opens a Socket for each paired device and waits for it to turn on , is that true ?

4) Is it possible to accomplish what im trying ? Meaning creating a BluetoothServerSocket that will accept a connection from the head set ?

code example for the server side:

//This may b needs to be the UUID of the headset ? or special one ? or what ?
UUID myUUID = UUID.fromString("0000111e-0000-1000-8000-00805f9b34fb"); 
private final BluetoothServerSocket mServerSocket;
private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(Activity.APP_NAME, myUUID);
mServerScooket = tmp;
//Im hoping that this will block until the specific headset will be turned on 
socket = mServerSocket.accept();

Thanks in advance.


Solution

  • 1) yes UUID is specific to each device and in order to connect to a device you need to have its UUID. as shown in the Bluetooth chat example by Android SDK

    2) depends on the hardware for example chip sets like the HC-06 does saves the last paired device UUID while the HC-05 dose not ...

    3) The headsets that Ive tested (Samsung made) Acts as a server. so by initiating a BluetoothSocket with the UUID of the headphone u can connect to it.

    4) Yes it is possible to connect to a Bluetooth head set answered in the post : Using the Android RecognizerIntent with a bluetooth headset

    Special notes :

    Best way to listen to oncoming connections without any wakelocks that I found is by registering the BluetoothDevice.ACTION_ACL_CONNECTED Broadcast and check the name\mac\UUID of each incomming connection.

    Thanks to Hoan Nagayu for the help.