Enregistrer les configurations de réseaux et de Passpoint
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Sur Android 11 (niveau de SDK 30) ou version ultérieure, les applications peuvent utiliser l'intent android.provider.Settings.ACTION_WIFI_ADD_NETWORKS pour guider l'utilisateur dans l'ajout d'un ou plusieurs réseaux enregistrés ou configurations Passpoint. L'API fonctionne également telle quelle pour modifier les configurations enregistrées existantes.
Pour enregistrer une configuration de réseau ou de Passpoint :
Créez un intent ACTION_WIFI_ADD_NETWORKS.
Créez une ou plusieurs configurations à l'aide de WifiNetworkSuggestion.Builder.
Notez que même si vous utilisez un WifiNetworkSuggestion, cette API Intent n'est pas liée à l'API Suggestion.
Créez une liste de tableaux Parcelable des configurations et attachez-la à l'intent avec l'extra EXTRA_WIFI_NETWORK_LIST.
Si resultCode est défini sur RESULT_OK, les données Intent contiennent l'élément supplémentaire EXTRA_WIFI_NETWORK_RESULT_LIST, qui contient un tableau de codes de résultat indiquant si les configurations individuelles ont été enregistrées avec succès. Voici les codes de résultat possibles :
Si la requête aboutit, la plate-forme déclenche une connexion à l'un des réseaux nouvellement enregistrés.
Exemple de code
L'exemple de code suivant montre comment enregistrer une configuration réseau ou Passpoint.
classMainActivity:AppCompatActivity(){overridefunonCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState)...}funstartOperation(){valsuggestions=ArrayList<WifiNetworkSuggestion>()// WPA2 configurationsuggestions.add(WifiNetworkSuggestion.Builder().setSsid("test111111").setWpa2Passphrase("test123456").build())// Open configurationsuggestions.add(WifiNetworkSuggestion.Builder().setSsid("test222222").build())// Passpoint configurationvalconfig=PasspointConfiguration()config.credential=Credential().apply{realm="realm.example.com"simCredential=Credential.SimCredential().apply{eapType=18imsi="123456*"}}config.homeSp=HomeSp().apply{fqdn="test1.example.com"friendlyName="Some Friendly Name"}suggestions.add(WifiNetworkSuggestion.Builder().setPasspointConfig(config).build())// Create intentvalbundle=Bundle()bundle.putParcelableArrayList(EXTRA_WIFI_NETWORK_LIST,suggestions)valintent=Intent(ACTION_WIFI_ADD_NETWORKS)intent.putExtras(bundle)// Launch intentstartActivityForResult(intent,0)}overridefunonActivityResult(requestCode:Int,resultCode:Int,data:Intent?){super.onActivityResult(requestCode,resultCode,data)if(resultCode==RESULT_OK){// user agreed to save configurations: still need to check individual resultsif(data!=null && data.hasExtra(EXTRA_WIFI_NETWORK_RESULT_LIST)){for(codeindata.getIntegerArrayListExtra(EXTRA_WIFI_NETWORK_RESULT_LIST)){when(code){ADD_WIFI_RESULT_SUCCESS->
...// Configuration saved or modifiedADD_WIFI_RESULT_ADD_OR_UPDATE_FAILED->
...// Something went wrong - invalid configurationADD_WIFI_RESULT_ALREADY_EXISTS->
...// Configuration existed (as-is) on device, nothing changedelse->
...// Other errors}}}}else{// User refused to save configurations}}}
Le contenu et les exemples de code de cette page sont soumis aux licences décrites dans la Licence de contenu. Java et OpenJDK sont des marques ou des marques déposées d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/08/27 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Il n'y a pas l'information dont j'ai besoin","missingTheInformationINeed","thumb-down"],["Trop compliqué/Trop d'étapes","tooComplicatedTooManySteps","thumb-down"],["Obsolète","outOfDate","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Mauvais exemple/Erreur de code","samplesCodeIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/08/27 (UTC)."],[],[],null,["On Android 11 (SDK level 30) and higher, apps can use the\n[`android.provider.Settings.ACTION_WIFI_ADD_NETWORKS`](/reference/android/provider/Settings#ACTION_WIFI_ADD_NETWORKS)\nintent to guide the user through adding one or more new saved networks or\nPasspoint configurations. The API also works as-is to modify existing saved\nconfigurations.\n| **Note:** This API is the closest in functionality to the deprecated `WifiManager.addNetwork(WifiConfiguration config)` API, in that the resulting configuration is added to the user-facing and managed saved network and subscription list.\n\nTo save a network or Passpoint configuration, do the following:\n\n1. Create an `ACTION_WIFI_ADD_NETWORKS` intent.\n\n2. Create one or more configurations using\n [`WifiNetworkSuggestion.Builder`](/reference/android/net/wifi/WifiNetworkSuggestion.Builder).\n Note that even though you use a `WifiNetworkSuggestion`, this Intent API is\n not related to the [Suggestion API](/develop/connectivity/wifi-suggest).\n\n3. Create a parcelable array list of the configurations and attach it to the\n intent with the\n [`EXTRA_WIFI_NETWORK_LIST`](/reference/android/provider/Settings#EXTRA_WIFI_NETWORK_LIST)\n extra.\n\n4. Execute\n [`Activity.startActivityForResult()`](/reference/android/app/Activity#startActivityForResult(android.content.Intent,%20int)),\n passing in the intent.\n\n5. Listen for the result using the\n [`Activity.onActivityResult()`](/reference/android/app/Activity#onActivityResult(int,%20int,%20android.content.Intent))\n callback.\n\n The `resultCode` can be one of the following:\n - [`Activity.RESULT_OK`](/reference/android/app/Activity#RESULT_OK): indicating that the user accepted the proposed networks and saved them.\n - [`Activity.RESULT_CANCELED`](/reference/android/app/Activity#RESULT_CANCELED): indicating that the user rejected the proposed networks.\n\n If the `resultCode` is `RESULT_OK`, then the data `Intent` contains the\n [`EXTRA_WIFI_NETWORK_RESULT_LIST`](/reference/android/provider/Settings#EXTRA_WIFI_NETWORK_RESULT_LIST)\n extra, which contains an array of result codes indicating whether individual\n configurations were saved successfully. The possible result codes are:\n - [`ADD_WIFI_RESULT_SUCCESS`](/reference/android/provider/Settings#ADD_WIFI_RESULT_SUCCESS): configuration added or successfully updated.\n - [`ADD_WIFI_RESULT_ADD_OR_UPDATE_FAILED`](/reference/android/provider/Settings#ADD_WIFI_RESULT_ADD_OR_UPDATE_FAILED): failure when trying to add configuration, such as due to a badly formed configuration.\n - [`ADD_WIFI_RESULT_ALREADY_EXISTS`](/reference/android/provider/Settings#ADD_WIFI_RESULT_ALREADY_EXISTS): the requested configuration already existed so no action was necessary.\n6. If the request is successful, the platform triggers a connection to one of the\n newly saved networks.\n\nCode sample\n\nThe following code sample shows how to save a network or Passpoint\nconfiguration. \n\n class MainActivity : AppCompatActivity() {\n override fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n ...\n }\n\n fun startOperation() {\n val suggestions = ArrayList\u003cWifiNetworkSuggestion\u003e()\n\n // WPA2 configuration\n suggestions.add(\n WifiNetworkSuggestion.Builder()\n .setSsid(\"test111111\")\n .setWpa2Passphrase(\"test123456\")\n .build()\n )\n\n // Open configuration\n suggestions.add(\n WifiNetworkSuggestion.Builder()\n .setSsid(\"test222222\")\n .build()\n )\n\n // Passpoint configuration\n val config = PasspointConfiguration()\n config.credential = Credential().apply {\n realm = \"realm.example.com\"\n simCredential = Credential.SimCredential().apply {\n eapType = 18\n imsi = \"123456*\"\n }\n }\n config.homeSp = HomeSp().apply {\n fqdn = \"test1.example.com\"\n friendlyName = \"Some Friendly Name\"\n }\n suggestions.add(\n WifiNetworkSuggestion.Builder()\n .setPasspointConfig(config)\n .build())\n\n // Create intent\n val bundle = Bundle()\n bundle.putParcelableArrayList(EXTRA_WIFI_NETWORK_LIST, suggestions)\n val intent = Intent(ACTION_WIFI_ADD_NETWORKS)\n intent.putExtras(bundle)\n\n // Launch intent\n startActivityForResult(intent, 0)\n }\n\n override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {\n super.onActivityResult(requestCode, resultCode, data)\n if(resultCode == RESULT_OK) {\n // user agreed to save configurations: still need to check individual results\n if (data != null && data.hasExtra(EXTRA_WIFI_NETWORK_RESULT_LIST)) {\n for (code in data.getIntegerArrayListExtra(EXTRA_WIFI_NETWORK_RESULT_LIST)) {\n when (code) {\n ADD_WIFI_RESULT_SUCCESS -\u003e\n ... // Configuration saved or modified\n ADD_WIFI_RESULT_ADD_OR_UPDATE_FAILED -\u003e\n ... // Something went wrong - invalid configuration\n ADD_WIFI_RESULT_ALREADY_EXISTS -\u003e\n ... // Configuration existed (as-is) on device, nothing changed\n else -\u003e\n ... // Other errors\n }\n }\n }\n } else {\n // User refused to save configurations\n }\n }\n }"]]