Android Processing connect to wifi programatically
in
Android Processing
•
1 month ago
I am trying to connect to a wifi network programatically but my application crashes with the following error:
- FATAL EXCEPTION: Animation Thread
- java.lang.NullPointerException
- at processing.test.sketch_130903a.sketch_130903a.connect_to_wifi(sketch_130903a.java:54)
- at processing.test.sketch_130903a.sketch_130903a.setup(sketch_130903a.java:75)
- at processing.core.PApplet.handleDraw(Unknown Source)
- at processing.core.PGraphicsAndroid2D.requestDraw(Unknown Source)
- at processing.core.PApplet.run(Unknown Source)
- at java.lang.Thread.run(Thread.java:1019)
- import android.net.wifi.*;
- import android.content.*;
- import java.util.*;
- Context context;
- void connect_to_wifi(){
- //create wifi connection
- String networkSSID = "";
- String networkPass = "";
- WifiConfiguration conf = new WifiConfiguration();
- conf.SSID = "\"" + networkSSID + "\""; // Please note the quotes. String should contain ssid in quotes
- //For WPA network you need to add passphrase like this:
- conf.preSharedKey = "\""+ networkPass +"\"";
- //Then, you need to add it to Android wifi manager settings:
- WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
- wifiManager.addNetwork(conf);
- //And finally, you might need to enable it, so Android conntects to it:
- List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
- for( WifiConfiguration i : list ) {
- if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
- wifiManager.disconnect();
- wifiManager.enableNetwork(i.networkId, true);
- wifiManager.reconnect();
- break;
- }
- }
- }
- void setup() {
- size(480,800);
- noStroke();
- fill(255);
- rectMode(CENTER); // This sets all rectangles to draw from the center point
- connect_to_wifi();
- }
- void draw() {
- background(#FF9900);
- rect(width/2, height/2, 150, 150);
- }
In the sketch properties the permissions for ACCESS_WIFI_STATE and CHANGE_WIFI_STATE are set.
Any sugestions on where is the problem?
Thanks
1