# APDE USB / OTG communication

**URL:** <https://discourse.processing.org/t/apde-usb-otg-communication/30114>\
**Category:** Processing for Android\
**Created:** [May 16, 2021, 12:13am UTC](https://discourse.processing.org/t/apde-usb-otg-communication/30114 "2021-05-16T00:13:07Z")\
**Posts on this page:** 20\
**Page:** 6

<div class="post-metadata">

**Author:** ![ZENAR57](https://avatars.discourse-cdn.com/v4/letter/z/a698b9/32.png) [@ZENAR57](https://discourse.processing.org/u/ZENAR57)\
**Post date:** [May 22, 2021, 12:07am UTC](https://discourse.processing.org/t/apde-usb-otg-communication/30114/101 "2021-05-22T00:07:01Z")

</div>

my arduino configured for constant cyclic transmission at a rate of 57600. he cannot accept anything now, he has to change it.

---

<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:** [May 22, 2021, 12:07am UTC](https://discourse.processing.org/t/apde-usb-otg-communication/30114/102 "2021-05-22T00:07:01Z")

</div>

> [@ZENAR57](#):
>
> but I not runing. it needs to be edited manifest! Continue?

it runs when detect the suit hardware

---

<div class="post-metadata">

**Author:** ![ZENAR57](https://avatars.discourse-cdn.com/v4/letter/z/a698b9/32.png) [@ZENAR57](https://discourse.processing.org/u/ZENAR57)\
**Post date:** [May 22, 2021, 12:08am UTC](https://discourse.processing.org/t/apde-usb-otg-communication/30114/103 "2021-05-22T00:08:17Z")

</div>

Edit manifest.  
Wait 5 minutes.

---

<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:** [May 22, 2021, 12:10am UTC](https://discourse.processing.org/t/apde-usb-otg-communication/30114/104 "2021-05-22T00:10:51Z")

</div>

upload this to arduino

```auto
////////////////////////////////////////////////////////////////////////
//
// AndroidLedOn
//
// simpel serial in. If a 1 is received via serial, the led is 
// switched on. All other chars will switch of the leds.
//
#define NUMBER_OF_CHANNELS 8
#define PINLED1 5

volatile char lastReceivedCharFromSerialIn = '\0';

void setup() {

  pinMode(PINLED1, OUTPUT);
  Serial.begin(9600);
 
}

void loop() {
  digitalWrite( PINLED1, lastReceivedCharFromSerialIn == '1');
}

void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    lastReceivedCharFromSerialIn = (char)Serial.read(); 
  }
}

```

> [@ZENAR57](#):
>
> my arduino configured for constant cyclic transmission at a rate of 57600. he cannot accept anything now, he has to change it.

both must be matched

---

<div class="post-metadata">

**Author:** ![ZENAR57](https://avatars.discourse-cdn.com/v4/letter/z/a698b9/32.png) [@ZENAR57](https://discourse.processing.org/u/ZENAR57)\
**Post date:** [May 22, 2021, 12:15am UTC](https://discourse.processing.org/t/apde-usb-otg-communication/30114/105 "2021-05-22T00:15:41Z")

</div>

![Screenshot_20210522_031404_com.android.systemui](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c0c2b2ecdacfce46e1c19c30984dffd3c9152db8.jpeg)

---

<div class="post-metadata">

**Author:** ![ZENAR57](https://avatars.discourse-cdn.com/v4/letter/z/a698b9/32.png) [@ZENAR57](https://discourse.processing.org/u/ZENAR57)\
**Post date:** [May 22, 2021, 12:23am UTC](https://discourse.processing.org/t/apde-usb-otg-communication/30114/106 "2021-05-22T00:23:48Z")

</div>

It seems to me that it is much more reliable to simply receive messages from arduino at a speed of 57600. I see this in the terminal program.  
let’s just try to get something! there is no point in changing the arduino, I can not run any sketches on the phone, they collapse when accessing the usb initialization line. and your last program crashed too. nothing works on the phone, no screen!

---

<div class="post-metadata">

**Author:** ![ZENAR57](https://avatars.discourse-cdn.com/v4/letter/z/a698b9/32.png) [@ZENAR57](https://discourse.processing.org/u/ZENAR57)\
**Post date:** [May 22, 2021, 12:26am UTC](https://discourse.processing.org/t/apde-usb-otg-communication/30114/107 "2021-05-22T00:26:21Z")

</div>

Serial.list (this) [0]  
!!! Full crash programms!!!

> [@APDE USB / OTG communication](https://discourse.processing.org/t/apde-usb-otg-communication/30114/53):
>
> but I getting wrecked a primitive polling of devices. . Serial.list (this) [0] . I put the library well, in manual mode, through dfx . . -glv- wrote that he also did it on the phone. but he did not indicate which library he was using.I have provided an annotation and a direct link to the library that I used. in my previous post. but I do not understand this library is needed, or another.

---

<div class="post-metadata">

**Author:** ![ZENAR57](https://avatars.discourse-cdn.com/v4/letter/z/a698b9/32.png) [@ZENAR57](https://discourse.processing.org/u/ZENAR57)\
**Post date:** [May 22, 2021, 12:31am UTC](https://discourse.processing.org/t/apde-usb-otg-communication/30114/108 "2021-05-22T00:31:02Z")

</div>

[![](https://img.youtube.com/vi/BV5C0oCj30A/hqdefault.jpg "crash1") ](https://www.youtube.com/watch?v=BV5C0oCj30A)

---

<div class="post-metadata">

**Author:** ![ZENAR57](https://avatars.discourse-cdn.com/v4/letter/z/a698b9/32.png) [@ZENAR57](https://discourse.processing.org/u/ZENAR57)\
**Post date:** [May 22, 2021, 12:33am UTC](https://discourse.processing.org/t/apde-usb-otg-communication/30114/109 "2021-05-22T00:33:43Z")

</div>

it seems like one single library does not work well.

---

<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:** [May 22, 2021, 12:35am UTC](https://discourse.processing.org/t/apde-usb-otg-communication/30114/110 "2021-05-22T00:35:08Z")

</div>

i am going to check all and inform you

---

<div class="post-metadata">

**Author:** ![ZENAR57](https://avatars.discourse-cdn.com/v4/letter/z/a698b9/32.png) [@ZENAR57](https://discourse.processing.org/u/ZENAR57)\
**Post date:** [May 22, 2021, 12:39am UTC](https://discourse.processing.org/t/apde-usb-otg-communication/30114/111 "2021-05-22T00:39:34Z")

</div>

Ок.  
.  
.  
.  
No ellipse!

If delete line “if (Serial.list(this)[0] ==null){temp=0;}” -ellipse draws well

```auto
import org.slf4j.*;
import org.slf4j.helpers.*;
import com.yourinventit.processing.android.serial.*;
import com.hoho.android.usbserial.util.*;
import org.slf4j.spi.*;
import com.hoho.android.usbserial.driver.*;
import org.slf4j.impl.*;

Serial port;
int temp=1;
void setup(){
 if (Serial.list(this)[0] ==null){temp=0;}
//port = new Serial(this,Serial.list(this)[0],57600);
//port.bufferUntil('\n');
  size(800,800);
}

void draw (){
  fill(255, 0, 0);
  ellipse(100, 100, 100,100);
  text(temp, 300,300);
  
}

```

1. polling the list of USB devices Serial.list (this) [0]
2. based on point 1. setting port = new Serial (this, Serial.list (this) [0], 57600);
3. read / write to port

dies in the first paragraph. the second and third paragraph are now irrelevant!

---

<div class="post-metadata">

**Author:** ![ZENAR57](https://avatars.discourse-cdn.com/v4/letter/z/a698b9/32.png) [@ZENAR57](https://discourse.processing.org/u/ZENAR57)\
**Post date:** [May 24, 2021, 1:03pm UTC](https://discourse.processing.org/t/apde-usb-otg-communication/30114/112 "2021-05-24T13:03:35Z")

</div>

I am trying to repeat your successful feat.

> [@APDE USB / OTG communication](https://discourse.processing.org/t/apde-usb-otg-communication/30114/24):
>
> A working solution, with a bit of work from you, is already in your post: This was my starting point and I have shared this link with you before: [Impossible to grant access to USB port through AndroidManifest · Issue #84 · Calsign/APDE · GitHub](https://github.com/Calsign/APDE/issues/84) Calsign states: Yes technically you can achieve what you want by modifying the manifest template, but this isn’t really a solution for most users because it means that every sketch built with the modified version of APDE will have the uses-feature t…

I just need to take the last step to victory. how did you install the library you specified in APDE?

> **[GitHub - inventit/processing-android-serial: Android Serial Library for...](https://github.com/inventit/processing-android-serial)**
>
> Android Serial Library for Processing. Contribute to inventit/processing-android-serial development by creating an account on GitHub.

after all, this library lacks the elements necessary for connection, namely:  
jar files are missing in the “libraries” folder! How did you manage to connect such a library to APDE? You wrote in your post that you did it. Did you convert the library with something, if so, how and with what?

---

<div class="post-metadata">

**Author:** ![ZENAR57](https://avatars.discourse-cdn.com/v4/letter/z/a698b9/32.png) [@ZENAR57](https://discourse.processing.org/u/ZENAR57)\
**Post date:** [May 24, 2021, 1:18pm UTC](https://discourse.processing.org/t/apde-usb-otg-communication/30114/113 "2021-05-24T13:18:50Z")

</div>

@paulgoux  
I saw that you are very good at libraries, please tell me how and with what I can recompile libraries from github so that they work on android in APDE (there is .jar, there is a folder “library”)?  
I have seen the advice that this can be done using the “terminal ide”, but I have not found detailed instructions on how to do this anywhere. What are the options? I would be grateful for your help!

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [May 24, 2021, 1:45pm UTC](https://discourse.processing.org/t/apde-usb-otg-communication/30114/114 "2021-05-24T13:45:45Z")

</div>

Im not particularly great with libraries, i jave an android repo that you can make use of ive posted it on the repo however i think ive figured out how to get any library onto android.

Take a look at the process.

Navigate to the library settings in apde until you get to this screen

 ![Screenshot_20210524_144210_com.calsignlabs.apde](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/6ac57b23c114494cbd6e557e3e1b0a81cc5f1fea.jpeg)

Press dx dexer

 ![Screenshot_20210524_144708_com.calsignlabs.apde](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/0/0a6064f398e35269023da7d26fde284b9f6eaad9.jpeg)

Select the library you want to dex.(Note the ext requirements)

Then take a look at the files contained in the folder and their names.

 ![Screenshot_20210524_144222_com.android.documentsui](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/fa6e03d42e88a823eb3960674812e47b32ad06e6.jpeg)

As you can see i am dexing the fileWriter.jar library and so i need to name the output file fileWriter-dex.jar. Apde will dex your file and then you are good to go, i had no more crashes after this on my device.

Ive also managed to run my sketch with the jserial lib ive linked in a previous post.

i cant help any further as i dont have an arduino.

Ps

This is actually the solution posted by @jafal earlier in thread.

---

<div class="post-metadata">

**Author:** ![ZENAR57](https://avatars.discourse-cdn.com/v4/letter/z/a698b9/32.png) [@ZENAR57](https://discourse.processing.org/u/ZENAR57)\
**Post date:** [May 24, 2021, 2:32pm UTC](https://discourse.processing.org/t/apde-usb-otg-communication/30114/115 "2021-05-24T14:32:06Z")

</div>

> [@paulgoux](#):
>
> dont have an arduino.

and doesn’t need arduino.  
the question is not about the robustness of the libraries, but about how to glue them to APDE.

I got it pretty good with dfx, use many days.  
but where do I get the .jar files?  
try sticking the serial port library used by @glv . the link in the previous post is just above. Just stick it on, you don’t have to run it.

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [May 24, 2021, 2:54pm UTC](https://discourse.processing.org/t/apde-usb-otg-communication/30114/116 "2021-05-24T14:54:01Z")

</div>

Youll have to go with your preferred library, download them with a google search and copy the jar files into the appropriate folders.

The library im referring to was posted near the top of the convo, unfortunately its hard to scroll through such a long thread.

As for how to build libraries for android with eclipse.

> [@How to create android library eclipse?](https://discourse.processing.org/t/how-to-create-android-library-eclipse/28638):
>
> Hi everyone I’m hoping one of you can help please. I’ve been racking my brain trying to figure out how to extend my library to android. I already have a library which contains the following java files. Initially I just added the android libraries to my project and build path, and this seemed to clear up some of the code, however as I now understand traditionally the sketch extends PApplet whereas with android the sketch extends and therefore my current code wasn’t compatible. Having then …

Unfortunately i do not know how to do this in apde.

Now ive followed the thread as best i could and im pretty sure weve all posted solutions that work.

Help me understand which method you are using and what steps your are struggling with if anything.

Again following these steps i have managed to load the library with apde and run a sketch.

I jave not jeeded to amend any manifest or amend apde in any way.

> **[jSerialComm](https://fazecast.github.io/jSerialComm/)**
>
> jSerialComm : Platform-independent serial port access for Java

Download the jar

Add the jar to your library folder with the appropriate folder structure.

Import lib in apde.  
Dex lib in apde.  
Add import to the sketch.

To confirm i have made no use of the pc here to achieve any of this, phone only.

Done

---

<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:** [May 24, 2021, 3:29pm UTC](https://discourse.processing.org/t/apde-usb-otg-communication/30114/117 "2021-05-24T15:29:15Z")

</div>

@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

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

```

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
<?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>

```

i have tested many sketch all work fine

@ZENAR57 this is what i could reach

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [May 24, 2021, 3:37pm UTC](https://discourse.processing.org/t/apde-usb-otg-communication/30114/118 "2021-05-24T15:37:05Z")

</div>

Thank you and so i can understand, can you please explain the purpose of adding the device filter, is this a requirement for the library or the usb connection?

Also here is a link to the library @jafal was using

> **[inventit/processing-android-serial](https://github.com/inventit/processing-android-serial)**
>
> Android Serial Library for Processing. Contribute to inventit/processing-android-serial development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![ZENAR57](https://avatars.discourse-cdn.com/v4/letter/z/a698b9/32.png) [@ZENAR57](https://discourse.processing.org/u/ZENAR57)\
**Post date:** [May 24, 2021, 3:39pm UTC](https://discourse.processing.org/t/apde-usb-otg-communication/30114/119 "2021-05-24T15:39:39Z")

</div>

your code completely works 100%.  
full match of manifest files and device\_filter.xml.  
BUT!  
THE LIBRARY THAT YOU USED ON YOUR PERSONAL COMPUTER, COULD YOU INSTALL (ADHESIVE) TO YOUR PHONE-ANDROID-APDE?

---

<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:** [May 24, 2021, 3:48pm UTC](https://discourse.processing.org/t/apde-usb-otg-communication/30114/120 "2021-05-24T15:48:30Z")

</div>

> [@paulgoux](#):
>
> device filter, is this a requirement for the library or the usb connection?

it is indicate that hardware connected and determined the usb driver chip there is other device filter files could be used i am going to compile sketch with out this file and see what happen

[Previous page](https://discourse.processing.org/t/apde-usb-otg-communication/30114.md?page=5)

[Next page](https://discourse.processing.org/t/apde-usb-otg-communication/30114.md?page=7)
