Android Q, Programmatically Connect to Different Wifi Ap for Internet

Connect to Wifi in Android Q programmatically

So, the solution for me is compile your app with targetSdkVersion 28.
and for connection to wifi use this function

connectToWifi(String ssid, String key)

it's just a workaround for the moment, waiting Google to publish a fix for this bug, for more information the issue reported to Google : issuetracker.google.com/issues/138335744

public void connectToWifi(String ssid, String key) {

Log.e(TAG, "connection wifi pre Q");
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = "\"" + ssid + "\"";
wifiConfig.preSharedKey = "\"" + key + "\"";
int netId = wifiManager.addNetwork(wifiConfig);
if (netId == -1) netId = getExistingNetworkId(wifiConfig.SSID);

wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
}

Connect to WiFi Access Point programmatically with Android 10

I struggle couple days ago with wifi connection on Andrdoid Q, the link of my question in stackOverflow.

So, the solution is compile your app with targetSdkVersion 28.
and for connection to wifi use this function :

 public void connectToWifi(String ssid, String key) {

Log.e(TAG, "connection wifi pre Q");
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = "\"" + ssid + "\"";
wifiConfig.preSharedKey = "\"" + key + "\"";
int netId = wifiManager.addNetwork(wifiConfig);
if (netId == -1) netId = getExistingNetworkId(wifiConfig.SSID);

wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
}

Connect To Wifi Android Q

This is my code :

val wifiManager = applicationContext.getSystemService(WIFI_SERVICE) as WifiManager?
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
try {
val wifiConfig = WifiConfiguration()
wifiConfig.SSID = "\"" + yourSsid + "\""
wifiConfig.preSharedKey = "\"" + password + "\""
val netId = wifiManager!!.addNetwork(wifiConfig)
wifiManager.disconnect()
wifiManager.enableNetwork(netId, true)
wifiManager.reconnect()
if (isWifiConnected("\"" + deviceId + "\"")) {
doSomethingHere()
}
} catch (e: Exception) {
e.printStackTrace()
}
} else {
val wifiNetworkSpecifier = WifiNetworkSpecifier.Builder()
.setSsid( yourSsid!! )
.setWpa2Passphrase(password)
.build()

val networkRequest = NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.setNetworkSpecifier(wifiNetworkSpecifier)
.build()

connectivityManager = Boron.instance.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?

networkCallback = object : ConnectivityManager.NetworkCallback() {
override fun onUnavailable() {
super.onUnavailable()
}

override fun onLosing(network: Network, maxMsToLive: Int) {
super.onLosing(network, maxMsToLive)

}

override fun onAvailable(network: Network) {
super.onAvailable(network)
connectivityManager?.bindProcessToNetwork(network)
}

override fun onLost(network: Network) {
super.onLost(network)

}
}
connectivityManager?.requestNetwork(networkRequest,networkCallback)
}

Then in onDestroy
connectivityManager?.unregisterNetworkCallback(networkCallback)

Permissions needed :

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-feature android:name="android.permission.WRITE_SETTINGS"
android:required="false"/>

Connecting to a specific wifi network programmatically in Android (API 30)

You can use gently ask your users to turn on the wifi by using the new Settings.Panel (API 29+) or the old Settings API. Additionally, you can use startActivityForResult() to check if the user did turn on the Wifi from the Settings.

if(!wifiManager.isWifiEnabled()) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startActivity(new Intent(Settings.Panel.ACTION_WIFI));
} else {
// Use a full page activity - if Wifi is critcal for your app
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
// Or use the deprecated method
wifiManager.setWifiEnabled(true)
}
}

How do I connect to a specific Wi-Fi network in Android programmatically?

You need to create WifiConfiguration instance like this:

String networkSSID = "test";
String networkPass = "pass";

WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\""; // Please note the quotes. String should contain ssid in quotes

Then, for WEP network you need to do this:

conf.wepKeys[0] = "\"" + networkPass + "\""; 
conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);

For WPA network you need to add passphrase like this:

conf.preSharedKey = "\""+ networkPass +"\"";

For Open network you need to do this:

conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

Then, you need to add it to Android wifi manager settings:

WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); 
wifiManager.addNetwork(conf);

And finally, you might need to enable it, so Android connects to it:

List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();

break;
}
}

UPD: In case of WEP, if your password is in hex, you do not need to surround it with quotes.



Related Topics



Leave a reply



Submit