title: Bluetooth startDiscovery() is not working on Android 10
body: <p>I´ve got stuck on a problem. I´ve tried some hints from Stack Overflow and other websites.</p>
  
  <p>I would like to program an app, which searches for all Bluetooth devices around and if the matching device has been found (MAC-Address as reference), the app will start a connection.</p>
  
  <p>Therefore I have programmed a test application to test the discovery-function. BUT there is unfortunately a big problem with starting the discovering process on my Android 10 device. I´ve got an older Samsung S3 Mini with Android 4.1.2 (SDK 16) on it, where my code is working fine. 
  On the Android 10 device <code>startDiscovery()</code> returns false, different to the Android 4 device, which returns true. On the android developer page they say, that false is the returning value, if an error has occurred. The BroadcastReceiver should work fine, because the app on the Android 9 mobile phone detects that a Bluetooth search has been started in the settings. It is only the <code>startDiscovery()</code> function, which the problem is all about (in my opinion).</p>
  
  <p>I am checking all permissions and the Bluetooth state before starting the discovering-process. But i think, it can´t be the written code, because it works perfectly on the older device.  Maybe I have something missing for newer devices. </p>
  
  <h2>Edit</h2>
  
  <p>As Thomas Morris explains below, in Android 10 you need the location turned on by the user. In Android 9 or lower, Thomas Morris answer is correct, because in all SDKs below 29, only the permission is needed and not the enabled location service. </p>
  
  <p>Is there a solution to avoid asking the user to turn on the location themselves?</p>
  
  <p>This is my MainActivity:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  final String TAG = "MainActivity";
  BluetoothAdapter bluetoothAdapter;
  int status = 0;     //0 = start discovering, 1 = cancel discovering
  
  public static final int REQUEST_ACCESS_COARSE_LOCATION = 1;
  public static final int REQUEST_ENABLE_BLUETOOTH = 11;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
      registerReceiver(receiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED));
      registerReceiver(receiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED));
  
      bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  
      checkBluetoothState();
  
      final Button test = findViewById(R.id.testbutton);
      test.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              if(status == 0) {
                  if(bluetoothAdapter != null &amp;&amp; bluetoothAdapter.isEnabled()) {
                      if (checkCoarseLocationPermission()) {
                          Boolean result = bluetoothAdapter.startDiscovery(); //start discovering and show result of function
                          Toast.makeText(getApplicationContext(), "Start discovery result: " + result, Toast.LENGTH_SHORT).show();
                          Log.d(TAG, "Start discovery: " + result);
                          test.setText("Stop");
                          status = 1;
                      }
                  }else{
                      checkBluetoothState();
                  }
              }else{
                  Log.d(TAG,"Stop");
                  status = 0;
                  bluetoothAdapter.cancelDiscovery();
                  test.setText("Start");
              }
          }
      });
  
      checkCoarseLocationPermission();
  }
  
  private boolean checkCoarseLocationPermission() {
      //checks all needed permissions
      if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
          ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_ACCESS_COARSE_LOCATION);
          return false;
      }else{
          return true;
      }
  
  }
  
  private void checkBluetoothState() {
      //checks if bluetooth is available and if it´s enabled or not
      if(bluetoothAdapter == null){
          Toast.makeText(getApplicationContext(), "Bluetooth not available", Toast.LENGTH_SHORT).show();
      }else{
          if(bluetoothAdapter.isEnabled()){
              if(bluetoothAdapter.isDiscovering()){
                  Toast.makeText(getApplicationContext(), "Device is discovering...", Toast.LENGTH_SHORT).show();
              }else{
                  Toast.makeText(getApplicationContext(), "Bluetooth is enabled", Toast.LENGTH_SHORT).show();
              }
          }else{
              Toast.makeText(getApplicationContext(), "You need to enabled bluetooth", Toast.LENGTH_SHORT).show();
              Intent enabledIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
              startActivityForResult(enabledIntent, REQUEST_ENABLE_BLUETOOTH);
          }
      }
  }
  
  // Create a BroadcastReceiver for ACTION_FOUND.
  private final BroadcastReceiver receiver = new BroadcastReceiver() {
      public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();
          if (BluetoothDevice.ACTION_FOUND.equals(action)) {
              // Discovery has found a device. Get the BluetoothDevice
              // object and its info from the Intent.
              BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
              String deviceName = device.getName();
              String deviceHardwareAddress = device.getAddress(); // MAC address
              Log.d(TAG,"Device found: " + deviceName + "|" + deviceHardwareAddress);
              Toast.makeText(getApplicationContext(), "FOUND: " + deviceName + "|" + deviceHardwareAddress, Toast.LENGTH_SHORT).show();
          }
  
          if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
              //report user
              Log.d(TAG,"Started");
              Toast.makeText(getApplicationContext(), "STARTED", Toast.LENGTH_SHORT).show();
          }
  
          if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
              //change button back to "Start"
              status = 0;
              final Button test = findViewById(R.id.testbutton);
              test.setText("Start");
              //report user
              Log.d(TAG,"Finished");
              Toast.makeText(getApplicationContext(), "FINISHED", Toast.LENGTH_SHORT).show();
          }
  
          if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)){
              final int extra = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,-1);
              if(extra == (BluetoothAdapter.STATE_ON)) {
                  if (bluetoothAdapter.isDiscovering()) {
                      bluetoothAdapter.cancelDiscovery();
                  }
                  Boolean b = bluetoothAdapter.startDiscovery();
                  Toast.makeText(getApplicationContext(), "Start discovery" + b, Toast.LENGTH_SHORT).show();
              }
          }
      }
  };
  
  
  @Override
  protected void onDestroy() {
      super.onDestroy();
      if (bluetoothAdapter.isDiscovering()){
          bluetoothAdapter.cancelDiscovery();
      }
  
      unregisterReceiver(receiver);
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data){
      super.onActivityResult(requestCode,resultCode,data);
  
      if(requestCode == REQUEST_ENABLE_BLUETOOTH){
          checkBluetoothState();
      }
  }
  
  @Override
  public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){
      super.onRequestPermissionsResult(requestCode,permissions,grantResults);
  
      switch (requestCode){
          case REQUEST_ACCESS_COARSE_LOCATION:
              if(grantResults.length &gt; 0 &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED){
                  Toast.makeText(getApplicationContext(),"Permission granted",Toast.LENGTH_SHORT).show();
              }else{
                  Toast.makeText(getApplicationContext(),"Permission denied",Toast.LENGTH_SHORT).show();
              }
      }
  }
  
  
  
  }
  </code></pre>
  
  <p>This is my Manifest:</p>
  
  <pre><code>&lt;application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme"&gt;
      &lt;activity android:name=".MainActivity"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
  
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  &lt;/application&gt;
  
  &lt;uses-permission android:name="android.permission.BLUETOOTH"/&gt;
  &lt;uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/&gt;
  </code></pre>
  
  <p></p>