質問編集履歴

2

プログラムの詳細記載

2017/08/20 02:38

投稿

kumakumatan
kumakumatan

スコア213

test CHANGED
File without changes
test CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
 
8
8
 
9
- ・ssid:SSID_NAME
9
+ ・ssid:SSID
10
10
 
11
11
  ・password:password
12
12
 
@@ -82,6 +82,12 @@
82
82
 
83
83
  import android.annotation.SuppressLint;
84
84
 
85
+ import android.content.Context;
86
+
87
+ import android.net.wifi.ScanResult;
88
+
89
+ import android.net.wifi.WifiConfiguration;
90
+
85
91
  import android.net.wifi.WifiManager;
86
92
 
87
93
  import android.os.Bundle;
@@ -102,7 +108,7 @@
102
108
 
103
109
  private ToggleButton toggle;
104
110
 
105
- private WifiManager wifi;
111
+ private WifiManager Wifi;
106
112
 
107
113
 
108
114
 
@@ -120,53 +126,61 @@
120
126
 
121
127
  // WifiManagerのインスタンスを取得する
122
128
 
123
- wifi = (WifiManager) getSystemService(WIFI_SERVICE);
124
-
125
-
126
-
127
- //WEPのSSID設定ここで登録ここから
128
-
129
-
130
-
131
- WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
132
-
133
- String ssid = "SSID_NAME";
134
-
135
- WifiConfiguration config = new WifiConfiguration();
136
-
137
- config.SSID = "\"" + ssid + "\"";
138
-
139
- config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
140
-
141
- config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
142
-
143
- config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
144
-
145
- config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
146
-
147
- config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
148
-
149
- config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
150
-
151
- config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
152
-
153
- config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
154
-
155
- config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
156
-
157
- config.wepKeys[0] = "\"password\"";
158
-
159
- config.wepTxKeyIndex = 0;
160
-
161
- int networkId = wifiManager.addNetwork(config); // 失敗した場合は-1となります
162
-
163
- wifiManager.saveConfiguration();
164
-
165
- wifiManager.updateNetwork(config);
166
-
167
-
168
-
169
- //WEPのSSID設定をここで登録ここまで
129
+ Wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
130
+
131
+
132
+
133
+ //WiFi機能ONにする
134
+
135
+ WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
136
+
137
+ if (wifiManager.isWifiEnabled() == false) {
138
+
139
+ wifiManager.setWifiEnabled(true);
140
+
141
+ }
142
+
143
+
144
+
145
+
146
+
147
+ //WEPSSID設定
148
+
149
+ String ssid = "SSID";
150
+
151
+ WifiConfiguration config = new WifiConfiguration();
152
+
153
+ config.SSID = "\"" + ssid + "\"";
154
+
155
+ config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
156
+
157
+ config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
158
+
159
+ config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
160
+
161
+ config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
162
+
163
+ config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
164
+
165
+ config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
166
+
167
+ config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
168
+
169
+ config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
170
+
171
+ config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
172
+
173
+ config.wepKeys[0] = "\"password\"";
174
+
175
+ config.wepTxKeyIndex = 0;
176
+
177
+ int networkId = wifiManager.addNetwork(config); // 失敗した場合は-1となります
178
+
179
+ wifiManager.saveConfiguration();
180
+
181
+ wifiManager.updateNetwork(config);
182
+
183
+
170
184
 
171
185
 
172
186
 
@@ -182,7 +196,7 @@
182
196
 
183
197
  // トグルボタンの状態初期化
184
198
 
