如何以编程方式在Android中搜索和连接到特定的wifi网络

时间:2016-03-19 07:01:05

标签: android wifi

如果这是应用程序的布局,并且当wifi打开时,我想要可用的网络列表,然后通过选择其中任何一个连接到它

enter image description here

2 个答案:

答案 0 :(得分:10)

搜索可用的wifi网络

    public class MainActivity extends Activity {


    WifiManager mainWifi;
    WifiReceiver receiverWifi;

    StringBuilder sb = new StringBuilder();


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


        mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

        receiverWifi = new WifiReceiver();
        registerReceiver(receiverWifi, new IntentFilter(
                WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        if(mainWifi.isWifiEnabled()==false)
        {
            mainWifi.setWifiEnabled(true);
        }


        doInback();
    }

    public void doInback()
    {
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run()
            {
                // TODO Auto-generated method stub
                mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

                receiverWifi = new WifiReceiver();
                registerReceiver(receiverWifi, new IntentFilter(
                        WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
                mainWifi.startScan();
                doInback();
            }
        }, 1000);

    }

    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, 0, 0, "Refresh");
        return super.onCreateOptionsMenu(menu);}
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        mainWifi.startScan();

        return super.onMenuItemSelected(featureId, item);}


    @Override
    protected void onPause()
    {
        unregisterReceiver(receiverWifi);
        super.onPause();
    }

    @Override
    protected void onResume()
    {
        registerReceiver(receiverWifi, new IntentFilter(
                WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        super.onResume();
    }

    class WifiReceiver extends BroadcastReceiver
    {
        public void onReceive(Context c, Intent intent)
        {

            ArrayList<String> connections=new ArrayList<String>();
            ArrayList<Float> Signal_Strenth= new ArrayList<Float>();

            sb = new StringBuilder();
            List<ScanResult> wifiList;
            wifiList = mainWifi.getScanResults();
            for(int i = 0; i < wifiList.size(); i++)
            {

                connections.add(wifiList.get(i).SSID);
            }


        }
    }



}

创建WifiConfiguration实例

 WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";

适用于WEP网络

    conf.wepKeys[0] = "\"" + networkPass + "\""; 
conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); 

适用于WPA网络

conf.preSharedKey = "\""+ networkPass +"\"";

适用于开放网络

conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

将其添加到Android wifi管理器

    WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); 
wifiManager.addNetwork(conf);

启用

    List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
    if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
         wifiManager.disconnect();
         wifiManager.enableNetwork(i.networkId, true);
         wifiManager.reconnect();               

         break;
    }           
 }

答案 1 :(得分:0)

从第一页获取第三个示例,扫描可用的wifi,它对我有用,我修改它以通过使用选择特定的BSSID

[bssid = wifiScanList.get(i).BSSID;
    if (desiredMacAddress.equals(bssid)){ APPEND TO SHOWING LIST...}
]

它测试它并且它没有问题地工作+给了我想要的东西。 从第二页获取如何加入所需的访问点它尝试它然后它解决任何问题,但我不能确定它给了我这个功能

Get WiFi's SSID without connecting to it?

最后一件事,如果你在真实设备上测试第二,它将改变其设置,这将导致问题