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
How to Resolve Javax.Xml.Bind.Jaxbcontext in Eclipse
How to Make a Textview Automatically Scroll as I Add More Lines of Text
Android Set Height and Width of Custom View Programmatically
Check If an Email Exists Before Creating a User Firebase Android
Play Store Alpha Test Download Link Not Working
How to Change the Icon Color Selected on Bottom Navigation Bar in Android Studio
Android - Transform Classes With Dex for Debug
How to Painlessly Change My App Title Text Color
How to Make User Login from Only One Device At a Time
How to Set Align (Right or Left) of Text in Textview, Programmatically
React Native: Java_Home Is Not Set and No 'Java' Command Could Be Found in Your Path
Why Does My Button Not Showing in the Layout
Method to Refresh Fragment Content When Data Changed ( Like Recall Oncreateview)
How to Set Spinner Default by Its Value Instead of Position
Cart Item Count Increment/Decrement & Add to Cart
Notification Disappears After Showing
How to Convert Utc Timestamp to Device Local Time in Android