title: Android Bluetooth for discover devices, method Broadcast receiver is never called
body: <p>i have problem with this code everything works but when i press scan button nothing happens why is that, i'm sure this code is right i follow Bluetooth Android Developer tutorial, Upon debug, I found that the onReceive method of BroadcastReceiver is never called. However, Bluetooth is enabled. What am I missing?</p>
  <pre><code>package com.example.bluetoothmaccounter;
  
  
  import androidx.annotation.Nullable;
  import androidx.appcompat.app.AppCompatActivity;
  
  import android.annotation.SuppressLint;
  import android.bluetooth.BluetoothAdapter;
  import android.bluetooth.BluetoothDevice;
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.content.IntentFilter;
  import android.media.MediaPlayer;
  import android.os.Bundle;
  import android.os.Vibrator;
  import android.view.View;
  import android.widget.ArrayAdapter;
  import android.widget.Button;
  import android.widget.ListView;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import java.util.ArrayList;
  public class MainActivity extends AppCompatActivity {
      Button buttonON , buttonOFF, scanBt;
      ListView deviceList;
      ArrayList&lt;String&gt; arrayList = new ArrayList&lt;String&gt;();
      ArrayAdapter&lt;String&gt; arrayAdapter;
      BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
      int requestCodeForEnable = 1;
      Intent btEnablingIntent;
      Vibrator vibrator;
      MediaPlayer mediaPlayer;
      TextView textView2 ;
  
          @Override
          protected void onCreate (Bundle savedInstanceState){
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          buttonOFF = findViewById(R.id.bt_off);
          buttonON = findViewById(R.id.bt_on);
          scanBt = findViewById(R.id.bt_scan);
          deviceList = findViewById(R.id.device_list);
          btEnablingIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
          vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
          mediaPlayer = MediaPlayer.create(this, R.raw.alarm);
          textView2 = findViewById(R.id.textView2);
          // /////////////////// Enable bluetooth ///////////////////////
          buttonON.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  if (bluetoothAdapter == null) {
                      Toast.makeText(getApplicationContext(), &quot;this device not support bluetooth&quot;, Toast.LENGTH_SHORT).show();
                  } else {
                      if (!bluetoothAdapter.isEnabled()) {
                          startActivityForResult(btEnablingIntent, requestCodeForEnable);
                      }
                  }
  
              }
  
  
          });
  
          // /////////////////// disable bluetooth ///////////////////////
  
          buttonOFF.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  if (bluetoothAdapter.isEnabled()) {
                      bluetoothAdapter.disable();
                      Toast.makeText(getApplicationContext(), &quot;bluetooth disable&quot;, Toast.LENGTH_SHORT).show();
  
                  }
              }
          });
  
          // /////////////////// start scan ///////////////////////
  
          scanBt.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  arrayList.clear();
                  if (!bluetoothAdapter.isEnabled()) {
                      Toast.makeText(getApplicationContext(), &quot; bluetooth not Enabled&quot;, Toast.LENGTH_SHORT).show();
  
                  } else {
                      bluetoothAdapter.startDiscovery();
                      Toast.makeText(getApplicationContext(), &quot;discover started&quot;, Toast.LENGTH_SHORT).show();
                  }
              }
          });
          try {
              IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
              textView2.setText(&quot;befoore receiver&quot;);
              registerReceiver(receiver, intentFilter);
              arrayAdapter = new ArrayAdapter&lt;String&gt;(getApplicationContext(), android.R.layout.simple_list_item_1, arrayList);
              deviceList.setAdapter(arrayAdapter);
  
          } catch (Exception e) {
              textView2.setText(&quot;exeption&quot; + e.getMessage());
          }
      }
  
      // Create a BroadcastReceiver for ACTION_FOUND.
      private final BroadcastReceiver receiver = new BroadcastReceiver() {
          public void onReceive(Context context, Intent intent) {
              textView2.setText(&quot;on receive 1&quot;);
              String action = intent.getAction();
              if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                  textView2.setText(&quot;found&quot;);
                  BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                  arrayList.add(device.getName() + device.getAddress());
                  String size = &quot;size is &quot; + arrayList.size();
                  textView2.setText(size);
                  arrayAdapter.notifyDataSetChanged();
                  if (arrayList.size() &gt;= 1) {
                      Toast.makeText(getApplicationContext(), &quot;There are :&quot; + arrayList.size() + &quot;person here &quot;, Toast.LENGTH_LONG).show();
                      vibrator.vibrate(3 * 1000);
                      mediaPlayer.start();
  
  
                  }
  
  //
              }
          }
      };
  
  
          @Override
          protected void onDestroy () {
          super.onDestroy();
  
  
          // Don't forget to unregister the ACTION_FOUND receiver.
          textView2.setText(&quot;destrrooooooy&quot;);
          unregisterReceiver(receiver);
      }
      }
  </code></pre>