I can not discover the Bluetooth devices in this app

0

I'm a beginner in dev Android and I'm trying to search for devices not paired with the device, but the code I wrote does not even enter the BroadcastReceiver part. I read the documentation from Android Studio but I do not think I understood very well how BroadcastReceiver works. My question is: What am I missing in this program?

package teste.myapplication;
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.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    //Getting the Bluetooth Adapter of device
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    ArrayList<Device> devices = new ArrayList<>();

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


        //Intent Filter to discovery the Bluetooth devices
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mReceiver, filter);
        mBluetoothAdapter.startDiscovery();


    }


    public void turnOnBt(View v){
        //This code doesn't treat the case where the bluetooth isn't supported by device.
        if(!mBluetoothAdapter.isEnabled()){
            Intent enableBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBTIntent,1);
        }
    }

    public void turnOffBT(View v){
        //This code doesn't treat the case where the bluetooth isn't supported by device.
        if(mBluetoothAdapter.isEnabled()){
            mBluetoothAdapter.disable();
        }
    }


    // Create a BroadcastReceiver for ACTION_FOUND.
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                System.out.println("Entrei aqui");
                // 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
                Device p = new Device (deviceName,deviceHardwareAddress);
                devices.add(p);
                System.out.println(p.getName());
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // Don't forget to unregister the ACTION_FOUND receiver.
        unregisterReceiver(mReceiver);
    }

    //Discovering the devices
    public void discovery(View v){

        mBluetoothAdapter.startDiscovery();
        populateLisView();
    }


    //Populate the list view
    public void populateLisView(){

        String[] devices_name = new String[devices.size()];

        for(int i = 0; i < devices.size();i++){
            devices_name[i] = devices.get(i).getName();
        }

        //Setting up the adapter for the list view
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,devices_name);

        //Getting the list view on design
        ListView devices = (ListView) findViewById(R.id.devices);

        //Setting the adapter of the list view
        devices.setAdapter(adapter);
    }


}
    
asked by anonymous 20.12.2018 / 23:48

0 answers