From java to android

@svan Could you please help him setting up the android serial library? It seems he can’t install it properly.
This is his code:

import processing.serial.*;
Serial myPort;
String myString = null;
int incoming_position = 0;
float position_in_degree = 0;
PFont font18, font63;

void setup() {
  size(800, 600);
  font18 = loadFont("Tahoma-18.vlw");
  font63 = loadFont("Tahoma-63.vlw");

  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 9600);
}

void draw() {
  background(100);
  textFont(font63, 36);
  text("RAW Position:", 30, 510);
  text(incoming_position, 270, 510);
  text("Position in Degree:", 30, 550);
  text(position_in_degree, 330, 550);
  draw_angle(400, 250, 200, position_in_degree); // draw circle and encoder position

  // Reading Serial Port when buffer not zero
  while (myPort.available() > 0) {
    myString = myPort.readStringUntil(10); // Read Data from Serial until line feed (10)
    if (myString != null) {
      myString = trim(myString); // Remove Line Feed
      incoming_position = int(myString); // String to int
      position_in_degree = map(incoming_position, 0, 1023, 0, 359.999); // translate raw position in to 360 degree position format
      println(position_in_degree);
    }
  }
}

void draw_angle(int x, int y, int size, float angle_dec) {
  noFill();
  stroke(255, 255, 255, 255);
  // X,Y plane Translation
  float end_x, end_y;
  float angle_rad = radians(angle_dec);
  end_x = x + cos(angle_rad)*size;
  end_y = y + sin(angle_rad)*size;
  // mark angle guage in circle
  line(x, y, end_x, end_y);

  int space = 20;
  int space2 = size - space;
  ellipse(x, y, size2+space, size2+space);

  // mark small tick on cicle every 15 degree
  for (int i = 0; i < 360; i = i +15) {
    float rad1 = radians(i);
    // mark large scale on 0 90 180 270 degree
    if ( i == 0 || i == 90 || i == 180 || i == 270) {
      line(x + cos(rad1)space2, y + sin(rad1)space2, x + cos(rad1)(size+space), y + sin(rad1)(size+space));
    } else {
      line(x + cos(rad1)*space2, y + sin(rad1)*space2, x + cos(rad1)*size, y + sin(rad1)*size);
    }
  }
}

@noel. I would be happy to try and help but my impression from my own experiences is that you can’t run his code on an Android device using processing.serial because I’ve never seen an Android device with a USB port. I’ve seen pc’s with USB serial ports, but not an Android device. Usually Android devices use bluetooth communication and it is possible to find and list paired devices with code I posted. Having said all of that why would someone write a serial library for Android if it wasn’t possible to do it? In order to run the user’s code first of all he has to use import io.inventit.processing.android.serial.*; and use Serial.list(this) instead of Serial.list(). I made a few other minor changes as shown below and got his code to run without error but then I get down to an error on my Android tablet that states the port is null and that I need to add a use-feature to the Manifest, which I did, but it still doesn’t work.

import io.inventit.processing.android.serial.*;

Serial myPort;
String myString = null;
int incoming_position = 0;
float position_in_degree = 0;
PFont font18, font63;
int space2 = 0;
int space = 0;

void setup() {
  size(800, 600);
 // font18 = loadFont("Tahoma-18.vlw");
 // font63 = loadFont("Tahoma-63.vlw");

  println(Serial.list(this));
  myPort = new Serial(this, Serial.list(this)[0], 9600);
}

void draw() {
  background(100);
  textFont(font63, 36);
  text("RAW Position:", 30, 510);
  text(incoming_position, 270, 510);
  text("Position in Degree:", 30, 550);
  text(position_in_degree, 330, 550);
  draw_angle(400, 250, 200, position_in_degree); // draw circle and encoder position

  // Reading Serial Port when buffer not zero
  
  while (myPort.available() > 0) {
    myString = myPort.readStringUntil(10); // Read Data from Serial until line feed (10)
    if (myString != null) {
      myString = trim(myString); // Remove Line Feed
      incoming_position = int(myString); // String to int
      position_in_degree = map(incoming_position, 0, 1023, 0, 359.999); // translate raw position in to 360 degree position format
      println(position_in_degree);
    }
  }
  
}

