We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am using broadcast function of oscP5 to build communication among a PC computer (acts as server) and more than one android phone (one HTC and one Samsung). The communication succeed among two PC computers, and sccceed from android phone to both PC server and PC client, but fail in the case of PC server to phone (,and also fail in the case of PC client to phone via PC server). The HTC phone use data connection approach while Samsung use Wifi approach to communicate
I am stuck by this problem for long and am very frustrated. Does anyone faces simialr situation?
I found a post: forum.processing.org/one/topic/oscp5-for-processing-android.html in which alkopop79 described a very similar problem like me. I found two sources: code.google.com/p/android/issues/detail?id=8407 and https://pzoleeblogen.wordpress.com/2014/03/04/android-udp-broadcast-message-is-not-received/ The comment 35 in former link and the code in later link could be the solution but I have no idea how to incorporate them with processing code. Or should there be other solutions?
the code in client side is here: (user input ip address by keyboard, then click system_start button, then draw stroke on screen, the storke data will be sent out)
import oscP5.*;
import netP5.*;
import controlP5.*;
import ketai.ui.*;
OscP5 oscP5;
NetAddress myBroadcastLocation;
ControlP5 _interface;
String mode;
boolean keyboardShow = false;
String IP = "";
PFont font;
void setup()
{
orientation(PORTRAIT);
background(204);
stroke(0);
frameRate(25);
textSize(18);
font = createFont("Arial.ttf", 18);
ControlFont cFont = new ControlFont(font);
textFont(font);
_interface = new ControlP5(this);
mode = "connect"; // connect | draw
_interface.addTextfield("IP address")
.setPosition(width-230, 20)
.setSize(180,50)
.setFont(font)
.setFocus(true)
.setColor(color(200,200,200))
;
_interface.getController("IP address")
.getCaptionLabel()
.setFont(cFont)
.setSize(12)
;
_interface.addButton("system_start")
.setValue(100)
.setPosition(width-230, 100)
.setSize(180,50)
;
_interface.getController("system_start")
.getCaptionLabel()
.setFont(cFont)
.setSize(18)
;
}
void draw()
{
if (!_interface.get(Textfield.class,"IP address").isFocus())
closeKeyboard();
if (mode=="connect") {
if (_interface.get(Textfield.class,"IP address").isFocus() && !keyboardShow)
{
KetaiKeyboard.toggle(this);
keyboardShow = true;
}}
else if (mode == "start") {
if (mousePressed == true)
{
// client draw
stroke(255);
line(pmouseX, pmouseY, mouseX, mouseY);
OscMessage m = new OscMessage("/draw");
m.add(pmouseX);
m.add(pmouseY);
m.add(mouseX);
m.add(mouseY);
oscP5.send(m, myBroadcastLocation);
}}
}
void system_start()
{
if (mode=="connect")
{
IP = _interface.get(Textfield.class,"IP address").getText();
if (!IP.equals(""))
{
oscP5 = new OscP5(this,12000); // 12000: the port that client listens incoming messages
myBroadcastLocation = new NetAddress(IP,32000); // the address of the osc broadcast server
OscMessage m = new OscMessage("/server/connect",new Object[0]); // send connect request to server
oscP5.flush(m,myBroadcastLocation);
oscP5.plug(this,"serverDraw","/draw");
mode = "start";
}
}
}
void oscEvent(OscMessage m)
{
if(m.isPlugged()==false)
{
// no code is here
}
}
// server draw
public void serverDraw(int px, int py, int x, int y)
{
stroke(0);
line(px, py, x, y);
}
void keyPressed()
{
OscMessage m;
if (key!=ENTER && key!=ESC && key!='d')
{
IP = IP + key;
_interface.get(Textfield.class,"IP address").setText(IP);
}
else if (key==ENTER)
closeKeyboard();
}
void closeKeyboard()
{
KetaiKeyboard.hide(this);
keyboardShow=false;
_interface.get(Textfield.class,"IP address").setFocus(false);
}
the code in server side is here:
import oscP5.*;
import netP5.*;
OscP5 oscP5;
NetAddressList myNetAddressList = new NetAddressList();
int myListeningPort = 32000; // this port: the server listens incoming messages
int myBroadcastPort = 12000; // this port: the clients listens incoming messages from the server
String myConnectPattern = "/server/connect";
String myDisconnectPattern = "/server/disconnect";
void setup()
{
size(450, 400);
background(204);
stroke(0);
frameRate(25);
oscP5 = new OscP5(this, myListeningPort);
}
void draw()
{
// case 1: server draw
if (mousePressed == true)
{
stroke(255);
line(pmouseX, pmouseY, mouseX, mouseY);
OscMessage m = new OscMessage("/draw");
m.add(pmouseX);
m.add(pmouseY);
m.add(mouseX);
m.add(mouseY);
oscP5.send(m, myNetAddressList);
}
}
void oscEvent(OscMessage m)
{
String address = m.netAddress().address();
if (m.addrPattern().equals(myConnectPattern)) // case 1: client request connect
connect(address);
else if (m.addrPattern().equals(myDisconnectPattern)) // case 2: client request disconnect
disconnect(address);
else
{
// to send message to clients other than the sender
// otherwise, the sender will receive message from itself and cause duplicated strokes
if(m.checkAddrPattern("/draw")==true && m.checkTypetag("iiii"))
{
for (int i=0; i<myNetAddressList.size(); i++)
{
NetAddress NA = myNetAddressList.get(i);
if ( !NA.address().equals(address) )
oscP5.send(m, NA);
}
// decode the message
int px = m.get(0).intValue();
int py = m.get(1).intValue();
int x = m.get(2).intValue();
int y = m.get(3).intValue();
// to draw
stroke(0);
line(px, py, x, y);
}
}
}
private void connect(String IP)
{
if (!myNetAddressList.contains(IP, myBroadcastPort))
{
myNetAddressList.add(new NetAddress(IP, myBroadcastPort));
println("### adding "+IP+" to the list.");
}
else
println("### "+IP+" is already connected.");
println("server say: "+myNetAddressList.list().size()+" remote locations connected.");
}
private void disconnect(String IP)
{
if (myNetAddressList.contains(IP, myBroadcastPort))
{
myNetAddressList.remove(IP, myBroadcastPort);
println("### removing "+IP+" from the list.");
}
else
println("### "+IP+" is not connected.");
println("server say: "+myNetAddressList.list().size()+" remote locations connected.");
}
Answers
As you can see from my post I have the same issues with UDP in hypermedia http://forum.processing.org/two/discussion/8860/android-pc-udp-communication
I didn't try with OSC yet. I'll give a try maybe something magical happen.