6

UPDATE

I tried many codes, also from examples shown on the internet. each of them follows my approach. After many hours of testing, i came to the conclusion that on Android 6.0 there's no chance to achieve bluetooth discovery of unknown devices, we can only retrieve the bonded ones. I'm pretty sure there's something with this android version.

if someone knows how to fix this, i would really appreciate any help.


Original Post

My code is working fine, but no devices get found. i only receive DISCOVERY_STARTED and DISCOVERY_FINISHED, so no devices are found, but using system app these devices get found.

This is the code of my application, hope it can help.

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    bluetoothAdapter= BluetoothAdapter.getDefaultAdapter();

//other stuff...

    IntentFilter filter=new IntentFilter();
    filter.addAction(BluetoothDevice.ACTION_FOUND);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

    registerReceiver(myreceiver,filter);
}

final BroadcastReceiver myreceiver = new BroadcastReceiver(){

    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();

        Log.i("test","RECEIVED: "+ action);
        if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
        }
        else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
        }

        if(BluetoothDevice.ACTION_FOUND.equals(action))
        {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Log.i("test", device.getName() + "\n" + device.getAddress());
        }
    }};

public void scanDevices(View v){

        if (bluetoothAdapter.isEnabled()){

            bluetoothAdapter.startDiscovery();
        }
}

I already got permissions set:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Francesco Rizzi
  • 631
  • 6
  • 24
  • Possible duplicate of this question [here](http://stackoverflow.com/questions/32656510/register-broadcast-receiver-dynamically-does-not-work-bluetoothdevice-action-f) – Edward Alexander Jul 04 '16 at 17:53
  • @EdwardAlexander i don't think so, i read that question and my onReceive method gets called correctly. the problem is that i am not receiving ACTION_FOUND intents, just DISCOVERY_STARTED and DISCOVERY_FINISHED. No devices get found, but using system application i can retrieve the list of available devices. Why? – Francesco Rizzi Jul 04 '16 at 17:55
  • 1
    You should have specified clearly that you're using Marshmallow, that's a whole other story, here check this: [Bluetooth Discovery Marshmallow](http://stackoverflow.com/questions/33052811/since-marshmallow-update-bluetooth-discovery-using-bluetoothadapter-getdefaultad) – Edward Alexander Jul 04 '16 at 21:07
  • @EdwardAlexander Thank you very much for that! i'll check it out! However, i'm working with minsdk 9, how can i use this method only on this api level? I can't compile the line which uses these new methods. – Francesco Rizzi Jul 04 '16 at 21:22
  • Use `Build.VERSION.SDK_INT`, [check this](http://stackoverflow.com/questions/3423754/retrieving-android-api-version-programmatically) – Edward Alexander Jul 04 '16 at 21:26
  • @EdwardAlexander Thanks for the help, i'll check this out. As soon as i fix this i will post the answer :) – Francesco Rizzi Jul 04 '16 at 21:29

1 Answers1

12

Very simple solution:

1. Add FINE_LOCATION permission to manifest:

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

2. Request FINE_LOCATION permission at runtime:

//since i was working with appcompat, i used ActivityCompat method, but this method can be called easily from Activity subclassess.
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},MY_PERMISSION_REQUEST_CONSTANT);

3. Implement onRequestPermissionsResult method:

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {

switch (requestCode) {
    case MY_PERMISSION_REQUEST_CONSTANT: {
        // If request is cancelled, the result arrays are empty.
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            //permission granted!
        }
        return;
    }
}
}

All this because Marshmallows requires this permission in order to use bluetooth for discovery.

Since this permission belongs to the Dangerous group of permissions, simply declaring it in the manifest doesn't work, we need the user's explicit consent to use the position (even if we don't need the position actually).

Francesco Rizzi
  • 631
  • 6
  • 24