Open Core Interface - MLPI
MLPI-MotionLogicProgrammingInterface(mlpi4Java)  1.26.2
Creating a new Java project for Android using Eclipse

Introduction

This manual shows how to set up a Java project on Eclipse running on Android smart devices which uses the MLPI to communicate with the target device.

Preparation

To start programming in Java, you need a working Java environment. By default, this includes:

  • Java SE (runtime environment), to run Java applications.
  • Java JDK (Java developing environment), to compile Java applications.
  • Eclipse for Java developers (Integrated Development Environment), to edit you code.
  • Android ADT Eclipse plugin, to edit Android code in Eclipse.
  • Android SDK, to compile android apps.

The Java downloads can be found and can be downloaded by courtesy of oracle: http://www.oracle.com/technetwork/java/javase/downloads/index.html. Eclipse can be downloaded at http://www.eclipse.org/downloads/. You can choose between 32-bit and 64-bit version. For this tutorial, "Eclipse for Java Developers - Juno(4.2)" has been used. But older versions of Eclipse(3.6) have also been successfully tested. For all downloads and informations regarding Android app development, it is recommended to have a look at http://developer.android.com/. There, you can find detailed instructions and articles how to program and develop for the Android platform.

Note
Please make sure that you are at least familiar with Android app development. Be sure that you have successfully created and downloaded an Android app without MLPI to your phone or tablet before proceeding with integration of the MLPI.

Creating a new Java project

Start by creating a new "Android Application Project" in your workspace:

eclipseAndNewProject.png

In this example, Android 4.0 is targeted. But MLPI has successfully been tested to also work with older versions, like Android 2.3. Finish the wizard and create an app with a single activity.

To use the MLPI from Java, you need to add the javamlpi.jar library which can be found in the folder mlpi4Java/lib of the MLPI-SDK. Add the MLPI to an existing project, by just dragging and dropping the javamlpi.jar from the lib folder of the mlpi4Java Toolbox to your project root libs folder in the Package Explorer of your Eclipse workspace. It is recommended to add the library "as a link".

eclipseAndAddLib1.png

Next, add a folder armeabi in the libs folder and drag and drop the file libmlpijavaandroid.so from the folder mlpi4Java/bin/... to it.

Note
This file cannot be linked! It has to be copied to the folder!
eclipseAndAddLib2.png

In a next step you should add the necessary permissions for your application to communicate via network to the manifest of your project. Therefore, open the file AndroidManifest.xml and add the following lines in front of the <application> node.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Add a button and a text box to your layout. We want to connect to a device, when pressing the button and print the device name to the text box.

eclipseAndLayout.png

Insert the following MLPI code to the corresponding activity class (e.g. MainActivity.java).

package com.boschrexroth.mlpisampleandroid;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText textName = (EditText)findViewById(R.id.editText1);
final Button buttonConnect = (Button)findViewById(R.id.button1);
// set the on click handler
buttonConnect.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// we run the connection and reading of the values in a background task
// to not block the gui thread.
new AsyncTask<Void, Void, Void>() {
String status;
protected Void doInBackground(Void... params) {
MlpiConnection connection = new MlpiConnection();
// connect to control
try {
connection.connect("192.168.1.72 -user=mlpiTest -password=mlpiTest");
status = connection.system().getDisplayedDiagnosis().text;
}
catch (MlpiException e)
{
status = e.getMessage();
}
return null;
}
protected void onPostExecute(Void result) {
textName.setText(status);
};
}.execute();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

Click "Run" to start your application. Happy coding...