How to get WIFI to turn on during POWERUP? - android

For a long time my device would always auto start WIFI now it does not. Could someone show how to turn wifi on automatically when device boots? Also if my app sleeps sometimes on recovery wifi is gone? Show how to test if wifi is active and if not active how to turn it on programatically. Thanks

Use wifi manager class and use the following code to test wifi connectivity
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if(wifi.isWifiEnabled())
{
// write your code
}else{
wifi.setWifiEnabled(true);
}

Related

Android WifiManager disconnect from a WifiSpot but never connects automatically again

I am using this code to disconnect from a Wifispot:
if(getActivity()!=null) {
WifiManager wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
wifiManager.disconnect();
}
WiFi disconnects and Settings show Wifi On, the wifi I was connected is shown as
Saved, secured with WPA/WPA2
That's perfect. The problem is that I get out of range and return into the range again of the WiFi, it doesn't "reconnect" automatically. Which code should I add to automatically reconnects WiFis disconnected by WifiManager?
When you set up your wifi configuration.
add this :
mWifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
for more information http://developer.android.com/intl/es/reference/android/net/wifi/WifiConfiguration.html
Hope this help!

Find available wifi connections in Android

I am trying to create a help wizard to recover from bad network connections in the app. One test case I hope to handle is the case where an end user has WiFi turned off, WiFi is available, and the mobile network is slower than the WiFi network. In this event, I want to be able to (1) discover the available WiFi network s, (2) find the WiFi network speed, (3) Compare its speed to the mobile network speed, (4) digest the user changes to the faster network.
For this to work, I need to know how to programmatically get information on available connections. Is that something we can do? If so, how can we tell what connections are available? Thanks in advance.
Task-1: Discover the available WiFi network
This can be done by getting WifiManager's instance from the System.
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiManager.getScanResults();
// The above is an async call and will results are available System will broadcast `SCAN_RESULTS_AVAILABLE` intent and you need to set a `BroadCastReceiver` for it.
// And get the results like this
List<ScanResult> results = wifiManager.getScanResults();
Task-2&3: find the network speed
This link gives an answer to your question about how to get network speeds of wifi and mobile network
Wifi:
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
int linkSpeed = wifiManager.getConnectionInfo().getRssi();
In case of mobile it should work:
TelephonyManager telephonyManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
CellInfoGsm cellinfogsm = (CellInfoGsm)telephonyManager.getAllCellInfo().get(0);
CellSignalStrengthGsm cellSignalStrengthGsm = cellinfogsm.getCellSignalStrength();
cellSignalStrengthGsm.getDbm();
Then You should compare this signal levels and if WIFI signal is better keep it turn on, but if mobile is better disconnect wifi
Task-4: Switching to the option with higher speed
In Android by default, if wifi is on and connected then your mobile network won't be used. Hence to use mobile data you must either disconnect from all available wifi-networks or switch off the wifi.
I will also suggest you to read link this, this and this for getting more information on how to get connection speed.

Android WifiLock WIFI_MODE_SCAN_ONLY not working

I'm trying to block the wifi connections. I want my application to turn on the wifi, but does not connect to any network that is already stored on the smartphone. But even after I use the SCAN_ONLY mode, he continues to connect to networks that already "know".
.....
wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
.....
wifiManager.setWifiEnabled(true);
WifiLock scanOnly = wifiManager.createWifiLock(WifiManager.WIFI_MODE_SCAN_ONLY, "scanOnly");
scanOnly.acquire();
Already in despair i tried to disconnect after to make sure that the state is WIFI_STATE_ENABLED wifi. That the app can not connect for a few seconds, but after some time it connects to a network in the same ...
wifiManager.setWifiEnabled(true);
....
WHEN (WIFI STATE == WIFI_STATE_ENABLED)
{wifiManager.disconnect();
scanOnly.acquire();}
Can someone help me?
Tks
WifiLock not for this. Only for "Allows an application to keep the Wi-Fi radio awake". This does not forbid anyone to connect to wifi. Only says "I want to be able to do at least this in sleep mode".
You have to do what you said Peanut above: get the list of all the stored networks and then disable wifi in BroadcastReceiver.
#Override
public void onReceive(Context c, Intent intent) {
saveWifiList(mainWifi.getScanResults());
unregisterReceiver(this);
mainWifi.setWifiEnabled(false);
showWifiList();
}
One way would be to get the list of all the stored networks and then disable them. You might want to store their state somewhere so you can restore the initial state after you're done.

