我听说有些开发人员在指定SSID时无法连接到wifi:
https://groups.google.com/forum/?fromgroups=#!topic/android-developers/di4gMhC0ZTY
然而,其他开发人员通过SSID连接:
How do I connect to a specific Wi-Fi network in Android programmatically?
应该使用哪种方法?
我目前无法连接。
来源:
import java.util.List;
import android.app.Activity;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.content.Context;
public class Connect extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.connect);
String networkSSID = "ANDRE-PC_NETWORK";
String networkPass = "superman";
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\""; //ssid must be in quotes
conf.wepKeys[0] = "\"" + networkPass + "\"";
conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
conf.preSharedKey = "\""+ networkPass +"\"";
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
WifiManager wifiManager = (WifiManager)getSystemService(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;
}
}
}}
logcat的:
04-04 12:05:11.843: I/ActivityManager(479): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.wifi.connect/.Connect} from pid 2785
04-04 12:05:11.873: D/Finsky(1555): [159] ConsumptionAppServiceConn.getDataFromService: retrieved 20 documents from [2]
04-04 12:05:11.923: D/dalvikvm(479): GC_FOR_ALLOC freed 2477K, 23% free 17481K/22424K, paused 59ms, total 60ms
04-04 12:05:11.943: D/AndroidRuntime(2785): Shutting down VM
04-04 12:05:11.953: D/dalvikvm(2795): Late-enabling CheckJNI
04-04 12:05:11.953: D/dalvikvm(2785): GC_CONCURRENT freed 96K, 17% free 500K/600K, paused 0ms+0ms, total 3ms
04-04 12:05:11.953: D/jdwp(2785): Got wake-up signal, bailing out of select
04-04 12:05:11.953: D/dalvikvm(2785): Debugger has detached; object registry had 1 entries
04-04 12:05:11.953: I/ActivityManager(479): Start proc com.wifi.connect for activity com.wifi.connect/.Connect: pid=2795 uid=10110 gids={50110, 3003, 1028}
04-04 12:05:12.073: D/dalvikvm(785): GC_FOR_ALLOC freed 2349K, 25% free 18097K/24032K, paused 40ms, total 40ms
04-04 12:05:12.073: D/Finsky(1555): [1] ConsumptionAppDataCache.setConsumptionAppData: data didn't change for backend=[2], ignoring!
04-04 12:05:12.093: D/dalvikvm(479): GC_CONCURRENT freed 2628K, 24% free 17135K/22424K, paused 3ms+24ms, total 171ms
04-04 12:05:12.143: D/dalvikvm(2795): GC_FOR_ALLOC freed 74K, 3% free 7379K/7580K, paused 19ms, total 19ms
04-04 12:05:12.163: I/dalvikvm-heap(2795): Grow heap (frag case) to 10.841MB for 3686416-byte allocation
04-04 12:05:12.193: D/dalvikvm(2795): GC_FOR_ALLOC freed 1K, 2% free 10977K/11184K, paused 30ms, total 30ms
04-04 12:05:12.233: D/dalvikvm(2795): GC_CONCURRENT freed <1K, 2% free 10977K/11184K, paused 4ms+3ms, total 41ms
04-04 12:05:12.363: D/dalvikvm(785): GC_FOR_ALLOC freed 2349K, 25% free 18097K/24032K, paused 49ms, total 49ms
04-04 12:05:12.393: D/dalvikvm(479): GC_CONCURRENT freed 2439K, 24% free 17143K/22424K, paused 14ms+13ms, total 184ms
04-04 12:05:12.503: D/dalvikvm(785): GC_FOR_ALLOC freed 2349K, 25% free 18097K/24032K, paused 28ms, total 28ms
04-04 12:05:12.533: D/dalvikvm(479): GC_CONCURRENT freed 2423K, 24% free 17098K/22424K, paused 3ms+9ms, total 100ms
04-04 12:05:12.533: D/dalvikvm(479): WAIT_FOR_CONCURRENT_GC blocked 1ms
04-04 12:05:12.543: D/dalvikvm(2795): GC_FOR_ALLOC freed <1K, 2% free 10977K/11184K, paused 18ms, total 18ms
04-04 12:05:12.563: I/dalvikvm-heap(2795): Grow heap (frag case) to 17.067MB for 6529744-byte allocation
04-04 12:05:12.583: D/dalvikvm(2795): GC_FOR_ALLOC freed 0K, 2% free 17353K/17564K, paused 19ms, total 19ms
04-04 12:05:12.613: D/dalvikvm(2795): GC_CONCURRENT freed <1K, 2% free 17353K/17564K, paused 4ms+12ms, total 37ms
04-04 12:05:12.623: D/dalvikvm(2333): GC_CONCURRENT freed 174K, 6% free 7846K/8304K, paused 10ms+37ms, total 71ms
04-04 12:05:12.633: D/dalvikvm(785): GC_FOR_ALLOC freed 2349K, 25% free 18097K/24032K, paused 37ms, total 37ms
04-04 12:05:12.643: D/dalvikvm(479): GC_CONCURRENT freed 2359K, 24% free 17093K/22424K, paused 4ms+10ms, total 97ms
04-04 12:05:12.663: I/ActivityManager(479): Start proc com.android.keychain for broadcast com.android.keychain/.KeyChainBroadcastReceiver: pid=2819 uid=1000 gids={41000, 1015, 1028, 3002, 3001, 3003, 3007}
04-04 12:05:12.703: W/ContextImpl(2819): Calling a method in the system process without a qualified user: android.app.ContextImpl.startService:1352 android.content.ContextWrapper.startService:450 android.content.ContextWrapper.startService:450 com.android.keychain.KeyChainBroadcastReceiver.onReceive:12 android.app.ActivityThread.handleReceiver:2376
04-04 12:05:12.753: I/ActivityManager(479): Start proc com.google.android.googlequicksearchbox for broadcast com.google.android.googlequicksearchbox/com.google.android.apps.sidekick.GmmLocationProviderImpl$GmmPackageWatcher: pid=2837 uid=10055 gids={50055, 3003, 1028, 1007, 3002, 1015, 1006}
04-04 12:05:12.793: I/ActivityManager(479): No longer want com.google.android.gms (pid 2258): empty #17
04-04 12:05:12.823: I/ActivityManager(479): Start proc com.google.android.partnersetup for content provider com.google.android.partnersetup/.RlzAppProvider: pid=2854 uid=10025 gids={50025, 3003, 1028}
04-04 12:05:12.853: D/GStaticConfiguration(2837): #loadConfiguration
04-04 12:05:12.873: D/GStaticConfiguration(2837): #loadConfiguration from preference
04-04 12:05:12.883: E/wpa_supplicant(594): Line 0: Invalid WEP key length 8 - this network block will be ignored
04-04 12:05:12.913: D/dalvikvm(2837): GC_CONCURRENT freed 233K, 5% free 7558K/7916K, paused 4ms+2ms, total 45ms
04-04 12:05:12.913: D/dalvikvm(2837): WAIT_FOR_CONCURRENT_GC blocked 8ms
04-04 12:05:12.913: D/dalvikvm(2837): WAIT_FOR_CONCURRENT_GC blocked 8ms
04-04 12:05:12.913: D/dalvikvm(2837): WAIT_FOR_CONCURRENT_GC blocked 9ms
04-04 12:05:12.913: W/GAV2(2837): Thread[main,5,main]: Connection to service failed 1
04-04 12:05:12.913: W/ActivityManager(479): Unable to start service Intent { act=com.google.android.gms.analytics.service.START (has extras) } U=0: not found
04-04 12:05:12.923: I/wpa_supplicant(594): wlan0: CTRL-EVENT-DISCONNECTED bssid=20:4e:7f:08:61:e6 reason=3 locally_generated=1
04-04 12:05:12.933: D/GStaticConfiguration(2837): Configuration loaded [70]
04-04 12:05:12.933: W/GAV2(2837): Thread[main,5,main]: Need to call initialize() and be in fallback mode to start dispatch.
04-04 12:05:12.933: D/CommandListener(123): Clearing all IP addresses on wlan0
04-04 12:05:12.943: D/ConnectivityService(479): ConnectivityChange for WIFI: DISCONNECTED/DISCONNECTED
04-04 12:05:12.943: D/ConnectivityService(479): Attempting to switch to BLUETOOTH_TETHER
04-04 12:05:12.943: D/ConnectivityService(479): Attempting to switch to ETHERNET
04-04 12:05:12.963: D/NetUtils(479): android_net_utils_resetConnections in env=0x68afd138 clazz=0x3e400001 iface=wlan0 mask=0x3
04-04 12:05:12.963: D/ConnectivityService(479): resetConnections(wlan0, 3)
04-04 12:05:12.973: W/Smack/Packet(898): notify conn break (IOEx), close connection
04-04 12:05:12.973: W/Sidekick_LocationOracleImpl(2837): Best location was null
04-04 12:05:12.973: I/ActivityManager(479): Start proc jp.co.omronsoft.iwnnime.ml for broadcast jp.co.omronsoft.iwnnime.ml/.UninstallReceiver: pid=2883 uid=10062 gids={50062, 1028}
04-04 12:05:12.993: D/ConnectivityService(479): ConnectivityChange for WIFI: DISCONNECTED/DISCONNECTED
04-04 12:05:12.993: D/ConnectivityService(479): Attempting to switch to BLUETOOTH_TETHER
04-04 12:05:12.993: D/ConnectivityService(479): Attempting to switch to ETHERNET
04-04 12:05:12.993: D/NetUtils(479): android_net_utils_resetConnections in env=0x68afd138 clazz=0x40a00001 iface=wlan0 mask=0x3
04-04 12:05:12.993: D/ConnectivityService(479): resetConnections(wlan0, 3)
04-04 12:05:12.993: I/ActivityManager(479): No longer want com.google.android.calendar (pid 2122): empty #17
04-04 12:05:12.993: I/ActivityManager(479): No longer want com.android.providers.calendar (pid 2142): empty #17
04-04 12:05:13.083: I/ActivityManager(479): No longer want com.google.android.talk (pid 2217): empty #17
04-04 12:05:13.113: I/ActivityManager(479): Start proc com.google.android.gms for broadcast com.google.android.gms/.common.receiver.SystemBroadcastReceiver: pid=2900 uid=10021 gids={50021, 3003, 1007, 1028, 1015, 2001, 3006}
04-04 12:05:13.113: I/ActivityManager(479): Displayed com.wifi.connect/.Connect: +1s174ms
04-04 12:05:13.123: I/qtaguid(898): Failed write_ctrl(u 50) res=-1 errno=22
04-04 12:05:13.123: I/qtaguid(898): Untagging socket 50 failed errno=-22
04-04 12:05:13.123: W/NetworkManagementSocketTagger(898): untagSocket(50) failed with errno -22
04-04 12:05:13.123: D/ConnectivityService(479): handleInetConditionChange: no active default network - ignore
04-04 12:05:13.193: D/dalvikvm(2900): Trying to load lib /data/app-lib/com.google.android.gms-2/libAppDataSearch.so 0x426085b0
04-04 12:05:13.203: D/dalvikvm(2900): Added shared lib /data/app-lib/com.google.android.gms-2/libAppDataSearch.so 0x426085b0
04-04 12:05:13.203: D/dalvikvm(2900): No JNI_OnLoad found in /data/app-lib/com.google.android.gms-2/libAppDataSearch.so 0x426085b0, skipping init
04-04 12:05:13.213: D/Icing(2900): Init last flush num docs 0 last docstore size 0
04-04 12:05:13.213: D/Icing(2900): Docid map file has no data, nothing to scan
04-04 12:05:13.213: D/Icing(2900): File /data/data/com.google.android.gms/files/AppDataSearch/main/cur/ds.perdocdata contains 0 records of size 6
04-04 12:05:13.213: D/Icing(2900): Init docstore ok num docs 0 bytes 0
04-04 12:05:13.263: V/Icing(2900): Lite index crc computed in 0.101ms
04-04 12:05:13.263: V/Icing(2900): Lite index init ok in 4.994ms
04-04 12:05:13.263: V/Icing(2900): Warming lite-index took 0.007ms
04-04 12:05:13.263: V/Icing(2900): Warming lexicon took 0.005ms
04-04 12:05:13.263: V/Icing(2900): Warming display mappings took 0.004ms
04-04 12:05:13.263: D/Icing(2900): Init index ok num docs 0
04-04 12:05:13.263: D/Icing(2900): Init done
P.S。
我想要做的就是建立与特定wifi网络的连接。如果有人知道在哪里可以找到一些我可以测试的源代码的工作副本 - 它可能只解决我的所有问题(我能找到的唯一一个帖子是上面显示的那个,它不是工作)
答案 0 :(得分:0)
你可以试试这种方式
conf.SSID = "\" + networkSSID + \""; //ssid must be in quotes
conf.wepKeys[0] = "\" + networkPass + \"";