void draw_angle(int x, int y, int size, float angle_dec) {
  noFill();
  stroke(255, 255, 255, 255);
  // X,Y plane Translation
  float end_x, end_y;
  float angle_rad = radians(angle_dec);
  end_x = x + cos(angle_rad)*size;
  end_y = y + sin(angle_rad)*size;
  // mark angle guage in circle
  line(x, y, end_x, end_y);

  //int space = 20;
  //int space2 = size - space;
  space = 20;
  space2 = size - space;
//  ellipse(x, y, size2+space, size2+space);
 ellipse(x, y, size, size);
  // mark small tick on cicle every 15 degree
  for (int i = 0; i < 360; i = i +15) {
    float rad1 = radians(i);
    // mark large scale on 0 90 180 270 degree
    if ( i == 0 || i == 90 || i == 180 || i == 270) {
      line(x + cos(rad1)*space2, y + sin(rad1)*space2, x + cos(rad1)*(size+space), y + sin(rad1)*(size+space));
    } else {
      line(x + cos(rad1)*space2, y + sin(rad1)*space2, x + cos(rad1)*size, y + sin(rad1)*size);
    }
  }
}

This is the error msg that shows up on my Android Samsung tablet:

This is the line of code that I put into the manifest:

<uses-feature android:name="android.hardware.usb.host"/>

I wish I could be more help, but I’ve run into the same roadblock with both my own project and the user’s demo. I would suggest trying to get something simple like the following to work first:

import io.inventit.processing.android.serial.*;

void setup() {
  println(Serial.list(this));
}

void draw() {
}

I’m used to type A to micro B USB connectors. If someone was using a cable with two micro B connectors then maybe you could connect a USB serial device to an Android mobile device.

Thank you for the response!
Another topic was opened by him here. I also believe that the best solution is using bluetooth or wifi.

In the past you could talk to android with a serial comm.

I have a TV box with android N and a working USB-A port. Also, I used a Samsung Galaxy 4 with android Lollypop to control a USB 3d printer via otg adapter and an app called “3dFox”. But that app does not work on my current Samsung Note 8. It seems modern android phones only activate /dev/ttyS* if it is requested from ADB. At least, that is what I used to send massive calls during confinement.

@JoseMY. I just purchased an OTG (On The Go) cable and am interested in getting it to work on my Nexus 4 running Android 4.4. Any help would be deeply appreciated. I would like to send/receive text or data with an Arduino Uno if that is possible. I know the cable was initially designed primarily to run a printer and could probably find one to hook up if necessary. We might have to start a new thread if you’re interested in pursuing this.

yes I’m interested proceed to open a new thread but why send and receive text what is it for

I don’t care what we send/receive; I just want to see an Android device connect to something besides a printer or pc.

Actually what I’d like to know is how to get rid of this error message when I try to run this code on an Android device:

import io.inventit.processing.android.serial.*;

void setup() {
  println(Serial.list(this));
}

void draw() {
}

androidSerialError

try

import io.inventit.processing.android.serial.*;
void setup() {
  
println(this, Serial.list(this)[0], 9600);
}

void draw() {
}

Unfortunately that fails here also; same error message. Does it work on your system?

send me screeshoot error

I already posted it along with the demo that you are running.

go https://github.com/inventit/processing-android-serial

I’ve already seen that url, but it didn’t tell me how to get rid of the error message. I have not run their example which requires special hardware (although I may have it). Let us know if you get it to work on your system.

I found the piece of hardware that they are talking about. I already have it connected to an Arduino Uno with a screw shield to connect the input leads (3) and 2.8" lcd for data display. It was sitting on my work bench and as soon as I connected it to the otg cable and connected it to a Samsung S2 tablet it came to life with no app open. As soon as I inserted a finger into the ppg (photoplethysmography) heart rate sensor my pulse appeared on the lcd screen. I can only conclude that the error message may not be important. All you have to do is unplug the pc and re-connect to the Arduino. Progress; at least the Arduino is able to draw power from the Android. Thanks for making me take a second look; I missed that the first time around. We still have to write an Android app to display the data and then I can remove the lcd on the UNO.

on processing you have on skretch —>import library---->Android Serial Library for Processing ?

I already have the library.

[quote=“svan, post:18, topic:25022, full:true”]
good
I found the piece of hardware that they are talking about. Where talking about?

https://www.sparkfun.com/products/11574