קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
במכשירים עם Android 10 (API ברמה 29) ומעלה, אפשר להשתמש ב-API חדש של עמית לעמית כדי לאתחל את התצורה של מכשירים משניים כמו Chromecast וציוד של Google Home. התכונה הזו מאפשרת לאפליקציה לבקש מהמשתמש לשנות את נקודת הגישה שהמכשיר מחובר אליה באמצעות WifiNetworkSpecifier כדי לתאר את המאפיינים של הרשת המבוקשת.
כל בקשה צריכה לכלול לפחות אחד מהפרמטרים הבאים: SSID, SSID pattern, BSSID או BSSID pattern
בכל בקשה אפשר להגדיר רק אחד מהערכים SSID או SSID pattern
בכל בקשה אפשר להגדיר רק אחד מהערכים BSSID או BSSID pattern
מוסיפים את המגדירים לבקשת הרשת יחד עם מופע של NetworkCallback כדי לעקוב אחרי סטטוס הבקשה.
אם המשתמש מאשר את הבקשה והחיבור לרשת מצליח, הפונקציה NetworkCallback.onAvailable() מופעלת באובייקט של הקריאה החוזרת. אם המשתמש דוחה את הבקשה או אם החיבור לרשת לא מצליח, הפונקציה NetworkCallback.onUnavailable() מופעלת באובייקט של הקריאה החוזרת.
כשמתחילים את הבקשה להתחבר למכשיר עמית, נפתחת תיבת דו-שיח באותו מכשיר, שבה המשתמש במכשיר יכול לאשר את בקשת החיבור.
דילוג על אישור המשתמש
אחרי שהמשתמש מאשר לאפליקציה מסוימת להתחבר לרשת בתגובה לבקשה, האישור נשמר במכשיר לנקודת הגישה הספציפית.
אם האפליקציה שולחת בקשה ספציפית להתחבר שוב לנקודת הגישה הזו, המכשיר מדלג על שלב אישור המשתמש ומתחבר לרשת באופן אוטומטי. אם המשתמש יבחר להגדיר שהמכשיר ישכח את הרשת בזמן שהוא מחובר לרשת שהתבקשה על ידי ה-API, האישור השמור הזה לשילוב של האפליקציה והרשת יוסר, וכל בקשה עתידית מהאפליקציה תצטרך לקבל אישור מהמשתמש שוב. אם האפליקציה שולחת בקשה לא ספציפית, למשל עם תבנית SSID או BSSID, המשתמש צריך לאשר את הבקשה.
קוד לדוגמה
בדוגמת הקוד הבאה אפשר לראות איך להתחבר לרשת פתוחה עם קידומת SSID של "test" ו-OUI של BSSID של "10:03:23":
Kotlin
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);
דוגמאות התוכן והקוד שבדף הזה כפופות לרישיונות המפורטים בקטע רישיון לתוכן. Java ו-OpenJDK הם סימנים מסחריים או סימנים מסחריים רשומים של חברת Oracle ו/או של השותפים העצמאיים שלה.
עדכון אחרון: 2025-08-27 (שעון UTC).
[[["התוכן קל להבנה","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```"]]