在搭载 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"]],["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Wi-Fi Network Request API for peer-to-peer connectivity\n\nOn 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-----------------------\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-----------\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\n### Kotlin\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\n### Java\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```"]]