Android 10 Connect wifi programmatically - android

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.

Related

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 10 don`t connect to WPA wifi programmatically

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?

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);

How to know if wifi is connected with new Android Lollipop wifi management

I need to connect my mobile by wifi to a connected device. This hot spot have not internet.
Before 5.0 I did like this to check if I was connected to the wifi
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(mWifi.isConnected()&& wifiManager.getConnectionInfo().getNetworkId()==addedNetwork)
But now with the 5.0 version of android, when I check isConnected it always returns false because of the none connectivity to internet.
How can I manage this case ?
Regards
I haven't tried this out but as per the google docs this should work
ConnectivityManager cManager = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder = new Builder();
//Indicates that this network should be able to reach the internet.
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
NetworkRequest build = builder.build();
cManager.requestNetwork(build, new NetworkCallback() {
#Override
public void onAvailable(Network network) {
super.onAvailable(network);
// do what you want to do here
}
});`

Categories

Resources