We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi everyone, I would like to know if its possible to force connect the phone to a pre defined network using any existing wifi libraries? I made a small app that sends accelerometer data to the server through udp but works only if both devices are connected to the same network, on the app side I would like to pre define a network that application automatically connects. Thanks for your help already and Happy New Year! :)
Answers
@poYo===
imports:
Not sure to understand exactly what you want... Yet it seems to me that, after having added permissions (internet, wifi state, wifi change), verified that wifi is enabled and opened one connexion from your phone to your network you can:
create an instance of WifiManager;
create an instance of WifiConfiguration;
add to it your password and the name of the network using android.net.wifi.WifiConfiguration.KeyMgmt;
list all enregistered networks and create a loop for the list; your network will be listed among others.
connect to this one that has the name you have choosen (before doing that disconnect from the others)
The idea is, I have an Installation, a game that uses the phone as a controller. I don't want the participant to look for the network and connect to it manually. If I can solve it on the application side, that would make the things easier for me. So the final goal would be, the participant downloads the app from the store, click's connect and the app automatically finds the predefined network and connects. I made a simple version while ago that sends the accelerometer and screen activity to the server. But it was just a prototype.
So that would be something like this?
@poyo==
yes, something like that; as soon as possible i ll give a look; as for now what i can say is that i have coded this kind of things with P5...Don't dispair!
@Poyo===
more precisely, code snippet:
But this only connects if the network is known or ? I simplified the code a bit to test and on console it was listing the known networks. What if I want it to connect to a network which is not know for the device?
here is the code that I edited.
Or is it because of some permission mistake I made?
@poyo===
yes: this is the code you have given:: i adapted it to ¨P5, that s all!!!- now , if you want to connect to any networks found (scan result!!!) try that....
Now I get the error "The Global Variable "networkId" does not exist" :S
@PoYo===
normal: scan does not know nothing about networkID; netWorkID is known when you list configured networks...So= you have firstly to scan and get a list of wifi spots, registred or not registered; then you create a new configuration for the network you want to use, add it to the registered list and finally use the first code i have given there.
I'll start bashing my head :D
So I tried some stuff but now I get an error that I don't understand at all...
new code :
WiFiList class
@Poyo===
of course: you never initialize your context! && BTW: unregister your receiver because without that it will run undefinitely...
on line 67 wifi list class I had a silly mistake, tnx for pointing out but I didn't understand the unregistering receiver part also now the app doesn't give error but still doesn't connect to unknown network
@PoYo===
i ll give a look at that but i am quite sure that your scan is never completed because your receiver is never unregistered and so on connect cannot run!
Any luck ? cuz I am still stuck in same point :D starting to hate this :D
@PoYo===
try this=== BroadcastReceiver monRecepteur;
then unregister also in onPause() &&& onStop() &&& onDestroy() (adding a condtion: if(monRecepteur !=null){}
gives an error - Cannot refer to the non-final local variable monRecepteur defined in an enclosing scope -
can you somehow implement it to the code and send that way? I know its too much to ask but I am already super confused with every single change :D
Judging by the fact there's no datatype before monRecepteur where it's being initialized, much probably it has been declared someplace else. L-)
So there's a real chance monRecepteur is a field, rather than a local variable, which has been declared, but not initialized yet, at the top of the sketch. :\">
However, Java surprisingly can accept local variables or parameters to be accessed inside classes and lambdas created inside a function/method, as long as they're declared as
final
too. <:-POr since Java 8, that also works if they can be determined as effectively
final
, even though not explicitly declared as such. :ar!For those who also know JS, accessing variables defined in an enclosing outer function from within enclosed inner functions is called closure. B-)
Tnx GoToLoop, fixed the error cuz I realized I was registering the monRecepteur in inner scope, yet still doesn't connect to the network :D
when tapped, scans the networks, prints them on the screen than registers the pre defined network in the configured networks but doesn't connect to it.
current code
@PoYo=== of course you have to declare monRecepteur as a global, instance of BroadcastReceiver...
well even so still doesn't work :D
@PoYo===
put your "new" code; what does not work???what error???
Error still remains the same, scans the network, lists them yet doesn't connect. I am using WPA/WPA2 security.