We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi everyone, im trying to scan al available networks, and with the help of some other post i have the following code:
import java.util.List;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.app.Activity;
Activity act;
Context context;
WifiManager wm;
List<ScanResult> results;
WifiInfo wifiInfo ;
boolean primeraVez = true;
void setup() {
fullScreen();
act = this.getActivity();
context = act.getApplicationContext();
}
void draw() {
textSize(height/18);
if (primeraVez) {
primeraVez = false;
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
wifiManager.startScan();
List<ScanResult> result = wifiManager.getScanResults();
println(result);
}
}
the intention was to print all the available networks but i'm only obtaining an empty array "[]" over the console, what am i doing wrong?
the code may have some useless parts
Answers
@erwrow===
What permissions are you using?
startScan does not return immeditaley but asynch.: you have to use a broadcast receiver as i have explained in this forum. add it a filter: SCAN_RESULTS_AVAILABLE_ACTION
you have also to release it after using
see what android docs are saying:
https://developer.android.com/reference/android/net/wifi/WifiManager.html#startScan()
@akenaton
1) I'm using the following permissions:
2) Ok, i'll try to find more information on broadcast receivers (i've no idea what they are).
Ok, so i know that this code is sloppy, but i came up with this
and when i try to run it the console throws the following "fatal exception"
i have no idea what went wrong
&erwrow===
line 21 you initialize your WifiManager with "context" that is only initialized line 29: null object reference.
you're right, now i have this:
but still prints an empty list "[]" (no errors this time)
@erwrow===
after line 51 try to add a boolean: if(result.length>0)...before segundo: because in your code it returns even when nothing was scanned...
ok, I added it, but it shows nothing. It's like there's no wifi network
@ erwrow===
code snippet tested: it works (android 6.X) dont forget - 4 permissions (accessCoarseLocation, wifiState, wifiChange, netWorkstate) (i think now that you have not added these permissions, verify it!)
unregister your receiver onPause(), onStop(), on Destroy()
Sorry for the delay in answering. This still doesn't work, i might think that the problem is
<a href="/two/profile/Override">@Override</a>
in line 51, it trhows an error "unexpected token: href", so i commented it. About the permissions, i added everything that says wifi and more. i'm testing it on android 7.x
@erwrow That is just a bug when posting in this forum. You should replace the line above with
@ Override
and make sure you remove the space character after the ampersand (@). Check this link for further info: https://docs.oracle.com/javase/tutorial/java/annotations/predefined.htmlKf
@erwrow===
of course, this is just a typo from the forum....@kfrajer is right
Ok, i've changed that, but it still doesn't work. (and i have all the permissions)
@erwrow===
put your manifest file; i have tested the code i put, it works
well, after some research i've found the "error", (at least in my case) you have to go to settings->app->(name of the app)->permisions and enable the location permisions, then you have to enable the gps and then voila! it works jaja. it's a very odd bug.
so, now i need to automatically enable the location permises
Or, do you have any idea on how to solve that?
@erwrow===
put answered for others
as for permissions that s why i have told you in my previous post to put your manifest; since android 6 you have to deal with " dangerous" permissions listed in the manifest on runtime. They never are automatically granted, because the user can change his mind.
here for more details: https://developer.android.com/training/permissions/requesting.html
in resume, to scan the available wifi networks you need the following code:
with the permissions: accessCoarseLocation, wifiState, wifiChange, netWorkstate and accessFineLocation (this last one is sugested by this page), in some devices you need to enable the location to get the available networks.
Remember that in line 53, where it says:
<a href="/two/profile/Override">@Override</a>
it actually says @Override