LoginSignup
13
14

More than 5 years have passed since last update.

AndroidアプリからWi-Fiのパスワード設定を変更する

Posted at

AndroidアプリからWi-Fi設定のパスワードを変更する際は以下のようにします。

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
String newPassword = "hogehoge"
WifiConfiguration wifiConf = ...;  // WifiManager#getConfiguredNetworks などから取得する
int networkId = wifiConf.networkId;
wifiConf.preSharedKey = "\"" + newPassword + "\"";
if (networkId == wifiManager.updateNetwork(wifiConf)) {
    // 成功
} else {
    // 失敗
}

パスワードを設定する際に注意すべきなのが、文字列の前後に"を付けることです。
これを付けないとupdateNetworkが失敗してしまいます。(戻り値が-1となります。)

13
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
14