Android 10 don`t connect to WPA wifi programmatically - android

For programmatically connection to WPA wifi I`am try to use below code.
WifiNetworkSpecifier.Builder builder = new WifiNetworkSpecifier.Builder();
builder.setSsid(deviceSSID);
builder.setWpa2Passphrase(devicePass);
builder.setIsEnhancedOpen(false);
WifiNetworkSpecifier wifiNetworkSpecifier = builder.build();
NetworkRequest.Builder networkRequestBuilder = new NetworkRequest.Builder();
networkRequestBuilder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
networkRequestBuilder.setNetworkSpecifier(wifiNetworkSpecifier);
NetworkRequest networkRequest = networkRequestBuilder.build();
final ConnectivityManager cm = (ConnectivityManager) StartActivity.this.getSystemService(Context.CONNECTIVITY_SERVICE);
ConnectivityManager.NetworkCallback networkCallback;
networkCallback = new ConnectivityManager.NetworkCallback() {
#Override
public void onAvailable(#NonNull Network network) {
super.onAvailable(network);
cm.bindProcessToNetwork(network);
}
};
cm.requestNetwork(networkRequest, networkCallback);
For android versions < 10 i use wifiManager which has reconnection method deprecated. Why does my code not working for Android 10?

Related

Android 10 Connect wifi programmatically

I am trying to connect Specific wifi network in android 10(Android Q). Below android 10 it was working fine. I gone through some documents. Theysaid will have to use WifiNetworkSpecifier. I tried by using below code stuff.
WifiNetworkSpecifier.Builder builder = new WifiNetworkSpecifier.Builder();
builder.setSsid(ssid);
builder.setWpa2Passphrase(password);
WifiNetworkSpecifier wifiNetworkSpecifier = builder.build();
NetworkRequest.Builder networkRequestBuilder = new NetworkRequest.Builder();
networkRequestBuilder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
networkRequestBuilder.setNetworkSpecifier(wifiNetworkSpecifier);
NetworkRequest networkRequest = networkRequestBuilder.build();
ConnectivityManager cm = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm != null) {
cm.requestNetwork(networkRequest, new ConnectivityManager.NetworkCallback() {
#Override
public void onAvailable(#NonNull Network network) {
super.onAvailable(network);
cm.bindProcessToNetwork(network);
}
});
}
But I am unable to connect specified network. Can anyone please guide me..
Thanks in Advance.

WifiNetworkSpecifier - auto connect when single network available

I'm establishing a connection to a specified network by ssid/pass using WifiNetworkSpecifier. Below is my code:
specifier = new WifiNetworkSpecifier.Builder()
.setSsid(this.ssid)
.setWpa2Passphrase(this.pass)
.build();
NetworkRequest networkRequest = new NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.addCapability(NetworkCapabilities.NET_CAPABILITY_TRUSTED)
.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)
.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)
.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)
.removeCapability(NetworkCapabilities.NET_CAPABILITY_FOREGROUND)
.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_CONGESTED)
.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED)
.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING)
.setNetworkSpecifier(specifier)
.build();
cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
callback = new ConnectivityManager.NetworkCallback() {
#Override
public void onUnavailable() {
[....]
}
#Override
public void onAvailable(#NonNull Network network) {
[....]
}
};
cm.requestNetwork(networkRequest, callback, TIMEOUT);
And here is my question. Can I auto connect to the only network available? The behaviour now is like this : a popup displayed with the only network available and I have to tap connect. I see it as an useless step for my app.
Mention: It is an internal app and I always have to be connected on my local wifi.
Thank you!

How to check if still connected to the network set by setProcessDefaultNetwork()

I have the following code that sets the default network of my internet connection.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
NetworkRequest.Builder builder = new NetworkRequest.Builder();
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
builder.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
NetworkCallback networkCallback = new NetworkCallback() {
#Override
public void onAvailable(Network network) {
boolean result = ConnectivityManager.setProcessDefaultNetwork(network);
Log.d(TAG, "success? " + result);
}
};
NetworkRequest networkRequest = builder.build();
connMgr.requestNetwork(networkRequest, networkCallback);
connMgr.registerNetworkCallback(networkRequest, networkCallback);
}
This works properly. However, later on, when I check using the following code:
ConnectivityManager connMgr = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfoMobile =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
networkInfoMobile.isConnected(); // returns false!
This always returns false. What am I doing wrong?
What I did is I saved the network provided by onAvailable() callback. Then later, I checked the connectivity thru:
NetworkInfo networkInfo = connMgr.getNetworkInfo(networkFromOnAvailable);
networkInfo.isConnected();
Cheers!

Android Q 10 Connecting to a wifi that i know SSID and KEY for

