如何连接到特定的wifi网络?

时间:2015-08-05 11:13:38

标签: java android eclipse wifi wifimanager

我有devlope项目列出了我的设备附近可用的所有wifi连接。现在,我想连接任何一个特定的网络连接。 那我该怎么办?我在listview中拥有所有网络。 如果有任何可用示例,请发表评论链接。

private void getWifiNetworksList() {
    IntentFilter filter = new IntentFilter();
    filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
    final WifiManager wifiManager = (WifiManager) getApplicationContext()
            .getSystemService(Context.WIFI_SERVICE);
    ;
    registerReceiver(new BroadcastReceiver() {

        @SuppressLint("UseValueOf")
        @Override
        public void onReceive(Context context, Intent intent) {
            sb = new StringBuilder();
            scanList = wifiManager.getScanResults();
            sb.append("\n  Number Of Wifi connections :" + " "
                    + scanList.size() + "\n\n");
            adapter = new ArrayAdapter<String>(DraginoActivity.this,
                    android.R.layout.simple_list_item_1, list);
            adapter.clear();
            for (int i = 0; i < scanList.size(); i++) {

                listWifiDevice.setAdapter(adapter);

                // list.add(new Integer(i + 1).toString() + ". ");
                list.add((new Integer(i + 1).toString() + ". ")
                        + (scanList.get(i)).toString().substring(5,
                                (scanList.get(i)).toString().indexOf(",")));
                // list.add("\n\n");

                listWifiDevice.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                listWifiDevice
                        .setOnItemClickListener(new OnItemClickListener() {

                            @Override
                            public void onItemClick(AdapterView<?> arg0,
                                    View arg1, int arg2, long arg3) {
                                TextView tv = (TextView) arg1;

                                // TODO Auto-generated method stub
                                Toast.makeText(DraginoActivity.this,
                                        "" + tv.getText().toString(), 1000)
                                        .show();

                            }
                        });
            }

            // listWifiDevice.setAdapter(adapter);
        }

    }, filter);
    wifiManager.startScan();

}

1 个答案:

答案 0 :(得分:2)

from collections import OrderedDict

print(list(OrderedDict.fromkeys(lst).keys()))

还有对话框

 private void setNetworksList() {
    networksList = (ListView) findViewById(R.id.networkList);
    networksList.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int position,
                                long id) {
            final int chosenPosition = position;
            WiFiItem wifiItem = new WiFiItem();
            wifiItem = (WiFiItem) parent.getItemAtPosition(position);
            networkSSID = wifiItem.getSSID();
            networkPass = "";
            showPasswordDialog(ConnectActivity.this);
            setPasswordListener(new PasswordTypedListener() {

                @Override
                public void onPasswordTyped() {
                    connectHelper = new ConnectHelper();
                    if(scanResult.get(chosenPosition).capabilities.contains("WEP")) {
                        connectHelper.setupConnection(networkSSID, networkPass, "WEP");
                    }
                    //jezeli siec WPA
                    else if(scanResult.get(chosenPosition).capabilities.contains("WPA")) {
                        connectHelper.setupConnection(networkSSID, networkPass, "WPA");
                    }

                    //inne przypadki (open network)
                    else {
                        connectHelper.setupConnection(networkSSID, networkPass, "ELSE");
                    }

                    wifiManager.addNetwork(connectHelper.getConfiguration());

                    List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
                    for( WifiConfiguration i : list ) {
                        if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
                            wifiManager.disconnect();
                            wifiManager.enableNetwork(i.networkId, true);
                            if(wifiManager.reconnect()) {
                                for(int j=0;j<listWifi.size();j++){
                                    listWifi.get(j).setConnected(false);
                                }
                                runnableCounter=0;
                                handler.postDelayed(ifConnected,1000);
                            }
                            break;
                        }
                    }
                }

            });

        }

    });
}

并且在onCreate中,但您可以跳过创建WifiItems并只存储SSID

 private void showPasswordDialog(Context context) {
    final EditText input = new EditText(context);
    input.setSingleLine();
    AlertDialog.Builder alert = new AlertDialog.Builder(context);
    alert.setTitle("PASSWORD");
    alert.setView(input);
    alert.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            networkPass = input.getText().toString();
            if(ConnectActivity.this.passwordListener != null)
                ConnectActivity.this.passwordListener.onPasswordTyped();
        }
    });
    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
        }
    });
    alert.show();

}