NEP+ Docs
  • NEP+ middleware documentation
  • Developer Tools
    • NEP CLI
  • User interfaces
    • Install NEP+ App (Windows)
    • Install NEP+ App (macOS)
    • NEP+ App (0.0.4) - Deprecated
  • Concepts for developers
    • Message Format
    • Tutorials for developers
      • Connecting Multiple Scripts on the Same Computer using NEP+ Tools: A Step-by-Step Tutorial
      • Connecting 2 scripts in a local network (e.g., two computers on the same Wifi network)
      • Connecting ROS to non-ROS nodes using NEP+ tools
  • Programming enviroments
    • Python
    • C# (Unity)
    • C# (Visual Studio)
    • Javascript (Node.js)
    • Java/Kotlin (Android Studio)
    • Java (Netbeans)
    • C++ (Visual Studio)
  • Other tutorials
    • Pepper & NAO robots
      • Using Pepper without Pepper in Choregraphe
      • Install Pepper/NAO SDK
  • Code examples
    • Sending images from Python and Opencv
Powered by GitBook
On this page
  • Using Java
  • Download .jar library
  • Include .jar library
  • Create a simple publisher:
  • Add a simple subscriber:
  • Using Koltin (Soon ...)
  1. Programming enviroments

Java/Kotlin (Android Studio)

Last updated 1 year ago

Using Java

Download .jar library

Download the NEP Java library using the next link:

Download nep-0.0.1.jar

This tutorial needs NEP-CLI to version 0.2.0 or above, or NEP+ App 0.0.5 or above.

Include .jar library

  1. Copy .jar to the "libs" Directory: Navigate to the apps/libs directory in the Android Studio project and copy and paste the .jar file, such as the figure below.

  1. Include the required .jar file in Your Project: In Android Studio open build.gradle(Module:app) located in Grandle Scripts -> build.gradle and the next dependencies:

bundle.gradle(:app)
implementation 'org.zeromq:jeromq:0.5.2'
implementation 'com.google.code.gson:gson:2.10.1'
implementation files('/libs/nep-0.0.1.jar')
  1. Sync Gradle Files: Click on the "Sync Project with Gradle Files" button in the toolbar. This action ensures that the newly added library is properly included in your project's build configuration.

  2. Verify the Gradle Sync: Once the Gradle sync is complete, verify that there are no errors in the Gradle console.

  3. Configure Network Permissions: Since your app will be using network sockets, you need to ensure that your AndroidManifest.xml file includes the necessary permissions. Add the following permission to your manifest file:

Android.Manifest.xml
<uses-permission android:name="android.permission.INTERNET" />

Create a simple publisher:

Import NEP libraries to the MainActivity file as follows:

MainActivity.java
import nep.Node;
import nep.Publisher;

