I am connecting to a wifi network using this code:
String SSID = "Alarm";
String pass = "Alarm1234";
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + SSID + "\"";
conf.preSharedKey = "\"" + pass + "\"";
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
if(!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
}
int netId = wifiManager.addNetwork(conf);
wifiManager.disconnect();
wifiManager.getConfiguredNetworks().forEach((WifiConfiguration wifiConfiguration) -> wifiManager.disableNetwork(wifiConfiguration.networkId));
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
and it works fine. The problem is however, that while connected to the new wifi-network, mobile data stays enabled as well (the 4g icon is still showing), and this is a problem for my application. When I close the app, the 4g icon dissapears. I need it to turn off while I'm still in the app. Is there any solution for this?