Saturday, May 5, 2012

Android Example - Bluetooth Discover and List Devices and Services

This example does a bluetooth device discovery followed by a service discovery for every device it finds. The devices and services are listed as they are found.

There is one mystery that has me perplexed. Two identical sets of services are returned for each device. In the Android documentation it states that fetchUuidsWithSdp() will:
If there is an error in getting the SDP records or if the process takes a long time, ACTION_UUID intent is sent with the UUIDs that is currently present in the cache.
It is conceivable that the first set of responses is from cache and the second is from the remote device.

This example also demonstrates the use of a BroadcastReceiver, Intent Filters and a scrolling TextArea.

I have included the files that have content that is key to setting up the application.

You should be able to create a new Android project in eclipse and then copy the code included below into the appropriate file. Because fetchUUidsWithSDP() is called directly this example requires ICS or higher.

Below the code attachments you will find a screen capture of what it looks like running on my Acer A100.

If you have any suggestions or questions please post them as comments.

Download Zip Archive of Android Source Files
goes in <ProjectRoot>\src\com\example\discoverdevicesandservices\
goes in <ProjectRoot>\res\values\
goes in <ProjectRoot>\res\layout\
goes in <ProjectRoot>
Screen Capture from Acer A100


  1. Due to fetchUuidsWithSDP() this requires API Release 15
  2. As the SDK Emulator doesn’t emulate bluetooth this example will not run in the emulator.
  3. This example is built upon the work of others. I post it here not as an example of original work but rather as a complete working example for reference. If I didn't appropriately credit you for your work please let me know and I will add you.


Anonymous said...

Is it possible to get the full package in a zip file ?

digitalhack said...

re: Anonymous

Done, see above.

Kumar said...

Thanks, im using your code, when i receive of extre.uuid

Parcelable[] uuidExtra = intent.getParcelableArrayExtra("android.bleutooth.device.extra.UUID");

uuidExtra obj is always im not able to parse it. it gives nullpointerexception. what i m doing wrong?

Unknown said...

I'm seeing null for that on some devices and broadcasts, but not for others. I'd recommend just checking for null and skipping that item. That worked for me to prevent crashing enough to list other valid entries.

shilpa said...

I wanted to know if I implement a thread in one activity and once the thread get starts and if I change the activity, Whether the thread also goes into On Pause and or it gets killed….? If it happens any other way to handle this..? I am running Bluetooth connection in a thread and if I change the activity then the connection is getting lost.

digitalhack said...


I believe it gets paused but I don't have an Android device at the moment that I can test with.

You might try looking at making it a service.


Luke Fernandez said...

Does this example simply list the devices that have already been paired? Or does it show bluetooth devices that are in the vicinity of the android device?

digitalhack said...


It should list devices that are in the vicinity.


Unknown said...

This is really nice code. I am also use this code.
Thank You

Sooraj said...

error:bluetoothdevice cannot be resolved to a type

Unknown said...

for MAC address cheange it to your bluetooth MAC address. it works