I am making an application where I want the user to connect to a particular wifi on click of a button. I know the SSID and KEY for the wifi network that I want to connect to. This is what I have done so far, please can someone guide me what I am doing wrong?
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = String.format("\"%s\"", ssid);
wifiConfig.preSharedKey = String.format("\"%s\"", key);
WifiManager wifiManager = (WifiManager)getApplicationContext().getSystemService(WIFI_SERVICE);
int netId = wifiManager.addNetwork(wifiConfig);
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
sorry but that method doesn't work anymore.
You need to use the NetworkSpecifier, NetworkRequest and the ConnectivityManager class.
public void ConnectToSSID(string ssid, string pwd)
{
var myNetwork = new NetworkSpecifier.Builder();
myNetwork.SetSsid(ssid);
myNetwork.SetWpa2Passphrase(pwd);
var myRequest = new NetworkRequest.Builder();
myRequest.AddTransportType(TransportType.WIFI);
myRequest.SetNetworkSpecifier(myNetwork.Build());
NetCallback callback = new NetCallBack();
ConnectivityManager manager = context.GetSystemService(Context.ConnectivityService);
manager.RequestNetwork(myRequest.Build(), callback);
}
And the NetworkCallback class, wich is similar to BroadcastReceiver class
public class NetCallback : NetworkCallback
{
public override void OnAvailable(Network network)
{
base.OnAvailable(network);
//Connected
}
}

Android Q 10 Connect to network WifiNetworkSpecifier

Since Android Q doesn't allow the WifiManager to add Networks, they gave the advise to use WifiNetworkSpecifier instead.
With the WifiNetworkSuggestionBuilder I was already able to display the notification on the statusbar, that user can join the network. But this API doesn't fulfill my requirements since that I don't the user to have to use the suggestion from the statusbar.
With WifiNetworkSpecifier I was also already able to display a popup about joining the network and the app also established a connection to the app. But that wifi connection seems only be available in the scope of the app. How is it possible to overcome this scope of the app, so other apps and for example also the browser is able to use this new established connection?
Below is my code
WifiNetworkSpecifier.Builder builder = new WifiNetworkSpecifier.Builder();
builder.setSsid("abcdefgh");
builder.setWpa2Passphrase("1234567890");
WifiNetworkSpecifier wifiNetworkSpecifier = builder.build();
NetworkRequest.Builder networkRequestBuilder = new NetworkRequest.Builder();
networkRequestBuilder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
networkRequestBuilder.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED);
networkRequestBuilder.addCapability(NetworkCapabilities.NET_CAPABILITY_TRUSTED);
networkRequestBuilder.setNetworkSpecifier(wifiNetworkSpecifier);
NetworkRequest networkRequest = networkRequestBuilder.build();
ConnectivityManager cm = (ConnectivityManager) App.getInstance().getBaseContext().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm != null) {
cm.requestNetwork(networkRequest, new ConnectivityManager.NetworkCallback() {
#Override
public void onAvailable(#NonNull Network network) {
//Use this network object to Send request.
//eg - Using OkHttp library to create a service request
super.onAvailable(network);
}
});
A bit late to the party, but maybe it will help someone else that encounters this problem.
It seems like you are right.
In android Q once the app is killed, the system automatically disconnects the WiFi network we've connected to through WifiNetworkSpecifier and there is no way to prevent the system from doing so.
The best solution I've come up with, is using WifiNetworkSpecifier with WifiNetworkSuggestion. This allows us to use WifiNetworkSpecifier while we are using our app, and suggest wifi networks for the system to auto connect to once the system disconnects from our WiFi due to the app being terminated.
Here is some sample code:
WifiNetworkSuggestion.Builder builder = new WifiNetworkSuggestion.Builder()
.setSsid("YOUR_SSID")
.setWpa2Passphrase("YOUR_PASSWORD")
WifiNetworkSuggestion suggestion = builder.build();
ArrayList<WifiNetworkSuggestion> list = new ArrayList<>();
list.add(suggestion);
WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
int status = manager.addNetworkSuggestions(list);
if (status == STATUS_NETWORK_SUGGESTIONS_SUCCESS) {
//We have successfully added our wifi for the system to consider
}
Cheers,
I tried this code its working fine
WifiNetworkSpecifier.Builder builder = new WifiNetworkSpecifier.Builder();
builder.setSsid("abcdefgh");
builder.setWpa2Passphrase("1234567890");
WifiNetworkSpecifier wifiNetworkSpecifier = builder.build();
NetworkRequest.Builder networkRequestBuilder = new NetworkRequest.Builder();
networkRequestBuilder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
networkRequestBuilder.setNetworkSpecifier(wifiNetworkSpecifier);
NetworkRequest networkRequest = networkRequestBuilder.build();
ConnectivityManager cm = (ConnectivityManager)
App.getInstance().getBaseContext().getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm != null) {
cm.requestNetwork(networkRequest, new ConnectivityManager.NetworkCallback() {
#Override
public void onAvailable(#NonNull Network network) {
super.onAvailable(network);
cm.bindProcessToNetwork(network)
}});
You must add this line for use internet. networkRequestBuilder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);

Categories

Resources