与Android Q中一样,几个WiFi API也受到限制。我正在尝试使用备用API连接到Internet的不同Wifi AP。
下面是我的代码:
WifiNetworkSpecifier.Builder builder = new WifiNetworkSpecifier.Builder(); builder.setSsid("wifi-ap-ssid"); builder.setWpa2Passphrase("wifi-ap-password"); WifiNetworkSpecifier wifiNetworkSpecifier = builder.build(); NetworkRequest.Builder networkRequestBuilder1 = new NetworkRequest.Builder(); networkRequestBuilder1.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); networkRequestBuilder1.setNetworkSpecifier(wifiNetworkSpecifier); NetworkRequest nr = networkRequestBuilder1.build(); ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); cm.requestNetwork(nr, callback);
这使我可以连接,但互联网已禁用。这正在按照Android文档中的定义运行。
我尝试的替代方法如下:
WifiNetworkSuggestion.Builder wifiNetworkSuggestionBuilder1 = new WifiNetworkSuggestion.Builder(); wifiNetworkSuggestionBuilder1.setSsid("wifi-ap-ssid"); wifiNetworkSuggestionBuilder1.setWpa2Passphrase("wifi-ap-password"); WifiNetworkSuggestion wifiNetworkSuggestion = wifiNetworkSuggestionBuilder1.build(); Listlist = new ArrayList<>(); list.add(wifiNetworkSuggestion); wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); wifiManager.removeNetworkSuggestions(new ArrayList ()); wifiManager.addNetworkSuggestions(list);
在清单中声明允许:
使用它并没有改变任何行为。
请告知API序列,以成功连接到具有Internet功能的其他Wifi AP。