Android Bluetooth and Bluetooth LE API Find nearby bluetooth devices

30% OFF - 9th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY9

Example

Declare a BluetoothAdapter first.

BluetoothAdapter mBluetoothAdapter;

Now create a BroadcastReceiver for ACTION_FOUND

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    //Device found                
    if (BluetoothDevice.ACTION_FOUND.equals(action)) 
    {
        // Get the BluetoothDevice object from the Intent
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        // Add the name and address to an array adapter to show in a list
        mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
    }
  }
};

Register the BroadcastReceiver

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);

Then start discovering the nearby bluetooth devices by calling startDiscovery

mBluetoothAdapter.startDiscovery(); 

Don't forget to unregister the BroadcastReceiver inside onDestroy

unregisterReceiver(mReceiver);


Got any Android Question?