After Android Q, I connected to WiFi through the following code:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q)
{
NetworkSpecifier specifier =
new WifiNetworkSpecifier.Builder()
.setSsidPattern(new PatternMatcher(ssid, PatternMatcher.PATTERN_PREFIX))
.setWpa2Passphrase(password)
.build();
NetworkRequest request =
new NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.setNetworkSpecifier(specifier)
.build();
final ConnectivityManager connectivityManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
// do success processing here..
Log.d(TAG, "onAvailable:" + network);
super.onAvailable(network);
LogUtil.e("network.getSocketFactory()",network.getSocketFactory()+"------->");
boolean b = connectivityManager.bindProcessToNetwork(network);
}
/**
*
*/
@Override
public void onLost(Network network) {
super.onLost(network);
Log.e(TAG, "onLost ==>" + network.toString());
}
@Override
public void onUnavailable() {
// do failure processing here..
LogUtil.e("qwe","failed");
}
};
connectivityManager.requestNetwork(request, networkCallback);
}
However, there is a problem. The connected WiFi can only be used within the range of my APP. The browser of the mobile phone or other APPs cannot use the network.
Does anyone know what the problem is, I hope everyone tells me how to deal with it so that other apps can use the network.
thank you very much.