How to scan WiFi networks on Android

1

I am having a series of problems that I hope you can solve me. I am very new to Android and I am looking for valid examples to scan WiFi networks with my mobile. In all the examples that I have loaded, the same problem arises, never find any network, when it is not true.

Right now I am using this sample code, which theoretically should work but does not. What can it be?

public class MainActivity extends AppCompatActivity {

private Element[] nets;
private WifiManager wifiManager;
private List<ScanResult> wifiList;
private TextView tv;

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            tv = (TextView) findViewById(R.id.textView);
            tv.setText("");

            detectWifi();
            Snackbar.make(view, "Scanning wifi ...", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}


//********************  HAM DO TIM WIFI  *********************
public void detectWifi() {
    this.wifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    this.wifiManager.startScan();
    this.wifiList = this.wifiManager.getScanResults();

    Toast.makeText(this, "So wifi tim thay: " + wifiList.size(), Toast.LENGTH_SHORT).show();

    this.nets = new Element[wifiList.size()];//khoi tao mang nets voi so phan tu = so phan tu cua mang wifiList.
    for (int i = 0; i < wifiList.size(); i++) {

        String item = wifiList.get(i).toString();
        //Toast.makeText(this, item, Toast.LENGTH_SHORT).show();

        String[] mang_item = item.split(",");//cat chuoi nhan duoc thanh roi luu vao mang_item.
        String item_ssid = mang_item[0]; //lay ra chuoi "SSID: name wifi"
        String ssid = item_ssid.split(": ")[1]; //cat chuoi "SSID: name wifi" => tao thanh mang 2 phan tu la SSID va name wifi|| String ssid la ten => lay o phan tu thu 1 cua mang

        nets[i] = new Element(ssid);
    }

    AdapterElements adapterElements = new AdapterElements(this);
    ListView netList = (ListView) findViewById(R.id.lv_ssid);
    netList.setAdapter(adapterElements);
    netList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            //Toast.makeText(getApplicationContext(), nets[i].getTitle() + "", Toast.LENGTH_SHORT).show();

            showdialow(nets[i].getTitle());
        }
    });

    //showdialow();
}

//********************  SHOW DIALOG  *********************
public void showdialow(final String ten_wifi) {
    dialog = new Dialog(MainActivity.this);
    dialog.setTitle("Nhập mật khẩu wifi");
    dialog.setCancelable(true);
    dialog.setContentView(R.layout.dialog_layout);

    Button btn_DongY = (Button) dialog.findViewById(R.id.btn_dongy);
    Button btn_Huy = (Button) dialog.findViewById(R.id.btn_huy);
    final EditText edt_Password = (EditText) dialog.findViewById(R.id.edt_password);

    CheckBox checkBox = (CheckBox) dialog.findViewById(R.id.cb_show);
    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (!isChecked){
                edt_Password.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
            else {
                edt_Password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }
        }
    });

    btn_DongY.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String matkhau = edt_Password.getText().toString();

            if (TextUtils.isEmpty(matkhau)) {
                edt_Password.setError("chưa nhập password");
            }
            else{
                Toast.makeText(MainActivity.this, "Tên wifi: " + ten_wifi + " Mat khau: " + matkhau, Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        }
    });
    btn_Huy.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    dialog.show();
}


class AdapterElements extends ArrayAdapter<Object> {
    Activity context;

    public AdapterElements(Activity context) {
        super(context, R.layout.items, nets);
        this.context = context;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();

        View item = inflater.inflate(R.layout.items, null);
        TextView tvSsid = (TextView) item.findViewById(R.id.tvSSID);

        tvSsid.setText(nets[position].getTitle());

        return item;
    }
}

Thank you very much everyone in advance.

    
asked by Lord Bergidum 24.07.2017 в 11:49
source

1 answer

0

You have to do the following, you declare these variables:

WifiManager wifi;
String wifis[];
WifiScanReceiver wifiReciever;

Now, you have to declare this class:

private class WifiScanReceiver extends BroadcastReceiver {
    public void onReceive(Context c, Intent intent) {
        List<ScanResult> wifiScanList = wifi.getScanResults();
        wifis = new String[wifiScanList.size()];

        for(int i = 0; i < wifiScanList.size(); i++){
            wifis[i] = ((wifiScanList.get(i)).toString());
        }
    }
}

Writing this now should recover the available Wi-Fi networks:

wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
wifiReciever = new WifiScanReceiver();

wifiReciever.onReceive(null, null);

wifi.startScan();

You have to make sure you have the following permissions in the manifest:

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

and (although I do not know if it is necessary 100%, I want to think that yes) have the wifi of your smartphone activated

    
answered by 24.07.2017 / 12:24
source