# USB Serial Library for Processing for Android?

**URL:** <https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750>\
**Category:** Processing for Android\
**Created:** [February 12, 2025, 8:21pm UTC](https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750 "2025-02-12T20:21:10Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)\
**Post date:** [February 12, 2025, 8:21pm UTC](https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750/1 "2025-02-12T20:21:10Z")

</div>

I have been trying to find a USB Serial Library for Processing for Android that actually works but I am not having much luck.  
The only one I found that I managed to make more or less work is the one found at

> **[GitHub - nimanns/ProcessingAndroidSerial: A serial communication library for processing for...](https://github.com/nimanns/ProcessingAndroidSerial)**
>
> A serial communication library for processing for android.

At least it compiles, uploads to both my android devices, one of them about 6 years old and the other quite recent, and I manage to send and receive serial over USB.  
The problem is that when I exit the app on my phone, but it stays on the recent apps screen, it crashes when I call it again either from there, or from the Home Screen. If I close the app completely and restart it, it works again. I think this might be the library causing this, but could someone please have a look at this problem? I am a beginner in Processing and I have no idea how to debug this. It has been a steep learning curve until now.  
also, do you know any other library that works better?  
Thank you in advance for your help. Here is the Test code I am running.

```auto
import com.processing.android.serial.*;

Serial my_port;

void setup() {
  fullScreen();
  noStroke();
  fill(0);
  my_port = new Serial(this, 9600);

}

void draw() {
  background(220);
  
}

void mousePressed() {
    if (mouseX < width/2) {
      rect(0, 0, width/2, height); // Left
      //send a string to mcu
      my_port.write("Gain\n");
    my_port.write(str(0) + "\n");
    delay(1000);
    my_port.write("Gain\n");
    my_port.write(str(127) + "\n");
    delay(1000);
    my_port.write("Gain\n");
    my_port.write(str(0) + "\n");
    delay(1000);
    my_port.write("Gain\n");
    my_port.write(str(127) + "\n");
    delay(1000);
    my_port.write("Gain\n");
    my_port.write(str(0) + "\n");
    } 
    if (mouseX > width/2) {
      rect(width/2, 0, width/2, height); // Right
      //send a string to mcu
      my_port.write("Volume\n");
    my_port.write(str(0) + "\n");
    delay(1000);
    my_port.write("Volume\n");
    my_port.write(str(127) + "\n");
    delay(1000);
    my_port.write("Volume\n");
    my_port.write(str(0) + "\n");
    delay(1000);
    my_port.write("Volume\n");
    my_port.write(str(127) + "\n");
    delay(1000);
    my_port.write("Volume\n");
    my_port.write(str(0) + "\n");
    }
    
  }

```

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [February 13, 2025, 12:03am UTC](https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750/2 "2025-02-13T00:03:02Z")

</div>

try this library

> [@APDE USB / OTG communication](https://discourse.processing.org/t/apde-usb-otg-communication/30114/117):
>
> @paulgoux here is what i made in android mode see if you can find something to compare with this is working code via android mode the link somewhere here import com.yourinventit.processing.android.serial.\*; //import processing.serial.\*; // imports library for serial communication Serial SerialPort; // defines Object for Serial String ang=""; String distance=""; String data=""; int angle, dist; void setup() { size (1200, 700); // myPort…

```auto
import com.yourinventit.processing.android.serial.*;

//import processing.serial.*; // imports library for serial communication

Serial SerialPort; // defines Object for Serial

String ang="";
String distance="";
String data="";

int angle, dist;

void setup() {
  
   size (1200, 700); 
   
  // myPort = new Serial(this,"COM4", 9600); // starts the serial communication
  
 SerialPort = new Serial(this, Serial.list(this)[0], 9600);
  SerialPort .bufferUntil('.'); // reads the data from the serial port up to the character '.' before calling serialEvent
   
 
  background(0);
}

void draw() {
  
                              //for the blur effect
      fill(0,5); //colour,opacity
      noStroke();
      rect(0, 0, width, height*0.93); 
      
      noStroke();
      fill(0,255);
      rect(0,height*0.93,width,height); // so that the text having angle and distance doesnt blur out
      
      
      drawRadar(); 
      drawLine();
      drawObject();
      drawText();
}

void serialEvent (Serial myPort) { // starts reading data from the Serial Port
                                                                                      // reads the data from the Serial Port up to the character '.' and puts it into the String variable "data".
      data = myPort.readStringUntil('.');
      data = data.substring(0,data.length()-1);
      
      int index1 = data.indexOf(",");                                                    
      ang= data.substring(0, index1);                                                 
      distance= data.substring(index1+1, data.length());                            
      
      angle = int(ang);
      dist = int(distance);
      System.out.println(angle);
}

void drawRadar()
{
    pushMatrix();
    noFill();
    stroke(10,255,10); //green
    strokeWeight(3);
    
    translate(width/2,height-height*0.06);
    
    line(-width/2,0,width/2,0);
    
    arc(0,0,(width*0.5),(width*0.5),PI,TWO_PI);
    arc(0,0,(width*0.25),(width*0.25),PI,TWO_PI);
    arc(0,0,(width*0.75),(width*0.75),PI,TWO_PI);
    arc(0,0,(width*0.95),(width*0.95),PI,TWO_PI);
    
    line(0,0,(-width/2)*cos(radians(30)),(-width/2)*sin(radians(30)));
    line(0,0,(-width/2)*cos(radians(60)),(-width/2)*sin(radians(60)));
    line(0,0,(-width/2)*cos(radians(90)),(-width/2)*sin(radians(90)));
    line(0,0,(-width/2)*cos(radians(120)),(-width/2)*sin(radians(120)));
    line(0,0,(-width/2)*cos(radians(150)),(-width/2)*sin(radians(150)));
    
    stroke(175,255,175); 
    strokeWeight(1);
    line(0,0,(-width/2)*cos(radians(15)),(-width/2)*sin(radians(15)));
    line(0,0,(-width/2)*cos(radians(45)),(-width/2)*sin(radians(45)));
    line(0,0,(-width/2)*cos(radians(75)),(-width/2)*sin(radians(75)));
    line(0,0,(-width/2)*cos(radians(105)),(-width/2)*sin(radians(105)));
    line(0,0,(-width/2)*cos(radians(135)),(-width/2)*sin(radians(135)));
    line(0,0,(-width/2)*cos(radians(165)),(-width/2)*sin(radians(165)));

    popMatrix();
}

void drawLine() {
  
    pushMatrix();
    
    strokeWeight(9);
    stroke(0,255,0);
    translate(width/2,height-height*0.06); 
    
   line(0,0,(width/2)*cos(radians(angle)),(-width/2)*sin(radians(angle)));
   

    popMatrix();
    
}

void drawObject() {
  
    pushMatrix();
    
    strokeWeight(9);
    stroke(255,0,0);
    translate(width/2,height-height*0.06);
   
    float pixleDist = (dist/40.0)*(width/2.0); // covers the distance from the sensor from cm to pixels
    float pd=(width/2)-pixleDist;
    
             
    float x=-pixleDist*cos(radians(angle));
    float y=-pixleDist*sin(radians(angle));
    
    if(dist<=40) // limiting the range to 40 cms
    {                               
       //line(0,0,pixleDist,0);  
       line(-x,y,-x+(pd*cos(radians(angle))),y-(pd*sin(radians(angle))));
    }
    popMatrix();
}

void drawText()
{
    pushMatrix();
    
    fill(100,200,255);
    textSize(25);
    
    text("10cm",(width/2)+(width*0.115),height*0.93);
    text("20cm",(width/2)+(width*0.24),height*0.93);
    text("30cm",(width/2)+(width*0.365),height*0.93);
    text("40cm",(width/2)+(width*0.45),height*0.93);
    
    textSize(40);
    text("YAinnoware",width*0.08,height*0.99);
    text("Angle :"+angle,width*0.45,height*0.99);
    
    if(dist<=40) {
      text("Distance :"+dist,width*0.7,height*0.99);
    }
      
   translate(width/2,height-height*0.06);
   textSize(25);
   
   text(" 30°",(width/2)*cos(radians(30)),(-width/2)*sin(radians(30)));
   text(" 60°",(width/2)*cos(radians(60)),(-width/2)*sin(radians(60)));
   text("90°",(width/2)*cos(radians(91)),(-width/2)*sin(radians(90)));
   text("120°",(width/2)*cos(radians(123)),(-width/2)*sin(radians(118)));
   text("150°",(width/2)*cos(radians(160)),(-width/2)*sin(radians(150)));
    
    popMatrix();  
  
}

```

---

<div class="post-metadata">

**Author:** ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)\
**Post date:** [February 13, 2025, 12:17am UTC](https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750/3 "2025-02-13T00:17:42Z")

</div>

Thanks, but which one? On the link you provided there some other links to a few libraries posted…

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [February 13, 2025, 1:10am UTC](https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750/4 "2025-02-13T01:10:36Z")

</div>

> **[GitHub - inventit/processing-android-serial: Android Serial Library for Processing](https://github.com/inventit/processing-android-serial?tab=readme-ov-file)**
>
> Android Serial Library for Processing

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [February 13, 2025, 1:33am UTC](https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750/5 "2025-02-13T01:33:55Z")

</div>

read the documentation on github I made this

i have added 2 files to the root folder

this in the main root AndroidManifest

```auto

<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="">
    <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="28"/>
    <application android:icon="@mipmap/ic_launcher" android:label="">
        <activity android:name=".MainActivity" android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
                <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"/>
            </intent-filter>
            <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter"/>
        </activity>
    </application>
</manifest>

```

and created folders inside root folder \res\xml then added this file device\_filter

```auto

and created folders inside root folder \res\xml then added this file device_filter

<?xml version="1.0" encoding="UTF-8"?>

-<resources>

<!-- 0x0403 / 0x6001: FTDI FT232R UART -->

<usb-device product-id="24577" vendor-id="1027"/>

<!-- 0x2341 / Arduino -->

<usb-device vendor-id="9025"/>

<!-- 0x16C0 / 0x0483: Teensyduino -->

<usb-device product-id="1155" vendor-id="5824"/>

<!-- 0x10C4 / 0xEA60: CP210x UART Bridge -->

<usb-device product-id="60000" vender-id="4292"/>

</resources>

```

---

<div class="post-metadata">

**Author:** ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)\
**Post date:** [February 13, 2025, 1:34am UTC](https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750/6 "2025-02-13T01:34:09Z")

</div>

Your code is not compiling with this one… I am sure I am doing something wrong…  
But what?

---

<div class="post-metadata">

**Author:** ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)\
**Post date:** [February 13, 2025, 1:43am UTC](https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750/7 "2025-02-13T01:43:26Z")

</div>

I updated the manifest file to be like yours, same for the device\_filter file.  
But then I had to import the library because the one on your code seem not to be the correct one.

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

```

The code does not compile…

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [February 13, 2025, 1:50am UTC](https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750/8 "2025-02-13T01:50:18Z")

</div>

> [@jhsa](#):
>
> But then I had to import the library because the one on your code seem not to be the correct one.
> 
> ```auto
> import io.inventit.processing.android.serial.*;
> 
> ```

maybe i am mistake with library link

```auto
import com.yourinventit.processing.android.serial.*;

```

it was years ago my laptop not working now

edit : i tested it that time windows 7 and processing 3

---

<div class="post-metadata">

**Author:** ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)\
**Post date:** [February 13, 2025, 2:03am UTC](https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750/9 "2025-02-13T02:03:49Z")

</div>

the one you just sent me doesn’t work with

`import com.yourinventit.processing.android.serial.*;`

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [February 13, 2025, 2:15am UTC](https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750/10 "2025-02-13T02:15:52Z")

</div>

> [@jhsa](#):
>
> the one you just sent me doesn’t work with

trying to remember not sure try this

> **[Release 0.1.0-alpha · inventit/processing-android-serial](https://github.com/inventit/processing-android-serial/releases/tag/0.1.0-alpha)**
>
> Processing 3 support and Processing 2 is no longer supported

---

<div class="post-metadata">

**Author:** ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)\
**Post date:** [February 13, 2025, 2:27am UTC](https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750/11 "2025-02-13T02:27:25Z")

</div>

Same with this one. It doesn’t compile. I used the manifest file and the device filter file you posted above.

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [February 13, 2025, 4:13am UTC](https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750/12 "2025-02-13T04:13:34Z")

</div>

> **[libraries.rar](https://drive.google.com/file/d/1lPU9OBgMbhDKQMFTo3HEm9tsfSSHO6XA/view?usp=drivesdk)**
>
> Google Drive file.

this link contain 2 libraries one of them that i used try them …may both files same library i found it on my drive

if i remember may this the same if not the drive link i am sure. that i used

> **[GitHub - mik3y/usb-serial-for-android: Android USB host serial driver library for CDC,...](https://github.com/mik3y/usb-serial-for-android)**
>
> Android USB host serial driver library for CDC, FTDI, Arduino and other devices.

---

<div class="post-metadata">

**Author:** ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)\
**Post date:** [February 13, 2025, 8:56pm UTC](https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750/13 "2025-02-13T20:56:17Z")

</div>

I don’t think this library works on processing? The one I posted on the first post seems to have been adapted for processing from this one…

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [February 13, 2025, 10:39pm UTC](https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750/14 "2025-02-13T22:39:10Z")

</div>

the libraries.rar the same on my processing libraries folder that i used and it’s working with my example my system windows 7 processing 3 Android version 8.1.0

---

<div class="post-metadata">

**Author:** ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)\
**Post date:** [February 14, 2025, 11:57am UTC](https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750/15 "2025-02-14T11:57:11Z")

</div>

I have Processing 4.3.2 on windows 10. Could that be the reason why is not working?  
Sorry for the stupid questions jafal, you know I am not very familiar with all this yet.

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [February 14, 2025, 2:29pm UTC](https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750/16 "2025-02-14T14:29:02Z")

</div>

> [@jhsa](#):
>
> Could that be the reason why is not working?

in general yes it could be but in this case i am not sure i am going to try your library with example included Arduino / Android on my system and see how it’s going on may you missed permission or something else…and i am going to post the result

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [February 14, 2025, 2:34pm UTC](https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750/17 "2025-02-14T14:34:37Z")

</div>

> [@jhsa](#):
>
> I have Processing 4.3.2 on windows 10

I have the same and an older Android phone (Version 9) .

Using the latest Android Mode:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/b/9/b9e887ffacc77cf462291b7d313e7a74527eb978.png)

Using this library:  
_[GitHub - nimanns/ProcessingAndroidSerial: A serial communication library for processing for android.](https://github.com/nimanns/ProcessingAndroidSerial)_

I tested the “Simple Read” example that comes with the library and was able to receive numbers but also missed a lot of numbers and displayed this in between:

`Data: nread not positive`

There were no issues with closing and opening the app.

Note:  
I was not able to import the library and added this to the sketch folder ( drag and drop onto sketch and it adds it to a code folder in the sketch):  
`ProcessingAndroidSerial.jar`

That was the extent of my testing.

`:)`

---

<div class="post-metadata">

**Author:** ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)\
**Post date:** [February 14, 2025, 3:38pm UTC](https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750/18 "2025-02-14T15:38:21Z")

</div>

Thank you for your help jafal.

---

<div class="post-metadata">

**Author:** ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)\
**Post date:** [February 14, 2025, 3:43pm UTC](https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750/19 "2025-02-14T15:43:31Z")

</div>

yeah, it doesn’t work if you put the whole folder in the processing “libraries” folder, you need to copy the contents of the folder paste them in the library folder.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [February 14, 2025, 3:43pm UTC](https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750/20 "2025-02-14T15:43:39Z")

</div>

> [@jhsa](#):
>
> yeah, it doesn’t work if you put the whole folder in the processing “libraries” folder, you need to copy the contents of the folder paste them in the library folder.

> [@glv](#):
>
> Note:  
> I was not able to import the library and added this to the sketch folder ( drag and drop onto sketch and it adds it to a code folder in the sketch):  
> `ProcessingAndroidSerial.jar`

I got rid of the `-main` in the library name and it imported correctly!

From:  
`ProcessingAndroidSerial-main`

to:  
`ProcessingAndroidSerial`

No change to my observations in previous post.

I may try your code if time permits… busy days for me.

`:)`

[Next page](https://discourse.processing.org/t/usb-serial-library-for-processing-for-android/45750.md?page=2)
