I sadly don't have a solution for you, but here is what i tried already:
You can add the Wifi using WifiNetworkSuggestion, but there is no notification for the user to confirm the connection so it has no real effect.
You can connect to the wifi by creating a NetworkRequest, which works in that the user gets a popup and the ML2 connects while the app is open, however i had no sucess in sending/recieving anything on the network/internet.
Code For NetworkRequest
private static ConnectivityManager connectivityManager = null;
...
{
connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder = new NetworkRequest.Builder();
builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
WifiNetworkSpecifier wifiNetworkSpecifier = getWifiNetworkSpecifier(ssid, password);
builder.setNetworkSpecifier(wifiNetworkSpecifier);
NetworkRequest networkRequest = builder.build();
RegisterCallback();
connectivityManager.requestNetwork(networkRequest, callback);
}
private static void RegisterCallback() {
callback = new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
super.onAvailable(network);
connectivityManager.bindProcessToNetwork(network);
Log.d("NetworkManager", "WiFi connected");
}
@Override
public void onUnavailable() {
super.onUnavailable();
Log.d("NetworkManager", "Wifi not Available: ");
connectivityManager.bindProcessToNetwork(null);
}
};
}