I have a simple IntentService which is started with startWakefulService. The service has a for loop which gets the RSSI every 3000ms, as such:
for (int i=0; i<5; i++) {
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if (wifiInfo != null && wifiManager != null){
Log.i(TAG, "wifiInfo, RSSI: " + wifiInfo.getRssi());
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
}
While the device is sleeping (screen is off) the method WifiInfo.getRSSI always returns the same value (example: -58) even when I move the device around.
Can anyone help me?
Related
According to a lot of official documents also stackoverflow documents, I am trying to use wifimanager to get list of access points near me but that is not working. I want to tell you I tried to use below code in android 4.4 but right now not working in android 9. Have you any solution?
final WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
//****************** Enable Wifi And Connect To Hidden Access Point
if(wifiManager.getWifiState() != WifiManager.WIFI_STATE_ENABLING){
wifiManager.setWifiEnabled(true);
wifistate = true;
}
// Start lengthy operation in a background thread
new Thread(new Runnable() {
int count = 0;
public void run() {
while (wifiManager.isWifiEnabled() == false && count < 5) {
try {
// Here I'm making thread sleep to show progress
Thread.sleep(1000L);
count++;
} catch (Exception e) {
}
}
if (wifiManager.isWifiEnabled() == true) {
//
wifiManager.startScan();
List<ScanResult> wifiScanList =wifiManager.getScanResults();
i want to make 3g works when wifi is enabled! Stop for a while wifi and connect through 3g. after that close 3g and work with wifi again! thanks!
Just disabling Wi-Fi? Both processes are automatic: connecting a Wi-Fi network when it is enabled and getting 3G when there isn't a Wi-Fi connection.
I don't think it is possible to switch to mobile data network when WiFi is enabled.
Perhaps you should silently disable the WiFi (from code) and wait for the 3G network to establish. Once your required task is done, turn the WiFi on again and your 3G network will be disconnected.
I think the following should work...
WifiManager wm = null;
WifiManager.WifiLock wfl = null;
// To disable wi-fi
wm = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
if (wm != null) {
wfl = wm.createWifiLock(WifiManager.WIFI_MODE_SCAN_ONLY, "myWifiLock");
Boolean result = wm.setWifiEnabled(false);
Log.d(TAG, "wm.setWifiEnabled(false) result: " + result);
}
// To re-enable wi-fi
if (wfl != null)
wfl.release();
if (wm != null) {
Boolean result = wm.setWifiEnabled(true);
Log.d(TAG, "wm.setWifiEnabled(true) result: " + result);
}
here is a code that can enable and dissable inside your code!
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
Runnable runnable = new Runnable() {
// ginetai o kwdikas gia to anoigma kai to klisimo tou wifi
#Override
public void run() {
// new LongOperation().execute();
for (int i = 0; i <= 150; i++) {
try {
Thread.sleep(1000);
if (i >= 0 && i <18){
wifi.setWifiEnabled(true);
// wifi.setWifiEnabled(false);// Disabling WiFi
}
else if (i>= 18 && i < 40){
wifi.setWifiEnabled(true);
// wifi.setWifiEnabled(false); // Enabling WiFi
}
else if (i>= 40 && i < 58){
// wifi.setWifiEnabled(true); // Disabling WiFi
wifi.setWifiEnabled(false);
}
else if (i>= 58 && i < 78){
wifi.setWifiEnabled(true);
//wifi.setWifiEnabled(false); // Enabling WiFi
}
else{
// wifi.setWifiEnabled(true);
wifi.setWifiEnabled(false);// Disabling WiFi
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
new Thread(runnable).start();
I am trying to write some code that will disconnect current wifi network (if any) and reconnect to a specific wifi network with known SSID.
I have been following the code used here; How do I connect to a specific Wi-Fi network in Android programmatically?
which works but the connection takes several seconds, upto about 10 seconds.
Specifically, I use the code as follows;
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
WifiConfiguration config;
I obtain the config, whether by creating a new one and setting the SSID and KeyMgmt to NONE and then adding it;
wifiManager.add(config);
or by getting a config that already exists;
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
config = i;
break;
}
}
Then I call;
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
I have a broadcast received checking the wifi state and when i get a connected for my correct SSID i can continue, however, this process takes upto 10 seconds, how can i set up the config or wifimanager to connect to this much quicker?
Thanks
I think this code is help to you..
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.network_test);
context = this;
mUpdate = new UpdateTimeTask();
mHandler = new Handler();
mHandler.post(mUpdate);
}
public Boolean isNetAvailable(Context con) {
try{
connectivityManager = (ConnectivityManager) con.getSystemService(Context.CONNECTIVITY_SERVICE);
wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
mobileInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(wifiInfo.isConnected() || mobileInfo.isConnected()) {
return true;
}
}catch(Exception e){
e.printStackTrace();
}
return false;
}
private class UpdateTimeTask implements Runnable{
public void run() {
boolean net = isNetAvailable(context);
if(net != false) {
Toast.makeText(getBaseContext(), "network Available", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getBaseContext(), "network Not Available", Toast.LENGTH_SHORT).show();
}
mHandler.postDelayed(mUpdate, 30000);
}
}
}
Have you tried adding startScan() to your routine to force an immediate rescan for available networks at the time you wish to connect? I imagine forcing that command repeatedly with an alarmManager or something similar is possible, but I would think that has the potential to have an expensive performance/battery impact. If you have a specific trigger, it could be a solution.
See here: http://developer.android.com/reference/android/net/wifi/WifiManager.html#startScan()
I am writing an application to display the WiFi network types and status. How do I find all the "not in range" WiFi networks? Is it possible to get the list of all configured (previously seen) WiFi networks that are out of range?
I used the below code to get the result
WifiManager mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
List<ScanResult> results = mWifiManager.getScanResults();
if (configs != null) {
for (WifiConfiguration config : configs) {
for (ScanResult result : results) {
if (result.SSID == null || result.SSID.length() == 0) {
continue;
}
else {
if (result.SSID.equals(MyString.removeDoubleQuotes(config.SSID))) {
int level = mWifiManager.CalculateSignalLevel(result.level, 4);
Log.d("MyApp", Config.SSID + " " + level);
}
}
}
}
}
But if configured network is high in number then it will take long time to execute. Is there any way to optimize this problem? By getting the scanned result of only the configured network.
How about subtracting the wifi networks in range from all wifi networks?
My Android app can only function with WiFi connected to the Internet. Thus, I use the following code to check if the device is connected:
ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Activity.CONNECTIVITY_SERVICE);
boolean wifi = conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
However, very often when the application is launched and WiFi connected to the Internet, I get the notification that is only shown when wifi = false. Have I missed something, or the check is not that accurate?
My project also relies on Wifi (although I use a private network). The following is my code for setting up a Wifi connection on start up:
private void initWIFI (WifiManager wifiMgr, String SSID, String key)
{
WifiInfo curr;
if (null == (curr = wifiMgr.getConnectionInfo())) // Get current wifi state
{
joinNetwork (wifiMgr, SSID, key);
}
else switch (curr.getSupplicantState())
{
case DISCONNECTED:
case DORMANT:
case INACTIVE:
case SCANNING:
joinNetwork (wifiMgr, SSID, key);
break;
default:
if (!curr.getSSID().equals (SSID))
joinNetwork (wifiMgr, SSID, key);
}
while (wifiMgr.getConnectionInfo().getIpAddress() == 0)
{
try
{
Thread.sleep (1000);
}
catch (Exception e)
{ }
}
}
/**This method is used to join the proper WiFi network when necessary. Normally,
* the Android retains network configuration and it is not necessary to manually
* re-join the desired network on software startup. However, when it is determined
* that the Android is not currently attached to the proper network, this function
* is used to correct that situation. */
private void joinNetwork (WifiManager wifiMgr, String SSID, String key)
{
try
{
WifiConfiguration wc = new WifiConfiguration();
wc.allowedAuthAlgorithms.set (WifiConfiguration.AuthAlgorithm.OPEN);
wc.allowedAuthAlgorithms.set (WifiConfiguration.AuthAlgorithm.SHARED);
wc.allowedGroupCiphers.set (WifiConfiguration.GroupCipher.WEP40);
wc.allowedGroupCiphers.set (WifiConfiguration.GroupCipher.WEP104);
wc.allowedKeyManagement.set (WifiConfiguration.KeyMgmt.NONE);
wc.allowedPairwiseCiphers.set (WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedPairwiseCiphers.set (WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedProtocols.set (WifiConfiguration.Protocol.WPA);
wc.allowedProtocols.set (WifiConfiguration.Protocol.RSN);
wc.hiddenSSID = false;
wc.priority = 32;
wc.SSID = "\"" + SSID + "\"";
wc.status = WifiConfiguration.Status.ENABLED;
wc.wepKeys[0] = key;
wc.wepTxKeyIndex = 0;
int netID;
if (-1 == (netID = wifiMgr.addNetwork (wc)))
{
listener.lostConnection (true);
}
else
{
wifiMgr.enableNetwork (netID, true);
Thread.sleep (5000); // Delay to allow the DHCP process to work
}
}
catch (Exception e)
{
listener.lostConnection (true);
}
}
It should be pointed out that I always use the same wireless access point, and the code in joinNetwork() is specifically configured for it, so if your configuration needs to be more flexible, then your solution may be more complex. Sadly, I do not remember the web site where I found the starting point for this code, but it didn't take a ton of Googling to find it. Finally, I'm pretty sure your application needs to have the ACCESS_WIFI_STATE and CHANGE_WIFI_STATE permissions.
I use code like this:
public static String getCurrentSsid(Context context) {
final WifiInfo wifiInfo = getCurrentWifiInfo(context);
if (wifiInfo != null && !StringUtil.isBlank(wifiInfo.getSSID())) {
return wifiInfo.getSSID();
}
return null;
}
public static WifiInfo getCurrentWifiInfo(Context context) {
final ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkInfo != null && networkInfo.isConnected()) {
final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
return wifiManager.getConnectionInfo();
}
return null;
}
At the same time be aware of this two issues 19078 and 3641.