Loading...
Processing Forum
Recent Topics
All Forums
Screen name:
ovicin
ovicin's Profile
1
Posts
0
Responses
0
Followers
Activity Trend
Last 30 days
Last 30 days
Date Interval
From Date :
To Date :
Go
Loading Chart...
Posts
Responses
PM
Show:
All
Discussions
Questions
Expanded view
List view
Private Message
Android Processing connect to wifi programatically
[0 Replies]
04-Sep-2013 03:50 AM
Forum:
Android Processing
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)
Here is the code that I used (the SSID and password were removed)
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
«Prev
Next »
Moderate user : ovicin
Forum