Dear All, I spent the day try to make work the connection between android and pc via UDP. The code that I put here is what I hope "closer-to-work". Still I can't send data from PC to the Android, while the reception of PC from Android works.
::::::::::::::::::::::::: PC side code::::::::::::::::::::::::::
import hypermedia.net.*;
int portRX=50000;
int portTX=51000;
String ip="224.0.0.2";
int rx_buffer_size=100;
String rx_string="Waiting for Data";
byte[] rx_byte=new byte[rx_buffer_size];
int rx_byte_count=0;
String message="";
UDP udpTX,udpRX;
void setup() {
size(300,300);
udpTX=new UDP(this,portTX,ip);
udpTX.log(true);
udpTX.setBuffer(5);
udpTX.loopback(false);
udpRX=new UDP(this,portRX,ip);
udpRX.log(true);
udpRX.listen(true);
}
void draw(){
background(255);
fill(0);
text("Is TX mulitcast: "+udpTX.isMulticast(), 20,20);
text("Has TX joined multicast: "+udpTX.isJoined(), 20,40);
text("Is loopback enabled on TX: "+udpTX.isLoopback(), 20,60);
//confirm if rx is multicast
text("Is RX mulitcast: "+udpRX.isMulticast(), 20,80);
text("Has RX joined multicast: "+udpRX.isJoined(), 20, 100);
text(rx_string, 20, 120);
text(message, 20, 140);
}
void keyReleased() {
udpsender(key+"ciao buonasera");
}
//------------------------------------------------- RECEIVER
void receive( byte[] data ) {
message= bytes2string(data);
}
//------------------------------------------------- SENDER
void udpsender(String whatIsend){
byte[] data = new byte[4];
data=string2bytes(whatIsend);
udpTX.send( data );
}
//------------------------------------------------- STRING CONVERSIONS
byte[] string2bytes(String chaine) {
byte[] bytes = new byte[0];
for (int i = 0; i < chaine.length(); i++) {
bytes = append(bytes, byte(chaine.charAt(i)));
}
return bytes;
}
String bytes2string(byte[] bytes){
String chaine = str(char(bytes[0]));
for (int i = 1; i < bytes.length; i++) {
chaine = chaine+str(char(bytes[i]));
}
return chaine;
}
::::::::::::::::::::::::: Android side::::::::::::::::::::::::::
import hypermedia.net.*;
UDP udpTX,udpRX;
color uno=#9C5052;
color due=#00A881;
color backgro;
int stage=0;
String receivedFromUDP = "";
String ip="224.0.0.2";
int portRX=51000;
int portTX=50000;
int rx_buffer_size=100;
String rx_string="Waiting for Data";
byte[] rx_byte=new byte[rx_buffer_size];
int rx_byte_count=0;
void setup() {
backgro=0;
orientation(PORTRAIT);
smooth();
udpTX=new UDP(this,portTX,ip);
udpTX.setBuffer(5);
udpTX.loopback(false);
udpRX=new UDP(this,portRX,ip);
udpRX.listen(true);
super.start();
}
void draw() {
background(backgro);
textAlign(CENTER, CENTER);
if(stage==0) {
fill(uno);
rect(0, 0, width, height/2);
fill(due);
rect(0, height/2, width, height/2);
fill(255);
textSize(36);
text("Player 1",width/2,height/4);
text("Player 2",width/2,height/4*3);
}
textSize(12);
if(udpRX.isListening()) text("OK", width/2,height/2);
text("Is TX mulitcast: "+udpTX.isMulticast(), width/2,20);
text("Has TX joined multicast: "+udpTX.isJoined(), width/2,40);
text("Is loopback enabled on TX: "+udpTX.isLoopback(), width/2,60);
//confirm if rx is multicast
text("Is RX mulitcast: "+udpRX.isMulticast(), width/2,80);
text("Has RX joined multicast: "+udpRX.isJoined(), width/2, 100);
text(rx_string, width/2, 120);
text("message:"+receivedFromUDP, width/2,height/3);
}
//------------------------------------------------- SENDER
void udpsender(String whatIsend){
byte[] data = new byte[4];
data=string2bytes(whatIsend);
udpTX.send( data );
}
//------------------------------------------------- RECEIVER
void receive( byte[] data ) {
receivedFromUDP= bytes2string(data);
}
//------------------------------------------------- BUTTONS
boolean rectOver = false;
void mousePressed() {
if (overRect(0, 0, width, height/2)) {
stage=1; backgro=uno;
}
else {stage=2; backgro=due;}
udpsender("s"+stage);
}
boolean overRect(int x, int y, int widthRect, int heightRect) {
if (mouseX >= x && mouseX <= x+widthRect &&
mouseY >= y && mouseY <= y+heightRect) {
return true;
}
else {return false;}
}
//------------------------------------------------- STRING CONVERSIONS
byte[] string2bytes(String chaine) {
byte[] bytes = new byte[0];
for (int i = 0; i < chaine.length(); i++) {
bytes = append(bytes, byte(chaine.charAt(i)));
}
return bytes;
}
String bytes2string(byte[] bytes){
String chaine = str(char(bytes[0]));
for (int i = 1; i < bytes.length; i++) {
chaine = chaine+str(char(bytes[i]));
}
return chaine;
}
::::::::::::::::::::::::: ::::::::::::::::::::::::: I saw others having similar issues, but still could not figure out what is going wrong with mine. Thank you