Networking Processing for Android

How to send data from Android Tablet to Processing Sketch across Wifi using Processing for Android.

This assumes you have Processing for Android working
https://android.processing.org/index.html
And you can upload a program from your laptop to your Android Tablet (Amazon Fire 7 in this case).

The aim is to use the Tablet to control data on another PC. For example: we are interested in creating a slider controller Android App which can change the background colours of programs running in a Gallery space. Allowing you to change the colours in the space when you want without having to restart programs.

You need to create two programs in Processing and then upload the server program to the Tablet.
When you go into Android mode in Processing it creates a code folder and a Manifest for you.

To send simple data across a network we can use the import processing.net.* library.
Here’s the server code.


import processing.net.*;

Server s;
float sliderval;

void setup() {
  fullScreen();
  frameRate(20);

  s = new Server(this, 12345); // Start server on a port
}

void draw() {
  background(0);

  if(mousePressed==true){
    //convert mouseX to a fraction (0.0 to 1.0)
    sliderval = float(mouseX) / width;
    s.write(sliderval + "\n");
  }

  fill(0);
  stroke(255);
  strokeWeight(5);

  rect(0,0,width,100);
  fill(128);
  rect(0,0,sliderval * width, 100);
}

The first time I tried to upload the program to the Tablet (through USB) there was an error message saying it couldn’t find the net library. To fix this you have to copy the net.jar file (a bundled up version of the library) from your Processing folder which on my system was at
C:\Program Files\processing-3.3.7\modes\java\libraries\net\library\net.jar
and put this into your code folder which is next to you program.
The next time I tried to run it another error came up

SocketException: Permission Denied

To solve this you have to change the AndroidManifest.xml file which is also next to your code.
You need too add the line to allow android.permission.INTERNET


<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="">
<uses-sdk android:minSdkVersion="17" android:targetSdkVersion="28"/>
<uses-permission android:name="android.permission.INTERNET"/>
.....
</manifest>

Now you can upload the program to the Tablet and it sets up a server with a simple slider. This is sending messages through Wifi to the client which is a program running on a laptop in my case. This needs to be on the same Wifi network and needs to point to the Server’s IP address.

You can find the IP address of the Tablet in Settings > Internet > Click on the Wifi you are using > and the IP address should be visible.
Copy these 4 numbers into your client program.

So you have the server running on the Tablet which looks like this – just one big slider.

Now run the client (In Java mode not Android mode).

The server just offers a very simple slider which sends a value from 0 to 1.0 to the client. The client use this value to change the background redness of the program.

 

Here is the Client code which runs normally in Java mode in Processing.

 


import processing.net.*;

Client c;
float sliderval = 0;

void setup(){
  size(800, 600);
  frameRate(20);
  c = new Client(this,"192.168.0.112", 12345); // Replace with your server's IP and port
}

void draw() {
  background(sliderval * 255, 0, 0);

  // Receive data from server
  if(c.available() > 0) {
    String input = c.readString();
    input = input.substring(0, input.indexOf("\n")); // Only up to the newline
    sliderval = float(input);
  }
}

Whether this works consistently is another matter. IP addresses can change as new devices come on and off a network. Previously for PC to PC networks I have fixed IP addresses on server and client so there is no problem. I couldn’t see an easy way to do this on a Tablet. But for this simple test it works fine on a local Wifi network.