I didn’t say that particular library wouldn’t work. I’ve never tried it. I just said many libraries don’t work with P4A. Maybe ask the developer(s), if it’s still supported by anyone.
Thank you for your help, a second step is done!
Thanks to this discussion, I was able to import the class Client into Android Studio with my processing sketch!
The build is successfull but I still have problems to make it work:
My phone and my computer are on the same wifi network. The phone is also linked to the computer with an USB cable to transfer the application from AS.
I have this processing sketch on the server side running on my computer:
import processing.net.*;
import java.net.InetAddress;
Server myServer;
int val = 0;
InetAddress inet;
String myIP;
void setup() {
size(200, 200);
// Starts a myServer on port 5204
myServer = new Server(this, 5204);
try {
inet = InetAddress.getLocalHost();
myIP = inet.getHostAddress();
}
catch (Exception e) {
e.printStackTrace();
myIP = "couldnt get IP";
}
println(myIP);
}
void draw() {
Client thisClient = myServer.available();
// If the client is not null, and says something, display what it said
if (thisClient !=null) {
String whatClientSaid = thisClient.readString();
println(thisClient.ip());
if (whatClientSaid != null) {
println(thisClient.ip() + "\t" + whatClientSaid);
}
}
}
When launching this sketch, the IP adress returned after setup() is 169.254.123.61 which is the IPadress of the ethernet card. The IP adress of my computer on the wifi is 192.168.1.78
Is it normal?
I have however the same behaviour with both adresses when declaring the client in the sketch on AS…
On the client side, I run this sketch on my mobile phone with AS:
public class Sketch extends PApplet {
Client cl;
{
cl = new Client(this, "192.168.1.78", 5204);
}
public void settings() {
size(600, 600);
}
public void setup() {
}
public void draw() {
if (mousePressed) {
ellipse(mouseX, mouseY, 50, 50);
textSize(12);
text(mouseX+" - "+mouseY, 10,10);
cl.write('a');
}
}
}
The app is installed and launched with no problem on the phone, but these error lines show on AS console as soon as I touch the phone screen to send the data from the Client object.
V/MiuiFrameworkFactory: get AllImpl object = android.common.MiuiFrameworkFactoryImpl@768c7a4
W/MirrorManager: this model don’t Support
I/System.out: pb avec write!!!
W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method ‘void java.io.OutputStream.write(int)’ on a null object reference
W/System.err: at androidstudio.tutorials.Client.write(Client.java:683)
W/System.err: at androidstudio.tutorials.Sketch.draw(Sketch.java:24)
W/System.err: at processing.core.PApplet.handleDraw(PApplet.java:1895)
W/System.err: at processing.core.PSurfaceNone.callDraw(PSurfaceNone.java:478)
W/System.err: at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:518)
I have just understood that an exception was thrown by the “void write” function of the Client (because of the println(“pb avec write!!!”) I added after the “catch(Exception e)” of this function.
But found nothing useful with the lines:
V/MiuiFrameworkFactory: get AllImpl object = android.common.MiuiFrameworkFactoryImpl@768c7a4
W/MirrorManager: this model don’t Support
Do you have an idea of what I could do?
Hi Sir
Best way to know when you try even you didn’t get the expecting results but that drivers you investigate issues by your self
The attempt generally sharpens skills and enables a deeper judgment of events
I wish you well
Whatever. I just wanted to clarify what I had said and correct any misunderstanding on your part. I have no interest in using this library myself, I was just trying to help.
Good luck with your problems.
I am very sorry I thought that I was talking with @Glenton didn’t notice
Yes Sir you are true some libraries include issues with updated versions of processing
@svan good afternoon
Look at this example and if you have 4.1.2 /4.1.1processing try it some tips in the link
It runs ok on the Android side; no errors. Slider works as expected. On the Java side I get the error below. I’m not certain that I have the IP and port set correctly. Any idea where I could find this information for a Mac? I think I have the IP address, but not sure about the port. Thanks for tracking this down. Moving the .jar file into the Code folder was the key.
@jafal Sir, be sure that I test, try and investigate as hard as I can!
My skills with java, networking through wifi and Android are poor, but I allready have learnt a lot since the beginning of this discussion by trying and investigating but also thank to you, svan and David.
I made one more step thank to the last example you posted: with adding the internet permission in the AndroidManifest.xml.
Now everything works OK with the emulator! I can read an write wrom both server and client side.
With my smartphone, I still have the two error lines with V/MiuiFrameworkFactory and W/MirrorManager, but actually these lines show even without using the processing.net librairie.
The problem now is that for some reason, the client on the phone fails to connect to the server on the PC. After some seconds this message appear:
W/System.err: java.net.ConnectException: failed to connect to /169.254.123.61 (port 5204) from /:: (port 42588): connect failed: ETIMEDOUT (Connection timed out)
I wonder what is the port 42588 the message mention?
I tried to change the timeout value in the Client constructor with “socket.setSoTimeout(x)” with various x values but this doesn’t seem to even change the time the error message takes to appear.
On a Mac I can determine the IP address and port on the client side with the android server running using this Terminal command: sudo lsof -i -n -P | grep TCP
. After doing that and entering the correct values for IP address and port I no longer get an error on the Java (client) side. However, the window background doesn’t change when I pull the slider on the Android side (server).
I get the idea that the port changes every time you go to use it, so you may need to check it each time. I used this reference for the Mac and there is a section for Windows:
https://help.extensis.com/hc/en-us/articles/360010122594-Identifying-ports-in-use-on-macOS-and-Windows
For whatever it’s worth, adding net.jar to the ‘code’ folder of the android test that you asked us to run now does so without error with import processing.net.*;
as the first line.
Runs without error:
import processing.net.*;
void setup() {
fullScreen();
noStroke();
fill(0);
}
void draw() {
background(204);
if (mousePressed) {
if (mouseX < width/2) {
rect(0, 0, width/2, height); // Left
} else {
rect(width/2, 0, width/2, height); // Right
}
}
}
If net.jar is added to code folder:
Thank you for your efforts to follow the issue to the end
Thanks for your contributions; couldn’t have done it without your efforts. But we aren’t home yet. The screen on that java app has to change background color when we pull the slider on the android app. For some reason c.available() returns zero.
Hello,
Last step done, it works!
I have now a processing sketch on my phone that communicates with a processing sketch on my PC!
Thank to the netstat command svan lead me to and ipconfig, I finally figured out what was wrong. I just had to plug and unplug different network connections to find what was the good ip adress to use in the client sketch…
To summarize what I did:
-For the problem with running an app on a smartphone using processing, the problem was solved with using Android Studio and following this tutorial.
-For the problem with using the client class of the library processing.net with processing on Android Studio, the solution was to:
- create a new java class named “Client” (next to the processing sketch and MainActivity, in the same folder. I easily did it from AS).
- Copy/paste modified client code from this discussion in this new java class
- Add the line (replace “][” by ><) [uses-permission android:name=“android.permission.INTERNET”] in the AndroidManifest.xml file
Then be careful with the ip adress set in the client sketch. Using ipconfig and netstat from a cmd terminal can help a lot!
Thanks for this information.
I don’t fully understand this but probably adding the net.jar file to the code folder is equivalent to what I did with adding it as a new java class in my AS project…
Congratulations on your accomplishment. You put in a lot of hard work on this project, and hung in there until completion. Persistence paid off and you’re to be commended for that; well done. I learned a few things also. Thanks for posting.
Possibly some connection. I’m not sure how putting the class in a .jar file changes things. Also Android apps have a ‘code’ folder while java apps do not.
While it’s fresh on your mind, I would encourage you to create a simple AS demo of your work and post it on github or in the Processing gallery so that others can benefit from your work. It wouldn’t have to be anything fancy but just show how the connections are made between server and client and possibly pass a string from one to the other.
Hello,
Thank you svan,
Good idea, the demo. I have posted an example on gitHub according to your advice.