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" />
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).