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);