Android->PC UDP communication

dandan
edited March 2016 in Android Mode

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

Tagged:

Answers

  • I just tried on Kindle (before I was trying in a Samsung GT--I9070) and it works, it seems to be the same problem as https://code.google.com/p/android/issues/detail?id=8407

  • Maybe the problem is because you need the external IP of the phone, because the information between the phone and the world is bypassed with a gateway, and you cant connect direct to the IP

  • @dan== a) what permissions in the manifest for android side??? - you must have a lot of them for udp android (internet, wifi, wifi state etc.) b) what min sdk??? c) in the code you must create Multicastlock; without it android filter the udp incoming packets & ignore them for security reasons. d) all these points checked and solved it can happen that receiving is ignored, depending of the hardware: and nobody knows... e) i ll try and see; till now i never used udp with android but tcp/ip (it works) f) your code for laptop (java) seems to me somewhat strange though you wrote that it works for receiving or sending. - why (udp sender) array length = 4??? - It fires index out of bounds exception...Set it to 20 or more - what is "append" in the conversion-btesToString??? - Append it seems to me is for StringList and it fires an error on eclipse (bad arrayType) hoping that it helps

  • @dan=== tested (with my code android side) kitkat sony xperia:: it works.

  • kfrajer and akenaton, I uploaded my instrument display sketch to my RCA tablet. The graphics/geometry looks good, but the WiFi UDP got a problem. At the beginning, the port even could be setup, it always showed EACCES (permission denied). I found that I need to check the "Sketch Permissions". So far I checked the following: Access_Network_State, Access_WIFI_State, Change_Network_State, Change_WIFI_Multicast_State, Change_WIFI_Sate, Internet, Read_Frame_Buffer, Read_Input_State. After I checked the permissions, the port seems opened no EACCES errors anymore, but the UDP is still not through. I don't know if I need to put more checks on or my code (working for Windows laptop) has problem in Android Mode:

    `int PORT_RX = 5555; String HOST_IP = "192.168.1.148";

    float Phi; float Theta; float Psi;

    void setup() { size(1360, 690); rectMode(CENTER); smooth();

    udp = new UDP( this, PORT_RX, HOST_IP); udp.listen( true );

    } ...

    void receive(byte[] data, String HOST_IP, int PORT_RX){ String value=new String(data); String[] input = split(value, " "); if(input.length == 3){ Phi = float(input[2]); Theta = float(input[1]); Psi = float(input[0]); } }`

    This code works for Windows fine. If you guys could give me some WiFi UDP reference code that works in Android mode? Thanks.

  • Are you working on a local network?

    Kf

  • kfrajer, Thank you for your reminder. I forgot to tell the sender side to change the IP address. After sender side changed IP address to 192.168.1.148, RCA tablet received UDP data from the instrument and displayed correctly. Now everything is OK. Again thank you very much for your help.

Sign In or Register to comment.