Through Android Bluetooth API, developers can access most of bluetooth functionalities and let the applications wirelessly connect to bluetooth devices. Before connecting to a device, an application must discover or scan available bluetooth devices, request pairing and connect to the device.
(apk + source code at the bottom of this post)
To discover a device, first get the bluetooth adapter by calling BluetoothAdapter.getDefaultAdapter().
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
To start discover, simply call the startDiscovery() from bluetooth adapter. This process is asynchronous so it will return immediately. To catch the discovery process, we can register a BroadcastReceiver with ACTION_FOUND, ACTION_DISCOVERY_STARTED, ACTION_DISCOVERY_STARTED. For each device found, the intent will carry extra field EXTRA_DEVICE containg the BluetoothDevice object.
IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothDevice.ACTION_FOUND); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); registerReceiver(mReceiver, filter); adapter.startDiscovery();
The receiver:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) { //discovery starts, we can show progress dialog or perform other tasks } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { //discovery finishes, dismis progress dialog } else if (BluetoothDevice.ACTION_FOUND.equals(action)) { //bluetooth device found BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); showToast("Found device " + device.getName()); } } };
And, don’t forget to unregister the receiver on Activity’s onDestroy method:
@Override public void onDestroy() { unregisterReceiver(mReceiver); super.onDestroy(); }
Download the sample APK and source code from github
[ad#ad-720×90]







How rotation of screen (or other configuration change) affect bluetooth discovery and scanning dialog? Would you save already found devices nad list them again on activity recreation or just start discovery again with empty list (which is not actually nice solution but more something like workaround). Thank you…
Hi Ivan,
I think it is good to save the already found devices. Then after the activity recreated again, save them (from onSavedInstanceState) on the list then start rescanning. We don’t have to use an empty list, just use the saved list and when a new device found, check if it already exists in the list or not. Also it is a good practice to allow user to cancel scanning using progress dialog with a cancel button.
Thanks for answer.. I did it already like that but I mess up something with fragment lifecycle and on discovery finished callback to dismiss dialog.. Sometimes I get crash cause dialog is not there and broadcast is initiating it’s dismission..btw I solved canceling BT discovery by dismissing dialog 😀
I think it is a good practice to check the progress dialog for its null value before dismissing it.
thank you, I’m new programming with wireless technology so this is what I need
🙂
You’re welcome..happy coding..;)
thank you very much sir.. this app was everything i wanted to know… thank you thank you…. you save me really ^_^
more power and god bless… btw can we friends in FB? here’s mine.. gersam13@yahoo.com (FB Account)
thanks again
Hi Thanx for your tutorial. It’s perfectly working in my case. Now i want to develop a Bluetooth chat type of app. I successfully developed but in server side code i found one issue. Server want able to connect to my Client using serversocket.accept() method. Do you have any idea to overcome this issue? any help!!!
This is awesome. I been working on something like this for long time
but was unable to make it work. This help me a lot. Thank you 🙂
Thanks for sharing the code. Here are my observations : this sample app working properly for Android O.S 5.0 L but it not showing any devices after scanning in 6.0 Android M. I am also looking into issue possibility can be we need to add some extra permission. Can you please update it if you get solution for Android M? Thanks.
hey Londa,
awesome app.
I need your help to develop a one click app (no interface) that automatically executes as follows on tap on an android device:
-scan all available bluetooth device
-temporary store available bluetooth devices
-auto initiates a connection on the available bluetooth devices (one by one with a 10sec time out to move on to the next available device )
-if the other device accepts the connection, pair and save it among paired devices.
thats all. let me know if you can help
Hi, really interested in this code, but on my Xiaomi doesn’t work, It can’t find any device. I can’t understand why. I tryed a lot of codes but this is the most complete and still not working for me. Could you help me? Thanks
NICE POST!
THANKS!
Truly a Legend (y)