AndroidStudio2.3.3にて、指定のwifi(SSIDをWEPキーで認証)の接続状態を調べ、
「オン」「オフ」状態を画面に表示し、ボタンを押すことにより、接続を切替えるアプリを作成
しようとしております。
・ssid:SSID
・password:password
■AndroidManifest.xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="wifi.sample.goodroid.mywifi01"> 4 5 <uses-sdk 6 android:minSdkVersion="10" 7 android:targetSdkVersion="15" /> 8 9 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> 10 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> 11 12 <application 13 android:allowBackup="true" 14 android:icon="@mipmap/ic_launcher" 15 android:label="@string/app_name" 16 android:supportsRtl="true" 17 android:theme="@style/AppTheme"> 18 <activity android:name=".WiFiActivity"> 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 </application> 26 27</manifest>
■WiFiActivity
java
1package wifi.sample.goodroid.mywifi01; 2 3import android.annotation.SuppressLint; 4import android.content.Context; 5import android.net.wifi.ScanResult; 6import android.net.wifi.WifiConfiguration; 7import android.net.wifi.WifiManager; 8import android.os.Bundle; 9import android.view.View; 10import android.view.View.OnClickListener; 11import android.widget.ToggleButton; 12import android.app.Activity; 13 14public class WiFiActivity extends Activity implements OnClickListener{ 15 16 private ToggleButton toggle; 17 private WifiManager Wifi; 18 19 @SuppressLint("WifiManagerLeak") 20 @Override 21 public void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_wi_fi); 24 25 // WifiManagerのインスタンスを取得する 26 Wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE); 27 28 //WiFi機能をONにする 29 WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE); 30 if (wifiManager.isWifiEnabled() == false) { 31 wifiManager.setWifiEnabled(true); 32 } 33 34 35 //WEPのSSID設定 36 String ssid = "SSID"; 37 WifiConfiguration config = new WifiConfiguration(); 38 config.SSID = "\"" + ssid + "\""; 39 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 40 config.allowedProtocols.set(WifiConfiguration.Protocol.RSN); 41 config.allowedProtocols.set(WifiConfiguration.Protocol.WPA); 42 config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); 43 config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED); 44 config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); 45 config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); 46 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); 47 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104); 48 config.wepKeys[0] = "\"password\""; 49 config.wepTxKeyIndex = 0; 50 int networkId = wifiManager.addNetwork(config); // 失敗した場合は-1となります 51 wifiManager.saveConfiguration(); 52 wifiManager.updateNetwork(config); 53 54 55 56 57 // トグルボタンのインスタンスを取得する 58 toggle = (ToggleButton) findViewById(R.id.toggleButton1); 59 60 // トグルボタンの状態初期化 61 if(Wifi.isWifiEnabled()){ 62 63 // 現在のWifiがONだった場合、トグルボタンをONに切り替える 64 toggle.setChecked(true); 65 66 }else{ 67 68 // 現在のWifiがOffだった場合、トグルボタンをOFFに切り替える 69 toggle.setChecked(false); 70 } 71 72 } 73 74 @Override 75 public void onClick(View v) { 76 77 if(Wifi.isWifiEnabled()){ 78 // 現在のWifiがONだった場合、OFFに切り替える 79 Wifi.setWifiEnabled(false); 80 81 }else{ 82 // 現在のWifiがOFFだった場合、ONに切り替える 83 // Wifi.setWifiEnabled(true); 84 85 //WiFi機能をONにする 86 WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE); 87 if (!wifiManager.isWifiEnabled()) { 88 wifiManager.setWifiEnabled(true); 89 } 90 91 //WEPのSSID設定 92 String ssid = "SSID"; 93 WifiConfiguration config = new WifiConfiguration(); 94 config.SSID = "\"" + ssid + "\""; 95 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 96 config.allowedProtocols.set(WifiConfiguration.Protocol.RSN); 97 config.allowedProtocols.set(WifiConfiguration.Protocol.WPA); 98 config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); 99 config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED); 100 config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); 101 config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); 102 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); 103 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104); 104 config.wepKeys[0] = "\"password\""; 105 config.wepTxKeyIndex = 0; 106 int networkId = wifiManager.addNetwork(config); // 失敗した場合は-1となります 107 wifiManager.saveConfiguration(); 108 wifiManager.updateNetwork(config); 109 110 //SSIDに接続する 111 // WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE); 112 // int networkId = 0; // 上記設定で取得できたものを使用 113 String targetSSID = "SSID"; 114// WiFi機能が無効の状態で呼び出されるとSSID検索の所でnullとなるので念のため例外処理を行なう 115 try { 116 // ssidの検索を開始 117 wifiManager.startScan(); 118 for (ScanResult result : wifiManager.getScanResults()) { 119 // Android4.2以降よりダブルクォーテーションが付いてくるので除去 120 String resultSSID = result.SSID.replace("\"", ""); 121 if (resultSSID.equals(targetSSID)) { 122 // 接続を行う 123 if (networkId > 0) { 124 // 先に既存接続先を無効にしてから接続します 125 for (WifiConfiguration c0 : wifiManager.getConfiguredNetworks()) { 126 wifiManager.enableNetwork(c0.networkId, false); 127 } 128 wifiManager.enableNetwork(networkId, true); 129 } 130 break; 131 } 132 } 133 } catch (NullPointerException e) { 134 // 適切な例外処理をしてください。 135 } 136 137 } 138 } 139 140} 141
■activity_wi_fi.xml
xml
1<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" > 5 6 <ToggleButton 7 android:id="@+id/toggleButton1" 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content" 10 android:text="ToggleButton" 11 android:layout_below="@+id/textView1" 12 android:layout_centerHorizontal="true" 13 android:layout_marginTop="82dp" /> 14 15 <TextView 16 android:id="@+id/textView1" 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:layout_marginLeft="15dp" 20 android:text="Wi-Fi状態" 21 android:textAppearance="?android:attr/textAppearanceLarge" 22 android:layout_alignParentTop="true" 23 android:layout_alignParentStart="true" 24 android:layout_marginStart="24dp" 25 android:layout_marginTop="37dp" /> 26 27</RelativeLayout>
*WiFiActivityの箇所を変更する事により、ボタンの「オン」「オフ」により、
登録したssidのwifiが接続、切断ができるようにしたいのですが、どのようにしたら
良いかご教授いただきたくお願い致します。
アプリを開いた際、wifiに接続はできるのですが(OFFの場合、接続ONになる)、ボタンの「ON」「OFF」での制御がうまくいかなく困っています。
「public void onClick(View v) {」の処理でボタンのON.OFFの処理命令を記載しようとしておりますが、うまくいかなく困っています。
どうかご教授、宜しくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/08/24 04:09
2017/08/24 04:13
2017/08/24 04:52
2017/08/24 05:28
2017/08/24 05:57
2017/08/24 07:12 編集
2017/08/25 05:02 編集
2017/08/29 01:12
2017/08/29 04:29 編集
2017/08/29 04:44
2017/08/29 04:58
2017/08/29 05:15
2017/08/29 05:57
2017/08/29 06:06
2017/08/30 01:10 編集
2017/08/30 06:30
2017/08/30 07:38
2017/09/01 04:07
2017/09/04 03:00
2017/09/26 00:48
2017/09/26 04:40 編集
2017/09/26 05:19
2017/09/26 05:23
2017/09/26 06:22
2017/09/27 01:04
2017/09/27 07:22