I am trying to use OSC to communicate between a desktop computer (OSX) and an Android phone, which are both connected to the same wifi network. I'm working in Processing with the oscP5 library but I think eveything is fine in the code - this is, I think, an address issue or permissions Android side.
Here is my computer/desktop code:
import oscP5.*;
import netP5.*;
OscP5 oscP5;
NetAddress myRemoteLocation;
int colour = 255;
void setup()
{
size(600, 600);
/* start oscP5, listening for incoming messages at port 1200 */
oscP5 = new OscP5(this,1200);
println(this);
/* the address of this device */
myRemoteLocation = new NetAddress("127.0.0.1",1300);
}
void draw()
{
background(0);
fill(colour);
circle(width/2, height/2, 200);
int i = int(random(0, 200));
if (i == 1) sendmessage();
}
void sendmessage()
{
/* create the message */
OscMessage myMessage = new OscMessage("/one");
color c = color(random(0, 255), random(0, 255), random(0, 255));
myMessage.add(c);
/* send the message */
println("Sending message: " + myMessage);
oscP5.send(myMessage, myRemoteLocation);
}
/* incoming osc message are forwarded to the oscEvent method */
void oscEvent(OscMessage theOscMessage)
{
colour = theOscMessage.get(0).intValue();
}
This runs without issue and gives this in the console:
### [2024/11/7 17:12:20] PROCESS @ OscP5 stopped.
### [2024/11/7 17:12:20] PROCESS @ UdpClient.openSocket udp socket initialized.
### [2024/11/7 17:12:21] PROCESS @ UdpServer.start() new Unicast DatagramSocket created @ port 1200
### [2024/11/7 17:12:21] PROCESS @ UdpServer.run() UdpServer is running @ 1200
### [2024/11/7 17:12:21] INFO @ OscP5 is running. you (192.168.0.106) are listening @ port 1200
For some reason it does not state that the ip is 127.0.0.1 and gives 192.168.0.106 instead - this is the ip of the computer so both point to the same device but Android does this differently (see below).
Here is the code that runs on the Android phone:
import oscP5.*;
import netP5.*;
OscP5 oscP5;
NetAddress myRemoteLocation;
int colour = 255;
void setup()
{
size(600, 600);
/* start oscP5, listening for incoming messages at port 12000 */
oscP5 = new OscP5(this,1300);
println(this);
/* the address of this device */
myRemoteLocation = new NetAddress("127.0.0.1",1200);
}
void draw()
{
background(0);
fill(colour);
circle(width/2, height/2, 200);
int i = int(random(0, 200));
if (i == 1) sendmessage();
}
void sendmessage()
{
/* create the message */
OscMessage myMessage = new OscMessage("/two");
color c = color(random(0, 255), random(0, 255), random(0, 255));
myMessage.add(c);
/* send the message */
println("Sending message: " + myMessage);
oscP5.send(myMessage, myRemoteLocation);
}
/* incoming osc message are forwarded to the oscEvent method */
void oscEvent(OscMessage theOscMessage)
{
colour = theOscMessage.get(0).intValue();
}
and the manifest has internet and wifi permissions as follows
<?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="">
<application android:icon="@mipmap/ic_launcher" android:label="">
<activity android:exported="true" android:name=".MainActivity" android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
This also runs without issue and gives this in the console:
### [2024/11/7 17:29:23] PROCESS @ OscP5 stopped.
### [2024/11/7 17:29:23] PROCESS @ UdpClient.openSocket udp socket initialized.
### [2024/11/7 17:29:24] PROCESS @ UdpServer.start() new Unicast DatagramSocket created @ port 1300
### [2024/11/7 17:29:24] PROCESS @ UdpServer.run() UdpServer is running @ 1300
### [2024/11/7 17:29:24] INFO @ OscP5 is running. you (127.0.0.1) are listening @ port 1300
But, note the 127.0.0.1 ip address.
Both sketches run but don't communicate with each other. If I run the Android sketch on the desktop computer alongside the computer sketch they communicate without issue both ways. What am I missing/overlooking here?
Don't use the localhost address 127.0.0.1 on both sides. Instead use the other side's IP address in 'myRemoteLocation'. I found the Android device's IP address under the WI-FI settings on my old Samsung tablet. The computer's IP address should be shown in the console log when you run oscP5. To confirm that messages are being sent both ways add appropriate println statements to the oscEvent() function. eg, println("Message from Android") on the computer side and println("Message from Processing") on the Android side. Color dots should be present on both sides. Make certain to change the IP addresses below for your systems and add INTERNET to the Android permissions.
Desktop Processing code:
import oscP5.*;
import netP5.*;
OscP5 oscP5;
NetAddress myRemoteLocation;
int colour = 255;
void setup() {
size(600, 600);
/* start oscP5, listening for incoming messages at port 1200 */
oscP5 = new OscP5(this, 1200);
/* **IP address of Android device** */
myRemoteLocation = new NetAddress("10.0.0.xx", 1200);
}
void draw() {
background(0);
fill(colour);
circle(width/2, height/2, 200);
int i = int(random(0, 200));
if (i == 1) sendmessage();
}
void sendmessage() {
/* create the message */
OscMessage myMessage = new OscMessage("/one");
color c = color(random(0, 255), random(0, 255), random(0, 255));
myMessage.add(c);
/* send the message */
println("Sending message: " + myMessage);
oscP5.send(myMessage, myRemoteLocation);
}
// incoming osc message are forwarded to the oscEvent method.
void oscEvent(OscMessage theOscMessage) {
/* check if theOscMessage has the address pattern we are looking for. */
if (theOscMessage.checkAddrPattern("/two") == true) {
colour = theOscMessage.get(0).intValue();
println("Received message from Android.");
}
}
Android Device Processing code:
import oscP5.*;
import netP5.*;
OscP5 oscP5;
NetAddress myRemoteLocation;
int colour = 255;
void setup() {
size(600, 600);
/* start oscP5, listening for incoming messages at port 12000 */
oscP5 = new OscP5(this,1200);
/* **IP address of computer** */
myRemoteLocation = new NetAddress("10.0.0.xxx",1200);
}
void draw() {
background(0);
fill(colour);
circle(width/2, height/2, 200);
int i = int(random(0, 200));
if (i == 1) sendmessage();
}
void sendmessage() {
/* create the message */
OscMessage myMessage = new OscMessage("/two");
color c = color(random(0, 255), random(0, 255), random(0, 255));
myMessage.add(c);
/* send the message */
println("Sending message: " + myMessage);
oscP5.send(myMessage, myRemoteLocation);
}
// incoming osc message are forwarded to the oscEvent method.
void oscEvent(OscMessage theOscMessage){
/* check if theOscMessage has the address pattern we are looking for. */
if (theOscMessage.checkAddrPattern("/one") == true) {
colour = theOscMessage.get(0).intValue();
println("Received message from Processing.");
}
}
In addition INTERNET permission must be added to Android app Manifest:
<uses-permission android:name="android.permission.INTERNET"/>