Modify the MainActivity class as follows:

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private Publisher pub;
    private Node node;
    private String nepMasterIP = "192.168.0.178";
    private int index = 0;

    private static class JsonValue {
        int result;

        public JsonValue(int result) {
            this.result = result;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        node = new Node("android_test");
        pub = node.new_pub("test", "json", nepMasterIP);
    }

    public void onClickBtn(View v)
    {

        if (pub.started)
        {
            JsonValue jsonValue = new JsonValue(index++);
            pub.publish(jsonValue);
        }

    }

Update nepMasterIP (line 6) with the IP address of the computer running the NEP master server (using NEP-CLI or NEP+ App).

Add a button in activity_main.xml and inside the XML layout (code ) file set its onClick attribute to execute a function named onClickBtn

activity_main.xml
   <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClickBtn"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

In this example onClick attribute is set in line 5.

Add a simple subscriber:

For this example, we need to import the next packages/libraries:

import nep.Subscriber;
import com.google.gson.Gson;
import android.os.AsyncTask;
import java.io.IOException;

To the previous code, we will add a background thread or AsyncTask to perform socket communication operations. AsyncTask is a convenient way to perform background tasks and update the UI thread with the results.

Here's an example of how you can define an AsyncTask to handle socket communication:


public class SubscriberTask extends AsyncTask<Void, String, Void> {

        private Node node;
        private Subscriber sub;

        // Constructor to initialize the node
        public SubscriberTask(Node node) {
            this.node = node;
        }

        @Override
        protected Void doInBackground(Void... params) {
            // Perform socket communication operations here
            // This method runs on a background thread
            // Create a new subscriber with the topic <'test'>
            sub = node.new_sub("test", "json", nepMasterIP);


            while (!isCancelled()) {
                String msg = null; // Message obtained as an String
                try {
                    msg = sub.listen();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                if (!"{}".equals(msg)) // If message is not {}
                {
                    System.out.println(msg);
                    // Then, convert string to Java object
                    Gson gson = new Gson();
                    JsonValue obj = gson.fromJson(msg,JsonValue.class);
                    // Print message
                    System.out.println(obj.result);
                }
            }

            // Clean up resources when finished
            sub.close();
            return null;
        }

        @Override
        protected void onProgressUpdate(String... values) {
            // Update the UI with the received message
            String message = values[0];
            // Handle the received message as needed
        }
    }

After defining the AsyncTask, you can create an instance of it and execute it to start listening to the subscriber. You can execute the AsyncTask in the onCreate() method of your Activity or wherever appropriate.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        node = new Node("android_test");
        pub = node.new_pub("test", "json", nepMasterIP);
        subscriberTask = new SubscriberTask(node);
        subscriberTask.execute();
    }

Ensure that you properly handle cleanup when your AsyncTask is no longer needed. You can cancel the AsyncTask and clean up resources in the onDestroy() method of your Activity or wherever appropriate.

   @Override
    protected void onDestroy() {
        super.onDestroy();
        if (subscriberTask != null) {
            subscriberTask.cancel(true);
        }
    }

An example of putting it all together is shown below:

MainActivity.java
...

import nep.Node;
import nep.Publisher;

import nep.Subscriber;
import com.google.gson.Gson;
import android.os.AsyncTask;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private Publisher pub;
    private Node node;
    private String nepMasterIP = "192.168.0.178";
    private int index = 0;

    SubscriberTask subscriberTask;

    private static class JsonValue {
        int result;

        public JsonValue(int result) {
            this.result = result;
        }
    }


    public class SubscriberTask extends AsyncTask<Void, String, Void> {

        private Node node;
        private Subscriber sub;

        // Constructor to initialize the node
        public SubscriberTask(Node node) {
            this.node = node;
        }

        @Override
        protected Void doInBackground(Void... params) {
            // Perform socket communication operations here
            // This method runs on a background thread
            // Create a new publisher with the topic <'test'>
            sub = node.new_sub("test", "json", nepMasterIP);


            while (!isCancelled()) {
                String msg = null; // Message obtained as an String
                try {
                    msg = sub.listen();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                if (!"{}".equals(msg)) // If message is not {}
                {
                    System.out.println(msg);
                    // Then, convert string to Java object
                    Gson gson = new Gson();
                    JsonValue obj = gson.fromJson(msg,JsonValue.class);
                    // Print message
                    System.out.println(obj.result);
                }
            }

            // Clean up resources when finished
            sub.close();
            return null;
        }

        @Override
        protected void onProgressUpdate(String... values) {
            // Update the UI with the received message
            String message = values[0];
            // Handle the received message as needed
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        node = new Node("android_test");
        pub = node.new_pub("test", "json", nepMasterIP);
        subscriberTask = new SubscriberTask(node);
        subscriberTask.execute();
    }

    public void onClickBtn(View v)
    {

        if (pub.started)
        {
            JsonValue jsonValue = new JsonValue(index++);
            pub.publish(jsonValue);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (subscriberTask != null) {
            subscriberTask.cancel(true);
        }
    }
}

Using Koltin (Soon ...)