Scan Wi-Fi network on Android

Asked

Viewed 29 times

1

I’m trying to scan all Wi-Fi networks that the phone normally takes. My intention of this scan is to select and connect the desired network. I even followed Andriod’s own tutorial Wifi-Scan. But the problem is that it does not point out the error, and does not show the result.

That is to say on the line wifimanager.getResults(); result 0, the line is located in the method sucesso().

public class MainActivity extends AppCompatActivity {

    public LinearLayout linearLayout =null;
    public WifiManager wifiManager =null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         linearLayout = findViewById(R.id.layoutLinear);

        wifiManager = ((WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE));

        BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Boolean sucess =  intent.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED,false);

                if(sucess)
                {
                    sucesso();
                }else{
                    old();
                }
            }
        };
        registerReceiver(broadcastReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        boolean success = wifiManager.startScan();
        if (!success) {
            // scan failure handling
            inSucess();
        }

    }
    public void sucesso()
    {
        List<ScanResult> results = wifiManager.getScanResults();
        for (ScanResult scanResult:results) {

            ImageView imageView = new ImageView(this);
            imageView.setImageResource(R.drawable.baseline);
            TextView textView = new TextView(this);
            String text = scanResult.SSID;
            textView.setText(text);

            linearLayout.addView(imageView);
            linearLayout.addView(textView);

        }
        TextView textView = new TextView(this);
        textView.setText("sucesso");
        linearLayout.addView(textView);
    }
    public void inSucess()
    {
        TextView textView = new TextView(this);
        textView.setText("falha");
        linearLayout.addView(textView);
    }
    public void old() {
        TextView textView = new TextView(this);
        textView.setText("old preview");
        linearLayout.addView(textView);
    }

}

Grateful to all.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.