Android authentication gets in deadlock when using EAP with PEAP

I've spent several days trying to figure out what is wrong with my android phone's wifi at school. The school wifi is 802.1x EAP using PEAP..requires username, password.
I've had two Android phones, and have run many different ROMs on them so I know this isn't just a ROM specific problem. I'm currently running 2.2.1 on a Mytouch 3g fender ed. I don't have a data plan with my phone so I keep data off. I only use wifi.
The problem I have is that after I connect to the wifi, everything is fine for a while, but soon (perhaps 15 minutes to an hour later), I'll turn my phone on and when I see the wifi connection, it says Authenticating and it stays stuck there till I reset it. If I try to surf the Internet with normal HTTP it works fine, but if I try to run any apps that require wifi connection, they give an error saying there is a network connection (example would be Google Voice search which will say "Connection Problem"). Feeds can't update, gmail won't update, etc. After I reset the connection, the wifi works perfectly again. I am not sure why this happens. I think it has something to do with how the school's wifi is set up.
To make a quick fix to the problem however, I'm writing an app that will test if the connection is authenticating and reassociate the connection if it is. I'm running into problems though.
Here is the code to see if the wifi is authenticating and also some debug output:
private void checkWifi() {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
SupplicantState state = info.getSupplicantState();
Log.d(getClass().getSimpleName(), state.toString());
NetworkInfo.DetailedState state2 = WifiInfo.getDetailedStateOf(info.getSupplicantState());
Log.d(getClass().getSimpleName(), state2.toString());
}
And here's the log output:
D/WifiCheckerService(24732): COMPLETED
D/WifiCheckerService(24732): OBTAINING_IPADDR
The log output is the same no matter if the wifi on the phone says Connected or Authenticating which means my program as of now cannot distinguish if it is in the funky "authentication" state or not. I need to be able to distinguish in order to be able to know when to reset the wifi. Any help is greatly appreciated.
Try this.
ConnectivityManager conn = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo nInfo = conn.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
Log.d("MY CONNECTIVITY MANAGER: NETWORK.DETAILEDSTATE", nInfo.getDetailedState().toString());
Log.d("MY CONNECTIVITY MANAGER: NETWORK.STATE", nInfo.getState().toString());
Here's my Output (If you entered a wrong password while connecting).
D/MY CONNECTIVITY MANAGER: NETWORK.DETAILEDSTATE(24732):
AUTHENTICATING D/MY CONNECTIVITY MANAGER: NETWORK.STATE(24732):
CONNECTING
Second Output (While obtaining IP).
D/MY CONNECTIVITY MANAGER: NETWORK.DETAILEDSTATE(24732): OBTAINING_IPADDR
D/MY CONNECTIVITY MANAGER: NETWORK.STATE(24732): CONNECTED
Third Output (Successfully Connected).
D/MY CONNECTIVITY MANAGER: NETWORK.DETAILEDSTATE(24732): CONNECTED
D/MY CONNECTIVITY MANAGER: NETWORK.STATE(24732): CONNECTED
4th Output (If you entered wrong password and failed to connect).
D/MY CONNECTIVITY MANAGER: NETWORK.DETAILEDSTATE(24732): DISCONNECTED
D/MY CONNECTIVITY MANAGER: NETWORK.STATE(24732): DISCONNECTED
5th Output (If WIFI is IDLE and DISCONNECTED).
D/MY CONNECTIVITY MANAGER: NETWORK.DETAILEDSTATE(24732): IDLE
D/MY CONNECTIVITY MANAGER: NETWORK.STATE(24732): DISCONNECTED
// hope it helps

How to keep my wifi always turn on?

While disconnectiong to the selected wifi AP, my WiFi is turned off.I want to keep my WiFi always turn on while disconnecting to the the selected AccessPoint and in the meantime WiFi is n't trying to connect to other AP also.Iam using Android 1.5.Is there any solution for this?
Regards,
Rajendar
mWifi = ((WifiManager)getSystemService( WIFI_SERVICE )).createWifiLock( WifiManager.WIFI_MODE_FULL , "yourtag" );
mWifi.acquire();
// when done
mWifi.release();

Categories

Resources