赞
踩
从 Android 10 开始,Wi-Fi 基础架构引入两个全新 API 表面:适用于互联网连接的 Wi-Fi 建议 API 和适用于对等连接的 Wi-Fi 网络请求 API。
文档
适用于互联网连接的 Wi-Fi 建议 API
适用于对等连接的 Wi-Fi 网络请求 API
运行 Android 10 (API 级别 29) 或更高版本 的设备允许您的应用添加设备的网络凭据,以自动连接到 WLAN 接入点。您可以使用 WifiNetworkSuggestion 就连接到哪个网络提供建议。平台最终会根据您的应用和其他应用的建议,选择要接受的接入点。
以下代码示例展示如何为一个开放式网络、一个 WPA2 网络和一个 WPA3 网络提供凭据:
val suggestion1 = WifiNetworkSuggestion.Builder() .setSsid("test111111") .setIsAppInteractionRequired() // Optional (Needs location permission) .build() val suggestion2 = WifiNetworkSuggestion.Builder() .setSsid("test222222") .setWpa2Passphrase("test123456") .setIsAppInteractionRequired() // Optional (Needs location permission) .build() val suggestion3 = WifiNetworkSuggestion.Builder() .setSsid("test333333") .setWpa3Passphrase("test6789") .setIsAppInteractionRequired() // Optional (Needs location permission) .build() val suggestionsList = listOf(suggestion1, suggestion2, suggestion3) val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager val status = wifiManager.addNetworkSuggestions(suggestionsList); if (status != WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS) { // do error handling here } // Optional (Wait for post connection broadcast to one of your suggestions) val intentFilter = IntentFilter(WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION); val broadcastReceiver = object : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { if (!intent.action.equals(WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION)) { return; } // do post connect processing here } }; context.registerReceiver(broadcastReceiver, intentFilter);
Java
final WifiNetworkSuggestion suggestion1 = new WifiNetworkSuggestion.Builder() .setSsid("test111111") .setIsAppInteractionRequired() // Optional (Needs location permission) .build() final WifiNetworkSuggestion suggestion2 = new WifiNetworkSuggestion.Builder() .setSsid("test222222") .setWpa2Passphrase("test123456") .setIsAppInteractionRequired() // Optional (Needs location permission) .build() final WifiNetworkSuggestion suggestion3 = new WifiNetworkSuggestion.Builder() .setSsid("test333333") .setWpa3Passphrase("test6789") .setIsAppInteractionRequired() // Optional (Needs location permission) .build() final List<WifiNetworkSuggestion> suggestionsList = new ArrayList<WifiNetworkSuggestion> {{ add(suggestion1); add(suggestion2); add(suggestion3); }}; final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); final int status = wifiManager.addNetworkSuggestions(suggestionsList); if (status != WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS) { // do error handling here… } // Optional (Wait for post connection broadcast to one of your suggestions) final IntentFilter intentFilter = new IntentFilter(WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION); final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (!intent.getAction().equals( WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION)) { return; } // do post connect processing here... } }; context.registerReceiver(broadcastReceiver, intentFilter);
应用的建议必须获得用户批准,然后平台才会发起指向建议的连接。首次在扫描结果中发现与用户某条建议相匹配的网络时,平台会向用户发送通知,用户提供此批准以响应该通知。当平台连接到其中一个建议网络时,设置会显示将网络连接归因到相应建议者应用的文本。
如果用户使用 WLAN 选择器来显式断开所连接的某个建议网络,则系统会将该网络列入黑名单 24 小时。在处于黑名单期间,系统不会考虑自动连接该网络,即使应用删除并重新添加与该网络对应的网络建议,也是如此。
拒绝网络建议通知的用户可以移除应用的 CHANGE_WIFI_STATE 权限。用户可在稍后进入 WLAN 控制菜单 (Settings > Apps & notifications > Special App access > Wi-Fi Control > App name),授予此批准。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。