How to run the sketch in Android mode.

edited February 2016 in Android Mode

Hello,

i made the following sketch to communicate with my arduino. The sketch is running fine in java mode but my goal is to run the sketch on my phone:

import processing.serial.*;
Serial myPort; // Objekt der Serial-Klasse
char HEADER = 'H'; // Zeichen zur Identifikation des Anfangs einer Nachricht
short LF = 10; // ASCII-Linefeed
float kmhArduino;
int distArduino;
int siebenArduino;
PFont font;
short portIndex = 0; // com-Port wa¨hlen, 0 ist der erste Port

void setup() {
  size(480, 800);
  font = loadFont ("Arial-BoldMT-48.vlw");
  println(Serial.list());
  println(" Verbinde mit -> " + Serial.list()[portIndex]);
  myPort = new Serial(this, Serial.list()[portIndex], 9600);
}
void draw() {
  background(0, 0, 50);
  fill(255, 0, 0);
  textFont(font, 48);
  text("Sk8tacho V1.0", 50, 100);
  textFont(font, 40);
  fill(255, 255, 0);
  text(kmhArduino + "kmh", 100, 300);
  text("Distance: " + distArduino + "m", 50, 400);
  fill(255, 0, 0);
  textFont(font, 30);
  text("made by inventED 2015", 50, 700);
  }
void serialEvent(Serial p)
{
  String message = myPort.readStringUntil(LF); // Serielle Daten einlesen
  if (message != null)
  {
    print(message);
    String [] data = message.split(","); // Kommaseparierte Nachricht zerlegen
    if (data[0].charAt(0) == HEADER && data.length > 3) // check validity
    {
      kmhArduino = float(data[1]);
      distArduino = int(data[2]);
      //siebenArduino = int(data[7]); // falls mehr werte gebraucht werden
    }
  }
}

i already tried to convert it to android mode. but when i switch to android mode i get this errors in the console:

Exception in thread "Thread-126" java.lang.NullPointerException
    at processing.mode.android.AndroidBuild.setSdkTarget(AndroidBuild.java:76)
    at processing.mode.android.AndroidEditor.updateSdkMenu(AndroidEditor.java:347)
    at processing.mode.android.AndroidEditor.access$300(AndroidEditor.java:50)
    at processing.mode.android.AndroidEditor$7.run(AndroidEditor.java:249)

Here is my converted android mode code:

//import processing.serial.*;
//Serial myPort;  
import android.os.Bundle;
import ketai.net.bluetooth.*;
KetaiBluetooth bt;

char HEADER = 'H'; // Zeichen zur Identifikation des Anfangs einer Nachricht
short LF = 10; // ASCII-Linefeed
float kmhArduino;
int distArduino;
int siebenArduino;
PFont font;
short portIndex = 0; // com-Port wa¨hlen, 0 ist der erste Port

void setup() {
  size(480, 800);
  font = loadFont ("Arial-BoldMT-48.vlw");
  /*println(Serial.list());
  println(" Verbinde mit -> " + Serial.list()[portIndex]);*/
  //myPort = new Serial(this, Serial.list()[portIndex], 9600);
  bt.start(); // BT Service starten.
  //***************************************************************
  // Hier muss die individuelle Adresse deines BT Moduls eingegeben werden.
  bt.connectDevice("98:D3:31:30:2C:04"); // HC-05 BT-Modul
  //bt.connectDevice("00:13:01:06:20:83"); // HC-05 BT-Modul
  //bt.connectDevice("00:11:12:11:04:73"); // BT-Shield Arduino
  //***************************************************************
}
void draw() {
  background(0, 0, 50);
  fill(255, 0, 0);
  textFont(font, 48);
  text("Sk8tacho V1.0", 50, 100);
  textFont(font, 40);
  fill(255, 255, 0);
  text(kmhArduino + "kmh", 100, 300);
  text("Distance: " + distArduino + "m", 50, 400);
  fill(255, 0, 0);
  textFont(font, 30);
  text("made by inventED 2015", 50, 700);
  }
