質問編集履歴
4
mod source
test
CHANGED
File without changes
|
test
CHANGED
@@ -104,7 +104,7 @@
|
|
104
104
|
|
105
105
|
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
|
106
106
|
|
107
|
-
config.preSharedKey = "\"xxxxxxxxxxxx
|
107
|
+
config.preSharedKey = "\"xxxxxxxxxxxx\"";
|
108
108
|
|
109
109
|
|
110
110
|
|
@@ -124,7 +124,7 @@
|
|
124
124
|
|
125
125
|
//int networkId = 0; // 上記設定で取得できたものを使用
|
126
126
|
|
127
|
-
//String targetSSID = "
|
127
|
+
//String targetSSID = "xxxxx";
|
128
128
|
|
129
129
|
|
130
130
|
|
3
modify source
test
CHANGED
File without changes
|
test
CHANGED
@@ -61,6 +61,10 @@
|
|
61
61
|
MyConnectWifiManager.enableNetwork(i.networkId, true);
|
62
62
|
|
63
63
|
MyConnectWifiManager.reconnect();
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
```
|
64
68
|
|
65
69
|
|
66
70
|
|
@@ -177,5 +181,3 @@
|
|
177
181
|
}
|
178
182
|
|
179
183
|
```
|
180
|
-
|
181
|
-
```
|
2
add source
test
CHANGED
File without changes
|
test
CHANGED
@@ -62,4 +62,120 @@
|
|
62
62
|
|
63
63
|
MyConnectWifiManager.reconnect();
|
64
64
|
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
```ここに言語を入力
|
70
|
+
|
71
|
+
WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
String ssidWpaWpa2Psk = "xxxxx";
|
76
|
+
|
77
|
+
String targetSSID = "xxxxx";
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
WifiConfiguration config = new WifiConfiguration();
|
82
|
+
|
83
|
+
config.SSID = "\"" + ssidWpaWpa2Psk + "\"";
|
84
|
+
|
85
|
+
config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
|
86
|
+
|
87
|
+
config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
|
88
|
+
|
89
|
+
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
|
90
|
+
|
91
|
+
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
|
92
|
+
|
93
|
+
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
|
94
|
+
|
95
|
+
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
|
96
|
+
|
97
|
+
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
|
98
|
+
|
99
|
+
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
|
100
|
+
|
101
|
+
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
|
102
|
+
|
103
|
+
config.preSharedKey = "\"xxxxxxxxxxxxxxxxxxxxx\"";
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
//ネットワークIDを取得する 成功は0 失敗は-1
|
108
|
+
|
109
|
+
int networkId = wifiManager.addNetwork(config);
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
wifiManager.saveConfiguration();
|
114
|
+
|
115
|
+
wifiManager.updateNetwork(config);
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
//WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
|
120
|
+
|
121
|
+
//int networkId = 0; // 上記設定で取得できたものを使用
|
122
|
+
|
123
|
+
//String targetSSID = "uwxpq2";
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
// 例外処理 WiFi機能が無効の状態で呼び出されるとSSID検索の所でnullとなる
|
128
|
+
|
129
|
+
try {
|
130
|
+
|
131
|
+
// ssidの検索を開始
|
132
|
+
|
133
|
+
wifiManager.startScan();
|
134
|
+
|
135
|
+
for (ScanResult result : wifiManager.getScanResults()) {
|
136
|
+
|
137
|
+
// Android4.2以降よりダブルクォーテーションが付いてくるので除去
|
138
|
+
|
139
|
+
String resultSSID = result.SSID.replace("\"", "");
|
140
|
+
|
141
|
+
if (resultSSID.equals(targetSSID)) {
|
142
|
+
|
143
|
+
// ユーザが入力したIDと検索したIDが同一だった為、接続を開始する
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
if (networkId > 0) {
|
148
|
+
|
149
|
+
// 先に既存接続先を無効にしてから接続します
|
150
|
+
|
151
|
+
for (WifiConfiguration c0 : wifiManager.getConfiguredNetworks()) {
|
152
|
+
|
153
|
+
wifiManager.enableNetwork(c0.networkId, false);
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
wifiManager.enableNetwork(networkId, true);
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
break;
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
} catch (NullPointerException e) {
|
168
|
+
|
169
|
+
// 例外処理
|
170
|
+
|
171
|
+
Toast ts = Toast.makeText(login.this, "ERROR WiFi機能が無効の状態で呼び出されています。", Toast.LENGTH_SHORT);
|
172
|
+
|
173
|
+
ts.setGravity(Gravity.CENTER, 0, 0);
|
174
|
+
|
175
|
+
ts.show();
|
176
|
+
|
177
|
+
}
|
178
|
+
|
65
179
|
```
|
180
|
+
|
181
|
+
```
|
1
add source
test
CHANGED
File without changes
|
test
CHANGED
@@ -25,3 +25,41 @@
|
|
25
25
|
上記ではパスを入力していません。
|
26
26
|
|
27
27
|
idとPASSを入力してwifiに接続するプログラムを教えてください。
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
```ここに言語を入力
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
String netSSIDNotQuote = String.format("\"%s\"", netSSID);
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
WifiManager MyConnectWifiManager = (WifiManager) getSystemService (Context.WIFI_SERVICE);
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
WifiInfo info = MyConnectWifiManager.getConnectionInfo ();
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
MyConnectWifiManager.setWifiEnabled(true);
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
List<WifiConfiguration> list = MyConnectWifiManager.getConfiguredNetworks();
|
54
|
+
|
55
|
+
for( WifiConfiguration i : list ) {
|
56
|
+
|
57
|
+
if(i.SSID != null && i.SSID.equals(netSSIDNotQuote)) {
|
58
|
+
|
59
|
+
MyConnectWifiManager.disconnect();
|
60
|
+
|
61
|
+
MyConnectWifiManager.enableNetwork(i.networkId, true);
|
62
|
+
|
63
|
+
MyConnectWifiManager.reconnect();
|
64
|
+
|
65
|
+
```
|