Android 10(API レベル 29)以降を搭載しているデバイスでは、新しいピアツーピア API を使用して、Chromecast や Google Home ハードウェアなどのセカンダリ デバイスの構成をブートストラップできます。この機能により、アプリはリクエストされたネットワークのプロパティを WifiNetworkSpecifier を使用して記述することで、デバイスが接続されているアクセス ポイントの変更を求めるプロンプトをユーザーに表示できます。
valspecifier=WifiNetworkSpecifier.Builder().setSsidPattern(PatternMatcher("test",PatternMatcher.PATTERN_PREFIX)).setBssidPattern(MacAddress.fromString("10:03:23:00:00:00"),MacAddress.fromString("ff:ff:ff:00:00:00")).build()valrequest=NetworkRequest.Builder().addTransportType(NetworkCapabilities.TRANSPORT_WIFI).removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).setNetworkSpecifier(specifier).build()valconnectivityManager=context.getSystemService(Context.CONNECTIVITY_SERVICE)asConnectivityManagervalnetworkCallback=object:ConnectivityManager.NetworkCallback(){...overridefunonAvailable(network:Network?){// do success processing here..}overridefunonUnavailable(){// do failure processing here..}...}connectivityManager.requestNetwork(request,networkCallback)...// Release the request when done.connectivityManager.unregisterNetworkCallback(networkCallback)
Java
finalNetworkSpecifierspecifier=newWifiNetworkSpecifier.Builder().setSsidPattern(newPatternMatcher("test",PatternMatcher.PATTERN_PREFIX)).setBssidPattern(MacAddress.fromString("10:03:23:00:00:00"),MacAddress.fromString("ff:ff:ff:00:00:00")).build();finalNetworkRequestrequest=newNetworkRequest.Builder().addTransportType(NetworkCapabilities.TRANSPORT_WIFI).removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).setNetworkSpecifier(specifier).build();finalConnectivityManagerconnectivityManager=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);finalNetworkCallbacknetworkCallback=newNetworkCallback(){...@OverridevoidonAvailable(...){// do success processing here..}@OverridevoidonUnavailable(...){// do failure processing here..}...};connectivityManager.requestNetwork(request,networkCallback);...// Release the request when done.connectivityManager.unregisterNetworkCallback(networkCallback);
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-08-27 UTC。"],[],[],null,["On Android 10 (API level 29) and higher devices, you can use a new peer to peer API to\nbootstrap configuration for secondary devices like Chromecast and Google Home\nhardware. This feature enables your app to prompt the user to change the access\npoint that the device is connected to by using\n[`WifiNetworkSpecifier`](/reference/android/net/wifi/WifiNetworkSpecifier)\nto describe properties of a requested network.\n\nTo use this API, do the following:\n\n1. Create a Wi-Fi network specifier using\n [`WifiNetworkSpecifier.Builder`](/reference/android/net/wifi/WifiNetworkSpecifier.Builder).\n\n2. Set a network filter to match networks to connect to, along with required\n credentials.\n\n3. Decide on a combination of [`SSID`](/reference/android/net/wifi/WifiNetworkSpecifier.Builder#setssid),\n [`SSID pattern`](/reference/android/net/wifi/WifiNetworkSpecifier.Builder#setssidpattern),\n [`BSSID`](/reference/android/net/wifi/WifiNetworkSpecifier.Builder#setbssid),\n and [`BSSID pattern`](/reference/android/net/wifi/WifiNetworkSpecifier.Builder#setbssidpattern)\n to set the network filter in each request, subject to the following\n requirements:\n\n - Each request should provide at least one of `SSID`, `SSID pattern`, `BSSID`, or `BSSID pattern`\n - Each request can set only one of `SSID` or `SSID pattern`\n - Each request can set only one of `BSSID` or `BSSID pattern`\n4. Add the specifiers to the network request along with a\n [`NetworkCallback`](/reference/android/net/ConnectivityManager.NetworkCallback)\n instance to track the status of the request.\n\n If the user accepts the request and the connection to the network is\n successful,\n [`NetworkCallback.onAvailable()`](/reference/android/net/ConnectivityManager.NetworkCallback#onAvailable(android.net.Network))\n is invoked on the callback object. If the user denies the request or if the\n connection to the network is unsuccessful,\n [`NetworkCallback.onUnavailable()`](/reference/android/net/ConnectivityManager.NetworkCallback#onUnavailable())\n is invoked on the callback object.\n\nInitiating the request to connect to a peer device launches a dialog box on the\nsame device, from which that device's user can accept the connection request.\n| **Note:** Creating a connection using this API does not provide an internet connection to the app or to the device. To provide an internet connection to the apps on a device, use the [Wi-Fi Suggestion API](/develop/connectivity/wifi/wifi-suggest) instead.\n\nBypassing user approval\n\nOnce the user approves a network to connect to in response to a request from a\nspecific app, the device stores the approval for the particular access point.\nIf the app makes a specific request to\nconnect to that access point again, the device skips the user approval phase\nand automatically connects to the network. If the user chooses to forget the\nnetwork while connected to a network requested by the API, then this stored\napproval for that combination of app and network is removed, and any future\nrequest from the app must be approved by the user again. If the app\nmakes a non-specific request, such as with an SSID or BSSID pattern, then the\nuser must approve the request.\n\nCode sample\n\nThe following code sample shows how to connect to an open network with an SSID\nprefix of `\"test\"` and a BSSID OUI of `\"10:03:23\"`: \n\nKotlin \n\n```kotlin\nval specifier = WifiNetworkSpecifier.Builder()\n .setSsidPattern(PatternMatcher(\"test\", PatternMatcher.PATTERN_PREFIX))\n .setBssidPattern(MacAddress.fromString(\"10:03:23:00:00:00\"), MacAddress.fromString(\"ff:ff:ff:00:00:00\"))\n .build()\n\nval request = NetworkRequest.Builder()\n .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)\n .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)\n .setNetworkSpecifier(specifier)\n .build()\n\nval connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager\n\nval networkCallback = object : ConnectivityManager.NetworkCallback() {\n ...\n override fun onAvailable(network: Network?) {\n // do success processing here..\n }\n\n override fun onUnavailable() {\n // do failure processing here..\n }\n ...\n}\nconnectivityManager.requestNetwork(request, networkCallback)\n...\n// Release the request when done.\nconnectivityManager.unregisterNetworkCallback(networkCallback)\n```\n\nJava \n\n```java\nfinal NetworkSpecifier specifier =\n new WifiNetworkSpecifier.Builder()\n .setSsidPattern(new PatternMatcher(\"test\", PatternMatcher.PATTERN_PREFIX))\n .setBssidPattern(MacAddress.fromString(\"10:03:23:00:00:00\"), MacAddress.fromString(\"ff:ff:ff:00:00:00\"))\n .build();\n\nfinal NetworkRequest request =\n new NetworkRequest.Builder()\n .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)\n .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)\n .setNetworkSpecifier(specifier)\n .build();\n\nfinal ConnectivityManager connectivityManager = (ConnectivityManager)\n context.getSystemService(Context.CONNECTIVITY_SERVICE);\n\nfinal NetworkCallback networkCallback = new NetworkCallback() {\n ...\n @Override\n void onAvailable(...) {\n // do success processing here..\n }\n\n @Override\n void onUnavailable(...) {\n // do failure processing here..\n }\n ...\n};\nconnectivityManager.requestNetwork(request, networkCallback);\n...\n// Release the request when done.\nconnectivityManager.unregisterNetworkCallback(networkCallback);\n```"]]