/*void serialEvent(Serial p)
{
  String message = myPort.readStringUntil(LF); // Serielle Daten einlesen
  if (message != null)
  {
    print(message);
    String [] data = message.split(","); // Kommaseparierte Nachricht zerlegen
    if (data[0].charAt(0) == HEADER && data.length > 3) // check validity
    {
      kmhArduino = float(data[1]);
      distArduino = int(data[2]);
      //siebenArduino = int(data[7]); // falls mehr werte gebraucht werden
    }
  }
}*/
void onBluetoothDataEvent(String who, String[]data) //Callback Methode: Es liegen Daten vom Arduino an der BT-Schnittstelle an.
{
    if (data != null)
  {
    //print(message);
    //message.split(","); // Kommaseparierte Nachricht zerlegen
    if (data[0].charAt(0) == HEADER && data.length > 3) // check validity
    {
      kmhArduino = float(data[1]);
      distArduino = int(data[2]);
      //siebenArduino = int(data[7]); // falls mehr werte gebraucht werden
    }
  }
}        

when i try to run the sketch i get this error in the console:

BUILD FAILED
C:\Users\ED\Documents\Processing\modes\AndroidMode\sdk\tools\ant\build.xml:538: Unable to resolve project target 'android-15'

Total time: 0 seconds

Would be really cool if anybody can help me out!! I´m pretty new to coding and processing, sorry if something is not clear.

I´m using Windows 7 64-bit, tried Processing 3.0.2 windows 64-bit version and 32-bit version.

Thank you!