185
- if(wifi.isWifiEnabled()){
199
+ if(Wifi.isWifiEnabled()){
186
200
 
187
201
 
188
202
 
@@ -196,7 +210,7 @@
196
210
 
197
211
 
198
212
 
199
- // 現在のWifiがONだった場合、トグルボタンをOFFに切り替える
213
+ // 現在のWifiがOffだった場合、トグルボタンをOFFに切り替える
200
214
 
201
215
  toggle.setChecked(false);
202
216
 
@@ -212,150 +226,138 @@
212
226
 
213
227
  public void onClick(View v) {
214
228
 
215
-
216
-
217
- //接続開始ここから
229
+
218
-
219
- WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
230
+
220
-
221
- int networkId = 0; // 上記設定で取得できたものを使用
222
-
223
- String targetSSID = "SSID_NAME";
224
-
225
-
226
-
227
- //接続開始ここまで
228
-
229
-
230
-
231
- if(wifi.isWifiEnabled()){
231
+ if(Wifi.isWifiEnabled()){
232
-
233
-
234
232
 
235
233
  // 現在のWifiがONだった場合、OFFに切り替える
236
234
 
237
-
238
-
239
- try {
240
-
241
- // ssidの検索を開始
242
-
243
- wifiManager.startScan();
244
-
245
- for (ScanResult result : wifiManager.getScanResults()) {
246
-
247
- // Android4.2以降よりダブルクォーテーションが付いてくるので除去
248
-
249
- String resultSSID = result.SSID.replace("\"", "");
250
-
251
- if (resultSSID.equals(targetSSID)) {
252
-
253
- // 接続を行う
254
-
255
- if (networkId > 0) {
256
-
257
- // 先に既存接続先を無効にしてから切断します
258
-
259
- for (WifiConfiguration c0 : wifiManager.getConfiguredNetworks()) {
260
-
261
- wifiManager.enableNetwork(c0.networkId, false);
262
-
263
- }
264
-
265
- wifiManager.enableNetwork(networkId, false);
266
-
267
- //ボタンのオフ
268
-
269
- wifi.setWifiEnabled(false);
235
+ Wifi.setWifiEnabled(false);
270
-
271
- }
272
-
273
- break;
274
-
275
- }
276
-
277
- }
278
-
279
- } catch (NullPointerException e) {
280
-
281
- // 適切な例外処理をしてください。
282
-
283
- }
284
-
285
-
286
-
287
-
288
-
289
- //wifi.setWifiEnabled(false);
290
236
 
291
237
 
292
238
 
293
239
  }else{
294
240
 
295
-
296
-
297
241
  // 現在のWifiがOFFだった場合、ONに切り替える
298
242
 
299
-
300
-
301
- try {
302
-
303
- // ssidの検索開始
304
-
305
- wifiManager.startScan();
306
-
307
- for (ScanResult result : wifiManager.getScanResults()) {
308
-
309
- // Android4.2以降よりダブルクォーテーションが付いてくるので除去
310
-
311
- String resultSSID = result.SSID.replace("\"", "");
312
-
313
- if (resultSSID.equals(targetSSID)) {
314
-
315
- // 接続を行う
316
-
317
- if (networkId > 0) {
318
-
319
- // 先に既存接続先を無効にしてから接続します
320
-
321
- for (WifiConfiguration c0 : wifiManager.getConfiguredNetworks()) {
322
-
323
- wifiManager.enableNetwork(c0.networkId, false);
324
-
325
- }
326
-
327
- wifiManager.enableNetwork(networkId, true);
328
-
329
- //ボタンのオン
330
-
331
- wifi.setWifiEnabled(true);
332
-
333
- }
334
-
335
- break;
336
-
337
- }
338
-
339
- }
340
-
341
- } catch (NullPointerException e) {
342
-
343
- // 適切な例外処理をしてください。
344
-
345
- }
346
-
347
-
348
-
349
- //wifi.setWifiEnabled(true);
243
+ // Wifi.setWifiEnabled(true);
244
+
245
+
246
+
247
+ //WiFi機能ONにする
248
+
249
+ WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
250
+
251
+ if (!wifiManager.isWifiEnabled()) {
252
+
253
+ wifiManager.setWifiEnabled(true);
254
+
255
+ }
256
+
257
+
258
+
259
+ //WEPのSSID設定
260
+
261
+ String ssid = "SSID";
262
+
263
+ WifiConfiguration config = new WifiConfiguration();
264
+
265
+ config.SSID = "\"" + ssid + "\"";
266
+
267
+ config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
268
+
269
+ config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
270
+
271
+ config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
272
+
273
+ config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
274
+
275
+ config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
276
+
277
+ config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
278
+
279
+ config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
280
+
281
+ config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
282
+
283
+ config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
284
+
285
+ config.wepKeys[0] = "\"password\"";
286
+
287
+ config.wepTxKeyIndex = 0;
288
+
289
+ int networkId = wifiManager.addNetwork(config); // 失敗した場合は-1となります
290
+
291
+ wifiManager.saveConfiguration();
292
+
293
+ wifiManager.updateNetwork(config);
294
+
295
+
296
+
297
+ //SSIDに接続する
298
+
299
+ // WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
300
+
301
+ // int networkId = 0; // 上記設定で取得できたものを使用
302
+
303
+ String targetSSID = "SSID";
304
+
305
+ // WiFi機能が無効の状態で呼び出されるとSSID検索の所でnullとなるので念のため例外処理を行なう
306
+
307
+ try {
308
+
309
+ // ssidの検索を開始
310
+
311
+ wifiManager.startScan();
312
+
313
+ for (ScanResult result : wifiManager.getScanResults()) {
314
+
315
+ // Android4.2以降よりダブルクォーテーションが付いてくるので除去
316
+
317
+ String resultSSID = result.SSID.replace("\"", "");
318
+
319
+ if (resultSSID.equals(targetSSID)) {
320
+
321
+ // 接続を行う
322
+
323
+ if (networkId > 0) {
324
+
325
+ // 先に既存接続先を無効にしてから接続します
326
+
327
+ for (WifiConfiguration c0 : wifiManager.getConfiguredNetworks()) {
328
+
329
+ wifiManager.enableNetwork(c0.networkId, false);
330
+
331
+ }
332
+
333
+ wifiManager.enableNetwork(networkId, true);
334
+
335
+ }
336
+
337
+ break;
338
+
339
+ }
340
+
341
+ }
342
+
343
+ } catch (NullPointerException e) {
344
+
345
+ // 適切な例外処理をしてください。
346
+
347
+ }
348
+
349
+
350
350
 
351
351
  }
352
352
 
353
- }
353
+ }
354
354
 
355
355
 
356
356
 
357
357
  }
358
358
 
359
+
360
+
359
361
  ```
360
362
 
361
363
  ■activity_wi_fi.xml
@@ -428,8 +430,12 @@
428
430
 
429
431
 
430
432
 
431
- アプリを開いた際、wifiの状態取得できてそうなのですが、ボタンの「ON」「OFF」での制御がうまくいかなく困っています。
433
+ アプリを開いた際、wifiに接続はできのですが(OFFの場合接続ONになる)、ボタンの「ON」「OFF」での制御がうまくいかなく困っています。
434
+
432
-
435
+ 「public void onClick(View v) {」の処理でボタンのON.OFFの処理命令を記載しようとしておりますが、うまくいかなく困っています。
433
-
434
-
436
+
437
+
438
+
439
+
440
+
435
- 宜しくお願いします。
441
+ どうかご教授、宜しくお願いします。

1

できていそうな箇所を修正

2017/08/20 02:38

投稿

kumakumatan
kumakumatan

スコア213

test CHANGED
File without changes
test CHANGED
@@ -425,3 +425,11 @@
425
425
  登録したssidのwifiが接続、切断ができるようにしたいのですが、どのようにしたら
426
426
 
427
427
  良いかご教授いただきたくお願い致します。
428
+
429
+
430
+
431
+ アプリを開いた際、wifiの状態は取得できてそうなのですが、ボタンの「ON」「OFF」での制御がうまくいかなく困っています。
432
+
433
+
434
+
435
+ 宜しくお願いします。