How to Programmatically Scan or Discover Android Bluetooth Devices

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.

android bluetooth discovery    android bluetooth scanning

(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

facebooktwittergoogle_plusredditpinterestlinkedinmailby feather

Comments

  1. Ivan says

    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…

    • says

      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.

      • Ivan says

        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 :D

Leave a Reply

Your email address will not be published. Required fields are marked *