如何在KITKAT上扫描可用的WI-FI?

时间:2018-11-28 15:25:42

标签: java android android-wifi android-4.4-kitkat stringindexoutofbounds

我正在创建一个应用程序,该应用程序扫描可用的WI-FI网络,然后在列表中显示它们。这是我的代码:

 public function courses(){
    return $this->hasMany('App\Course');
}

但是在Android版本Lollipop上运行时,一切正常。但是在Android版本KITKAT上它崩溃并显示(很遗憾,“应用程序名称”已停止。)。扫描WI-FI网络时会发生这种情况。我不知道我在做什么错。

这是我在Android KITKAT上的日志记录:

public class MainActivity extends ListActivity {

WifiManager mainWifiObj;
WifiScanReceiver wifiReciever;
ListView list;
String wifis[];

EditText pass;


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


    list=getListView();

    mainWifiObj = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    wifiReciever = new WifiScanReceiver();
    mainWifiObj.startScan();


    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


            String ssid = ((TextView) view).getText().toString();
            connectToWifi(ssid);
            Toast.makeText(MainActivity.this,"Wifi SSID : "+ssid,Toast.LENGTH_SHORT).show();

        }
    });
}

protected void onPause() {
    unregisterReceiver(wifiReciever);
    super.onPause();
}

protected void onResume() {
    registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    super.onResume();
}
class WifiScanReceiver extends BroadcastReceiver {
    @SuppressLint("UseValueOf")
    public void onReceive(Context c, Intent intent) {
        List<ScanResult> wifiScanList = mainWifiObj.getScanResults();
        wifis = new String[wifiScanList.size()];
        for(int i = 0; i < wifiScanList.size(); i++){
            wifis[i] = ((wifiScanList.get(i)).toString());
        }
        String filtered[] = new String[wifiScanList.size()];
        int counter = 0;
        for (String eachWifi : wifis) {
            String[] temp = eachWifi.split(",");

            filtered[counter] = temp[0].substring(5).trim();//+"\n" + temp[2].substring(12).trim()+"\n" +temp[3].substring(6).trim();//0->SSID, 2->Key Management 3-> Strength

            counter++;

        }
        list.setAdapter(new ArrayAdapter<String>(getApplicationContext(),R.layout.list_item,R.id.label, filtered));


    }
}

private void finallyConnect(String networkPass, String networkSSID) {
    WifiConfiguration wifiConfig = new WifiConfiguration();
    wifiConfig.SSID = String.format("\"%s\"", networkSSID);
    wifiConfig.preSharedKey = String.format("\"%s\"", networkPass);


    int netId = mainWifiObj.addNetwork(wifiConfig);
    mainWifiObj.disconnect();
    mainWifiObj.enableNetwork(netId, true);
    mainWifiObj.reconnect();

    WifiConfiguration conf = new WifiConfiguration();
    conf.SSID = "\"\"" + networkSSID + "\"\"";
    conf.preSharedKey = "\"" + networkPass + "\"";
    mainWifiObj.addNetwork(conf);
}

private void connectToWifi(final String wifiSSID) {
    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.connect);
    dialog.setTitle("Connect to Network");
    TextView textSSID = (TextView) dialog.findViewById(R.id.textSSID1);

    Button dialogButton = (Button) dialog.findViewById(R.id.okButton);
    pass = (EditText) dialog.findViewById(R.id.textPassword);
    textSSID.setText(wifiSSID);


    dialogButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String checkPassword = pass.getText().toString();
            finallyConnect(checkPassword, wifiSSID);
            dialog.dismiss();
        }
    });
    dialog.show();
}
}

0 个答案:

没有答案