Looking for net library alternative

edited September 2017 in Android Mode

Hello guys, With a friend of mine I am working on a telemetry system for my sailing classes. The components exchange their data via JSON on WiFi TCP massages.

The communication allready works fine and even sharing the data with a processing desktop application works but I planed to use a Android device to visualize the data online during the lessons.

I noticed that the net library does not exists in the Android mode and I found the OscP5 as well as the ketai library which shall include a TCP client class. I look through the examples but just found one in OscP5 but the example wouldn't work on my Nexus an the TCP server on my network. I am not sure whether it is because the Osc massage protocol or the TCP connection. My idea was to use the netP5 library which lies underneath oscP5 to establish the connection without the Osc stuff and unpack them with the JSON object. But I was not able to find examples how to use this lib.

Has anyone experience with the netP5 library and can help me? Or does anyone know another way to connect a processing app to the TCP server?

Answers

  • What sort of data are you transferring? Using oscP5 requires you to use the provided calls from this library. One thing you could do (untested) is to transfer blob data (binary objects). I have used the oscP5 on Android before and it works fine. If you provide more details about what data are you transferring and how do you use this data, people can provide you more ideas.

    Kf

  • As mentioned we transfer JSON strings. To be more precise from the sonsors it is something like "{'wDir':270,'wSpeed':4.5}. The mainmodule sends out commands to configure the sensors in the same protocol.

    The rest of the system works so we didn't planted to change the communication protocol. On the Java mode on a desktop using TCP is quiet simple and I thought in Android I would be easy as well. Bad luck.

    I read my post here I the forum and the most were pointing to oscP5. The is able to transmit on TCP as network layer but direktly comes with the open sound stuff.

    I need only to open a smoker, connect to my dad's recorder as server, send one string as identification and afterwards receiving strings with data. The parsing of JSON is included in processing and does not need to import anything.

  • edited August 2017

    @xj_diva===

    i want to be sure to understand:

    • you have a server
    • you can use it with "normal" computers connected wifi using TCP
    • you want now to stabilish the same kind of connection between your server and 1 android smartphone client in order to send && receive data (json) is it exact?

    if yes i dont see why you dont use java (on the smartphone): you dont need any library for that. The only special problem with android is that you must use an AsyncTask.

  • @akenaton

    I am not familiar with java and just found the references of net library. The client is realized by object an very easy to use. I had the hope that there maid be a easy solution as well.

    I found this tutorial with I modified in a way that my laptop is able to read the my massages but very slow. what do I have to do to get this running on my android device? I just found other hints about that AsyncTask but don't understand what I have to do.

    import java.net.*;
    import java.io.*;
    
    Socket myClient = null;
    DataInputStream input = null;
    DataOutputStream output = null;
    
    String string = "";
    
    void setup(){  
    try {
        myClient = new Socket("192.168.4.1",8888);
        input = new DataInputStream(myClient.getInputStream());
        output = new DataOutputStream(myClient.getOutputStream());
      }
      catch (IOException e) {
        System.out.println(e);
      }  
    }
    
    void draw(){
      char in;
      try{
        in = (char)input.read();
        string += in;
        if (in =='}'){
          System.out.println(string);
          string = "";
        }
      }
      catch (IOException e){
        System.out.println(e);
      }  
    }
    
    void stop(){
      try{
        input.close();
        output.close();
        myClient.close();
      }
      catch (IOException e){
        System.out.println(e);
      }
    }
    
  • @xj_diva===

    yes, this code can run; in order to accelerate try to use BufferedReader::

        input= new BufferedReader(new InputStreamReader(myClient.getInputStream()));
    

    (the same with output and printWriter)

    Then, for android , you have a) to create an activity, b) to create a class for tcp using either an asyncTask or a runnable and put the code inside this class (called by the launcher).That s all! - No library!

  • @akenaton

    Thank you for your hints. In generell my codes works in the Java modem and prints incoming strings. When I switch to Android mode the code stuck while creating the socket condition.

    Further I read about the asyncTask for example here but wasn't able to get nothing running. Do you have an example code which helps me?

    The other thing is I wan to do read the string continuously, parse it and use the values to draw the values on indication instruments. Is the asyncTask the right tools to use?

  • @xj_diva===

    put the code (android client) you have tried till now and i ll give a look at it. As for the other question depends: asynctask is easy to use but not for long processes (android doc:: < 5ms); in your case (json) it could be ok; but in other cases the best way can be to use an handler or a thread.

    more details: https://developer.android.com/topic/performance/threads.html

  • @akenaton

    Thank you for your help. I was testing my example to post it here when I realized that processing is not able to import android.os. I will try to fix it again and report back afterwards.

  • @xj_diva===

    what do you mean with "P5 is not able to..."???

  • @akenaton processing is not able to find android.os.AsyncTasker object so perhaps I installed the android mode or the SDK wrongly

  • @xj_diva===

    try this:

            import android.os.AsyncTask;
    
    
    
            void setup(){
    
              background (255,0,0);
              size(600,800);
              orientation(PORTRAIT);
              new chargeJSON().execute();
            };
    
            void draw(){
              background(255,0,0);
              textSize(36);
              text ("Of course it works",50,350);
    
            };
    
            void mouseReleased(){
              this.getActivity().finish();
            };
    

    ///////////////////////////////////////////////////////////////////////

            public   class chargeJSON extends AsyncTask< Void, Void, String> {
    
    
    
                    @Override
                    protected String  doInBackground(Void... params) {
    
    
    
    
                      return "achevé";
                  }
                    protected void onPostExecute(String result){
                      if(result.equals("achevé")){
    
                      System.out.println("resultat=======" + result + "  ");
                      }
                    }
    
                   }
    
Sign In or Register to comment.