If this is the layout of the app, and when the wifi is turned on, I want the list of networks available and then to connect to it by selecting any of them
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Search for available wifi networks
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);
}
}
}
}
create WifiConfiguration instance
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
for WEP network
conf.wepKeys[0] = "\"" + networkPass + "\"";
conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
For WPA network
conf.preSharedKey = "\""+ networkPass +"\"";
For Open network
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
add it to Android wifi manager
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(conf);
enable it
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;
}
}
回答2:
From the first page get only third example for scanning available wifi it worked for me and I modify it to choose specific BSSID
by using:
[bssid = wifiScanList.get(i).BSSID;
if (desiredMacAddress.equals(bssid)){ APPEND TO SHOWING LIST...}
]
it tests it and it worked without problems+ gave what i wanted. from the second page get how to join desired access point it try it then it work with out any problems but I can't sure it gives me this functionality
Get WiFi's SSID without connecting to it?
one final thing if you tested second on a real device it will change its setting which will lead to problems