1

I've copied the following code from Android documentation but doesn't discovery any device. Anyone know why?

public class MainActivity extends AppCompatActivity {

private BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
private TextView tvBoundedDevices;
private TextView tvDiscoveredDevices;

private IntentFilter intentFilter = new IntentFilter();
private BroadcastReceiver broadcastReceiver;
private ProgressDialog progressDialog;
private ArrayList<BluetoothDevice> bluetoothDevicesFound = new ArrayList<>();
private Set<BluetoothDevice> bluetoothDevicesBounded;

@Override
protected void onDestroy() {
    unregisterReceiver(broadcastReceiver);
    bluetoothAdapter.cancelDiscovery();
    super.onDestroy();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tvBoundedDevices = findViewById(R.id.tvBoundedDevices);
    tvDiscoveredDevices = findViewById(R.id.tvDiscoveredDevices);

    intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
    intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action){
                case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
                    progressDialog = ProgressDialog.show(MainActivity.this,"Attendere","Scan in corso");
                    break;
                case BluetoothDevice.ACTION_FOUND:
                    bluetoothDevicesFound.add((BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE));
                    break;
                case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
                    if(progressDialog.isShowing())
                        progressDialog.dismiss();
                    break;
                default:
                    Toast.makeText(context, "Action=" + action, Toast.LENGTH_LONG).show();
            }
        }
    };
    registerReceiver(broadcastReceiver,intentFilter);
}

public void turnBluetoothOn(View view){
    if(!bluetoothAdapter.isEnabled()){
        bluetoothAdapter.enable();
        Toast.makeText(this,"Bluetooth attivato",Toast.LENGTH_SHORT).show();
    }
}

public void turnBluetoothOff(View view){
    if(bluetoothAdapter.isEnabled()){
        bluetoothAdapter.disable();
        Toast.makeText(this,"Bluetooth disattivato",Toast.LENGTH_SHORT).show();
    }
}

public void scanDevices(View view){
    turnBluetoothOn(null);
    if(bluetoothAdapter.isDiscovering())
        bluetoothAdapter.cancelDiscovery();
    Toast.makeText(this, "Scansione " + (bluetoothAdapter.startDiscovery()?"":"non") + " avviata.", Toast.LENGTH_SHORT).show();
    printDevices();
}

String toStamp = "";
private void printDevices(){
    bluetoothDevicesBounded = bluetoothAdapter.getBondedDevices();
    if(!bluetoothDevicesBounded.isEmpty()){
        toStamp = "Dispositivi associati:\n";
        for(BluetoothDevice b : bluetoothDevicesBounded){
            toStamp += b.getName() + " | " + b.getAddress() + "\n";
        }
        tvBoundedDevices.setText(toStamp + "");
    }
    if(!bluetoothDevicesFound.isEmpty()){
        toStamp = "Dispositivi trovati:\n";
        for(BluetoothDevice b : bluetoothDevicesFound){
            toStamp += b.getName() + " | " + b.getAddress() + "\n";
        }
        tvDiscoveredDevices.setText(toStamp + "");
    }
}

Pastebin: https://pastebin.com/CnEBAtwt

What works: - List of bounded devices - Broadcast for "start discovery" and "end discovery"

What doesn't works: - Discovery nearby devices

Thank you

  • After some times I have the answer and maybe can be helpful for someone. To discover devices you must enable bluetooth or wifi ^.^ – Daniele Lupo Jan 17 '19 at 15:46

1 Answers1

0

You need to add one of the following permission to found devices:

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

or

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

If you are working on API 23 and higher you must ensure this permission is granted because it is a dangerous level permission.

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app

See this guide to request permission.

Diego D.
  • 398
  • 3
  • 8