在Android中选择WiFi接入点?

时间:2010-11-13 15:41:06

标签: android wifi

我正在开发一个从wifi列表中选择wifi接入点的Android应用程序。我使用了以下代码..

 WifiConfiguration wifiConfiguration = new WifiConfiguration(); 
wifiConfiguration.SSID = hotSpotSsid; 
wifiConfiguration.allowedKeyManagement.set(KeyMgmt.NONE); 
wifiConfiguration.BSSID = hotSpotBssid; 
wifiConfiguration.hiddenSSID = false; 

// wifiConfiguration.priority = 1;

// add this to the configured networks 
int inetId = wifiManager.addNetwork(wifiConfiguration); 
Log.i(TAG,"INetId :"+inetId); 
configs = wifiManager.getConfiguredNetworks(); 
Log.e(TAG,"After adding config :"+configs); 
if(inetId < 0) { 
    Log.i(TAG,"Unable to add network configuration for SSID: "+hotSpotSsid); 
    return; 
}else { 
    message="\t Successfully added to configured Networks"; 
    Log.i(TAG,message); 
} 

我的问题是我无法选择wifi接入点。每次都显示以前配置的wifi详细信息。

2 个答案:

答案 0 :(得分:2)

您似乎需要使用disableOthers = true

来呼叫WifiManager.enableNetwork
wifiManager.enableNetwork(inetId, true);

答案 1 :(得分:0)

您必须在manifest.xml中包含以下权限:

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