在Android Q中,有几个WiFi API受到限制。我正试图使用其他的API来连接不同的Wifi AP上网。
Below is my code :
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);
这使我能够连接,但互联网被禁用。这是在安卓文档中定义的工作。
我尝试的其他方法如下。
WifiNetworkSuggestion.Builder wifiNetworkSuggestionBuilder1 = new WifiNetworkSuggestion.Builder();
wifiNetworkSuggestionBuilder1.setSsid("wifi-ap-ssid");
wifiNetworkSuggestionBuilder1.setWpa2Passphrase("wifi-ap-password");
WifiNetworkSuggestion wifiNetworkSuggestion = wifiNetworkSuggestionBuilder1.build();
List<WifiNetworkSuggestion> list = new ArrayList<>();
list.add(wifiNetworkSuggestion);
wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.removeNetworkSuggestions(new ArrayList<WifiNetworkSuggestion>());
wifiManager.addNetworkSuggestions(list);
宣布允许在Manifest :
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
使用这个并没有在行为上改变什么。
请告诉我们成功连接到不同的具有互联网功能的Wifi AP的API序列。