Answers

  • Answer ✓

    do you have android sdk installed? specifically api level 15?

    android processing is very picky about version numbers.

  • no. i thought by installing the android mode this is also done. i´m going to install the sdk. is there something more i have to do in the sdk?

    thanks a lot for your fast answer!

  • now i´ve installed android 4.0.3 in the sdk manager. but the error is sadly the same. are there some more things i should install in the sdk?

    Thank you!

  • edited February 2016

    so when i switch to android mode it says:

    Exception in thread "Thread-63" java.lang.NullPointerException
        at processing.mode.android.AndroidBuild.setSdkTarget(AndroidBuild.java:76)
        at processing.mode.android.AndroidEditor.updateSdkMenu(AndroidEditor.java:347)
        at processing.mode.android.AndroidEditor.access$300(AndroidEditor.java:50)
        at processing.mode.android.AndroidEditor$7.run(AndroidEditor.java:249)
    

    under error section it says:

    The class "PApplet" does not exist The class "PApplet" does not exist (double-click for suggestions) sketch_160221e 2

    how can i get the android mode running?

  • edited February 2016

    I installed the Java SDK and now it´s working.

    Now i tried to run my android sketch on my device. I already selected my device at the android tab. When i press "run on device" its building the android projekt. there are some warnings in the console:

    ----------
    1. WARNING in C:\Users\ED\AppData\Local\Temp\android1020177072255040272sketch\src\processing\test\comm_3wertev4bt\comm_3werteV4BT.java (at line 4)
        import processing.data.*; 
               ^^^^^^^^^^^^^^^
    The import processing.data is never used
    ----------
    2. WARNING in C:\Users\ED\AppData\Local\Temp\android1020177072255040272sketch\src\processing\test\comm_3wertev4bt\comm_3werteV4BT.java (at line 5)
        import processing.event.*; 
               ^^^^^^^^^^^^^^^^
    The import processing.event is never used
    ----------
    3. WARNING in C:\Users\ED\AppData\Local\Temp\android1020177072255040272sketch\src\processing\test\comm_3wertev4bt\comm_3werteV4BT.java (at line 6)
        import processing.opengl.*; 
               ^^^^^^^^^^^^^^^^^
    The import processing.opengl is never used
    ----------
    4. WARNING in C:\Users\ED\AppData\Local\Temp\android1020177072255040272sketch\src\processing\test\comm_3wertev4bt\comm_3werteV4BT.java (at line 8)
        import android.os.Bundle; 
               ^^^^^^^^^^^^^^^^^
    The import android.os.Bundle is never used
    ----------
    5. WARNING in C:\Users\ED\AppData\Local\Temp\android1020177072255040272sketch\src\processing\test\comm_3wertev4bt\comm_3werteV4BT.java (at line 11)
        import java.util.HashMap; 
               ^^^^^^^^^^^^^^^^^
    The import java.util.HashMap is never used
    ----------
    6. WARNING in C:\Users\ED\AppData\Local\Temp\android1020177072255040272sketch\src\processing\test\comm_3wertev4bt\comm_3werteV4BT.java (at line 12)
        import java.util.ArrayList; 
               ^^^^^^^^^^^^^^^^^^^
    The import java.util.ArrayList is never used
    ----------
    7. WARNING in C:\Users\ED\AppData\Local\Temp\android1020177072255040272sketch\src\processing\test\comm_3wertev4bt\comm_3werteV4BT.java (at line 13)
        import java.io.File; 
               ^^^^^^^^^^^^
    The import java.io.File is never used
    ----------
    8. WARNING in C:\Users\ED\AppData\Local\Temp\android1020177072255040272sketch\src\processing\test\comm_3wertev4bt\comm_3werteV4BT.java (at line 14)
        import java.io.BufferedReader; 
               ^^^^^^^^^^^^^^^^^^^^^^
    The import java.io.BufferedReader is never used
    ----------
    9. WARNING in C:\Users\ED\AppData\Local\Temp\android1020177072255040272sketch\src\processing\test\comm_3wertev4bt\comm_3werteV4BT.java (at line 15)
        import java.io.PrintWriter; 
               ^^^^^^^^^^^^^^^^^^^
    The import java.io.PrintWriter is never used
    ----------
    10. WARNING in C:\Users\ED\AppData\Local\Temp\android1020177072255040272sketch\src\processing\test\comm_3wertev4bt\comm_3werteV4BT.java (at line 16)
        import java.io.InputStream; 
               ^^^^^^^^^^^^^^^^^^^
    The import java.io.InputStream is never used
    ----------
    11. WARNING in C:\Users\ED\AppData\Local\Temp\android1020177072255040272sketch\src\processing\test\comm_3wertev4bt\comm_3werteV4BT.java (at line 17)
        import java.io.OutputStream; 
               ^^^^^^^^^^^^^^^^^^^^
    The import java.io.OutputStream is never used
    ----------
    12. WARNING in C:\Users\ED\AppData\Local\Temp\android1020177072255040272sketch\src\processing\test\comm_3wertev4bt\comm_3werteV4BT.java (at line 18)
        import java.io.IOException; 
               ^^^^^^^^^^^^^^^^^^^
    The import java.io.IOException is never used
    ----------
    12 problems (12 warnings)
    

    When it´s finished my phone is trying to start the app but it´s suddenly stopping and in the console debug info is comming:

    FATAL EXCEPTION: Animation Thread
    Process: processing.test.comm_3wertev4bt, PID: 28101
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean ketai.net.bluetooth.KetaiBluetooth.start()' on a null object reference
        at processing.test.comm_3wertev4bt.comm_3werteV4BT.setup(comm_3werteV4BT.java:42)
        at processing.core.PApplet.handleDraw(Unknown Source)
        at processing.core.PGraphicsAndroid2D.requestDraw(Unknown Source)
        at processing.core.PApplet.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:818)
    

    Got someone an idea for me please? I got no idea whats wrong with my sketch because it´s running in java mode. so i think my problem is related to converting my sketch to android mode.

    Thank you.

  • edited February 2016 Answer ✓

    Try initializing bt, KetaiBluetooth bt;, KetaiBluetooth bt = new KetaiBluetooth(this);

  • Answer ✓

    i think you can ignore the import warnings - processing includes a lot of files just in case you use them.

  • edited February 2016 Answer ✓

    from ketai, and check in "sketch permissions" bluetooth, bletooth_admin

       import android.content.Intent;  
    
         //********************************************************************
            // The following code is required to enable bluetooth at startup.
            //********************************************************************
            void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              bt = new KetaiBluetooth(this);
            }
    
            void onActivityResult(int requestCode, int resultCode, Intent data) {
              bt.onActivityResult(requestCode, resultCode, data);
            }
    
  • Hey thank you for all for your help!

    @Deathy:

    i already got this in my sketch: KetaiBluetooth bt; so should i just add this?: KetaiBluetooth bt = new KetaiBluetooth(this);

    tried this but i get some errors from inside the android tools:

    ----------
    1. ERROR in C:\Users\ED\AppData\Local\Temp\android6441321696292789548sketch\src\processing\test\comm_3wertev4bt\comm_3werteV4BT.java (at line 26)
        KetaiBluetooth bt;
                       ^^
    Duplicate field comm_3werteV4BT.bt
    ----------
    2. ERROR in C:\Users\ED\AppData\Local\Temp\android6441321696292789548sketch\src\processing\test\comm_3wertev4bt\comm_3werteV4BT.java (at line 27)
        KetaiBluetooth bt = new KetaiBluetooth(this);
                       ^^
    Duplicate field comm_3werteV4BT.bt
    ----------
    2 problems (2 errors)
    
    BUILD FAILED
    C:\Users\ED\AppData\Local\Temp\android6441321696292789548sketch\build.xml:15: The following error occurred while executing this line:
    C:\Users\ED\AppData\Local\Temp\android6441321696292789548sketch\build.xml:28: Compile failed; see the compiler error output for details.
    
    Total time: 2 seconds
    

    @camperos:

    thank you! i forget about the permissions. but what about the code you posted? should i add the code to my sketch?

    After setting the permissions i got a new debug report:

    debug:
    FATAL EXCEPTION: Animation Thread
    Process: processing.test.comm_3wertev4bt, PID: 8776
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean ketai.net.bluetooth.KetaiBluetooth.start()' on a null object reference
    FATAL EXCEPTION: Animation Thread
        at processing.test.comm_3wertev4bt.comm_3werteV4BT.setup(comm_3werteV4BT.java:42)
    Process: processing.test.comm_3wertev4bt, PID: 8776
        at processing.core.PApplet.handleDraw(Unknown Source)
        at processing.core.PGraphicsAndroid2D.requestDraw(Unknown Source)
        at processing.core.PApplet.run(Unknown Source)
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean ketai.net.bluetooth.KetaiBluetooth.start()' on a null object reference
        at java.lang.Thread.run(Thread.java:818)
    FATAL EXCEPTION: Animation Thread
        at processing.test.comm_3wertev4bt.comm_3werteV4BT.setup(comm_3werteV4BT.java:42)
    FATAL EXCEPTION: Animation Thread
    Process: processing.test.comm_3wertev4bt, PID: 8776
    Process: processing.test.comm_3wertev4bt, PID: 8776
        at processing.core.PApplet.handleDraw(Unknown Source)
    FATAL EXCEPTION: Animation Thread
    Process: processing.test.comm_3wertev4bt, PID: 8776
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean ketai.net.bluetooth.KetaiBluetooth.start()' on a null object reference
        at processing.test.comm_3wertev4bt.comm_3werteV4BT.setup(comm_3werteV4BT.java:42)
        at processing.core.PApplet.handleDraw(Unknown Source)
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean ketai.net.bluetooth.KetaiBluetooth.start()' on a null object reference
        at processing.core.PGraphicsAndroid2D.requestDraw(Unknown Source)
    FATAL EXCEPTION: Animation Thread
    Process: processing.test.comm_3wertev4bt, PID: 8776
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean ketai.net.bluetooth.KetaiBluetooth.start()' on a null object reference
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean ketai.net.bluetooth.KetaiBluetooth.start()' on a null object reference
        at processing.core.PGraphicsAndroid2D.requestDraw(Unknown Source)
        at processing.core.PApplet.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:818)
        at processing.test.comm_3wertev4bt.comm_3werteV4BT.setup(comm_3werteV4BT.java:42)
        at processing.test.comm_3wertev4bt.comm_3werteV4BT.setup(comm_3werteV4BT.java:42)
        at processing.core.PApplet.handleDraw(Unknown Source)
        at processing.core.PGraphicsAndroid2D.requestDraw(Unknown Source)
        at processing.core.PApplet.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:818)
        at processing.core.PApplet.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:818)
        at processing.test.comm_3wertev4bt.comm_3werteV4BT.setup(comm_3werteV4BT.java:42)
        at processing.core.PApplet.handleDraw(Unknown Source)
        at processing.core.PApplet.handleDraw(Unknown Source)
        at processing.core.PGraphicsAndroid2D.requestDraw(Unknown Source)
        at processing.core.PApplet.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:818)
        at processing.core.PGraphicsAndroid2D.requestDraw(Unknown Source)
        at processing.core.PApplet.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:818)
    

    Thanks for helping!

  • Those errors are pretty clear. Looks like an error with your code (that you haven't posted)

  • edited February 2016

    Okay....thats bad.

    I posted my running java mode skech and my android version in the first post.

    To prevent running into code problems i fist made a running java mode sketch. I tried to leave the code like in the java mode version. can you please have a look on my code?

    //import processing.serial.*;
    //Serial myPort;  
    import android.os.Bundle;
    import ketai.net.bluetooth.*;
    KetaiBluetooth bt;
    
    char HEADER = 'H'; // Zeichen zur Identifikation des Anfangs einer Nachricht
    short LF = 10; // ASCII-Linefeed
    float kmhArduino;
    int distArduino;
    int siebenArduino;
    PFont font;
    short portIndex = 0; // com-Port wa¨hlen, 0 ist der erste Port
    
    void setup() {
      size(480, 800);
      font = loadFont ("Arial-BoldMT-48.vlw");
      /**println(Serial.list());
      println(" Verbinde mit -> " + Serial.list()[portIndex]);*/
      //myPort = new Serial(this, Serial.list()[portIndex], 9600);
      bt.start(); // BT Service starten.
      //***************************************************************
      // Hier muss die individuelle Adresse deines BT Moduls eingegeben werden.
      bt.connectDevice("98:D3:31:30:2C:04"); // HC-05 BT-Modul
      //bt.connectDevice("00:13:01:06:20:83"); // HC-05 BT-Modul
      //bt.connectDevice("00:11:12:11:04:73"); // BT-Shield Arduino
      //***************************************************************
    }
    
    void draw() {
      background(0, 0, 50);
      fill(255, 0, 0);
      textFont(font, 48);
      text("Sk8tacho V1.0", 50, 100);
      textFont(font, 40);
      fill(255, 255, 0);
      text(kmhArduino + "kmh", 100, 300);
      text("Distance: " + distArduino + "m", 50, 400);
      fill(255, 0, 0);
      textFont(font, 30);
      text("made by inventED 2015", 50, 700);
      }
    /**void serialEvent(Serial p)
    {
      String message = myPort.readStringUntil(LF); // Serielle Daten einlesen
      if (message != null)
      {
        print(message);
        String [] data = message.split(","); // Kommaseparierte Nachricht zerlegen
        if (data[0].charAt(0) == HEADER && data.length > 3) // check validity
        {
          kmhArduino = float(data[1]);
          distArduino = int(data[2]);
          //siebenArduino = int(data[7]); // falls mehr werte gebraucht werden
        }
      }
    }*/
    
    void onBluetoothDataEvent(String who, String[]data) //Callback Methode: Es liegen Daten vom Arduino an der BT-Schnittstelle an.
    {
        if (data != null)
      {
        //print(message);
        //message.split(","); // Kommaseparierte Nachricht zerlegen
        if (data[0].charAt(0) == HEADER && data.length > 3) // check validity
        {
          kmhArduino = float(data[1]);
          distArduino = int(data[2]);
          //siebenArduino = int(data[7]); // falls mehr werte gebraucht werden
        }
      }
    }        
    

    Thank you...

  • edited February 2016

    WOW Thank you!! My code running on my smartphone...that feels nice.

    but.....

    sadly i´m not receiving the data. I think this is the problem:

    debug report: Did not find onBluetoothDataEvent callback method.

    I got a problem in my code in this section:

    void onBluetoothDataEvent(String who, String[]data) //Callback Methode: Es liegen Daten vom Arduino an der BT-Schnittstelle an.
    {
        if (data != null)
      {
        //print(message);
        //message.split(","); // Kommaseparierte Nachricht zerlegen
        if (data[0].charAt(0) == HEADER && data.length > 3) // check validity
        {
          kmhArduino = float(data[1]);
          distArduino = int(data[2]);
          //siebenArduino = int(data[7]); // falls mehr werte gebraucht werden
        }
      }
    }
    

    In my Java serial connection sketch i did it like this:

    void serialEvent(Serial p)
    {
      String message = myPort.readStringUntil(LF); // Serielle Daten einlesen
      if (message != null)
      {
        print(message);
        String [] data = message.split(","); // Kommaseparierte Nachricht zerlegen
        if (data[0].charAt(0) == HEADER && data.length > 3) // check validity
        {
          kmhArduino = float(data[1]);
          distArduino = int(data[2]);
          //siebenArduino = int(data[7]); // falls mehr werte gebraucht werden
        }
      }
    }
    

    I got no idea who can fix this... it took weeks for me to get the java sketch running, but this time i´m lost.

    But the Programm is running on my phone... this is great! :)

    Thanks

  • just print (log) the who and data objects to start with. then you can see what you need to do with them to extract the payload.

  • sorry i´m very new to this....I tried a couple of things but.....

    How can i print who and data as easy as possible? because it´s already array and not like in the java serial version a complete string. so i didn´t get it to just print it. i tried some things but nothing worked. i found another code and now i can see some data flickering across the screen:

    void onBluetoothDataEvent(String who, byte[] data) //Callback Methode: Es liegen Daten vom Arduino an der BT-Schnittstelle an.
    {
    //Byte Array elementweise in Char umwandeln und dem String hinzufügen.
    for (int i=0; i<data.length; i++ ) {
    echoArduino+=String.valueOf((char)data[i]);
    }
    text(echoArduino, 50, 200); // Empfangene Daten in schwarzer Schrift darstellen.
    echoArduino=""; // Stingpufer zurück setzen.
    }
    

    Can you please give me a hint how to work with the array? Sorry for my english.

    Thanks

  • Is there an easy way to print the who and data objects? sorry i´m not getting it...

    Thank you.

  • edited February 2016

    Now i got it....i receive the data from my arduino via bluetooth!

    But one thing isn´t perfect:

    I receive the data with this sketch very slow (sometimes it takes 2sec until value is refreshed). I think this problem is related to " if ((char)data[data.length-1]=='\n') dataEnd = true;", but its needed to identify my data clearly. When i´m only text/print (log) the value of data i can see data comming in from the arduino very fast.

    void onBluetoothDataEvent(String who, byte[] data) 
        {
            for (int i=0; i<data.length; i++ ) {
            echoArduino +=String.valueOf((char)data[i]);
            }
           if ((char)data[data.length-1]=='\n') dataEnd = true;
           if (dataEnd) {
           feedbackArdu = split(echoArduino, ',')[0]; 
          sensorArdu = split(echoArduino, ',')[1]; 
          echoArduino=""; 
            dataEnd = false; 
          }
        }
    

    Anybody got an idea for another faster solution. Or is something wrong in the skech? Should i post this problem in another category?

    Thank you.

Sign In or Register to comment.