# From java to android

**URL:** <https://discourse.processing.org/t/from-java-to-android/25022>\
**Category:** Processing for Android\
**Tags:** homework\
**Created:** [October 29, 2020, 7:51pm UTC](https://discourse.processing.org/t/from-java-to-android/25022 "2020-10-29T19:51:26Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![r0x15](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r0x15](https://discourse.processing.org/u/r0x15)\
**Post date:** [October 29, 2020, 7:51pm UTC](https://discourse.processing.org/t/from-java-to-android/25022/1 "2020-10-29T19:51:26Z")

</div>

hello i have code java on processing 3 but i need export for make app apk  
who help me  
i new  
tankyou

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [October 29, 2020, 8:00pm UTC](https://discourse.processing.org/t/from-java-to-android/25022/2 "2020-10-29T20:00:37Z")

</div>

Hi @r0x15  
Welcome to the forum.  
Normally you can run the same Java code (not P5.js) and compile it.

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [November 1, 2020, 10:44am UTC](https://discourse.processing.org/t/from-java-to-android/25022/3 "2020-11-01T10:44:38Z")

</div>

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

```auto
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);
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [November 1, 2020, 5:31pm UTC](https://discourse.processing.org/t/from-java-to-android/25022/4 "2020-11-01T17:31:51Z")

</div>

@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.

```auto
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:

 ![error](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/fcfaf8a248ddb5114cf85e9d63e709bfdd809636.jpeg)

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

```auto
<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:

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

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

void draw() {
}

```

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [November 1, 2020, 6:07pm UTC](https://discourse.processing.org/t/from-java-to-android/25022/5 "2020-11-01T18:07:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [November 1, 2020, 6:45pm UTC](https://discourse.processing.org/t/from-java-to-android/25022/6 "2020-11-01T18:45:01Z")

</div>

Thank you for the response!  
Another topic was opened by him [here](https://discourse.processing.org/t/anyone-possible-make-project/25109/6). I also believe that the best solution is using bluetooth or wifi.

---

<div class="post-metadata">

**Author:** ![JoseMY](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josemy/32/2016_2.png) [@JoseMY](https://discourse.processing.org/u/JoseMY)\
**Post date:** [November 8, 2020, 9:45am UTC](https://discourse.processing.org/t/from-java-to-android/25022/7 "2020-11-08T09:45:06Z")

</div>

> [@svan](#):
>
> 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.’

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.

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [November 8, 2020, 2:50pm UTC](https://discourse.processing.org/t/from-java-to-android/25022/8 "2020-11-08T14:50:43Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![r0x15](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r0x15](https://discourse.processing.org/u/r0x15)\
**Post date:** [November 8, 2020, 3:19pm UTC](https://discourse.processing.org/t/from-java-to-android/25022/9 "2020-11-08T15:19:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [November 8, 2020, 4:32pm UTC](https://discourse.processing.org/t/from-java-to-android/25022/10 "2020-11-08T16:32:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [November 8, 2020, 5:03pm UTC](https://discourse.processing.org/t/from-java-to-android/25022/11 "2020-11-08T17:03:03Z")

</div>

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:

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

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

void draw() {
}

```

![androidSerialError](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/94992f0a2a88a8ee93afc406f3cf9a168b01ac87.jpeg)

---

<div class="post-metadata">

**Author:** ![r0x15](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r0x15](https://discourse.processing.org/u/r0x15)\
**Post date:** [November 8, 2020, 7:43pm UTC](https://discourse.processing.org/t/from-java-to-android/25022/12 "2020-11-08T19:43:55Z")

</div>

try

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

void draw() {
}

```

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [November 8, 2020, 8:01pm UTC](https://discourse.processing.org/t/from-java-to-android/25022/13 "2020-11-08T20:01:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![r0x15](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r0x15](https://discourse.processing.org/u/r0x15)\
**Post date:** [November 8, 2020, 8:11pm UTC](https://discourse.processing.org/t/from-java-to-android/25022/14 "2020-11-08T20:11:36Z")

</div>

send me screeshoot error

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [November 8, 2020, 8:22pm UTC](https://discourse.processing.org/t/from-java-to-android/25022/15 "2020-11-08T20:22:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![r0x15](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r0x15](https://discourse.processing.org/u/r0x15)\
**Post date:** [November 8, 2020, 8:26pm UTC](https://discourse.processing.org/t/from-java-to-android/25022/16 "2020-11-08T20:26:49Z")

</div>

go [https://github.com/inventit/processing-android-serial](https://github.com/inventit/processing-android-serial)

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [November 8, 2020, 8:46pm UTC](https://discourse.processing.org/t/from-java-to-android/25022/17 "2020-11-08T20:46:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [November 8, 2020, 9:07pm UTC](https://discourse.processing.org/t/from-java-to-android/25022/18 "2020-11-08T21:07:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![r0x15](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r0x15](https://discourse.processing.org/u/r0x15)\
**Post date:** [November 8, 2020, 9:08pm UTC](https://discourse.processing.org/t/from-java-to-android/25022/19 "2020-11-08T21:08:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [November 8, 2020, 9:09pm UTC](https://discourse.processing.org/t/from-java-to-android/25022/20 "2020-11-08T21:09:36Z")

</div>

I already have the library.

[Next page](https://discourse.processing.org/t/from-java-to-android/25022.md?page=2)
