Getting started¶
Before diving into ROS enabled Android application development, you should be familiar with rosjava and Android application development in general. Note that any information regarding command-line adt and eclipse development is depracating - we have moved early to a gradle-android studio environment.
Using RosActivity¶
The RosActivity class is the base class for all of your ROS enabled Android applications. Let’s consider the following example from the android_tutorial_pubsub package. In this example, we create a Publisher and a Subscriber that will exchange “Hello, World” messages.
On line 14, we extend RosActivity. When our activity starts, the RosActivity super class will:
- start the NodeMainExecutorService as a service in the foreground,
- launch the MasterChooser activity to prompt the user to configure a master URI,
- and display an ongoing notification informing the user that ROS nodes are running in the background.
On line 22 we call the super constructor with two strings that become the title and ticker message of an Android notification. The user may tap on the notification to shut down all ROS nodes associated with the application.
Lines 28-30 should look familiar to Android developers. We load the activity layout and get a reference to our RosTextView (more on that later).
On line 42 we define the abstract method RosActivity.init. This is where we kick off our NodeMains and other business logic.
And that’s it. RosActivity handles the rest of the application’s lifecycle management including:
- acquiring and releasing WakeLocks and WifiLocks,
- binding and unbinding the NodeMainExecutorService,
- and shutting down NodeMains when the application exits.
Nodes and Views¶
The android_core stack provides a number of Android Views which implement NodeMain. For example, let’s look at the implementation of RosTextView. The intent of this view is to display the textual representation of published messages.
The view is configured with a topic name, message type, and a MessageCallable. On line 40, in the NodeMain.onStart method, we create a new Subscriber for the configured topic and message type.
When a new message arrives, we either use the configured callable to transform the incoming message to a string (line 49), or we use the default toString() method if no callable was configured (line 56). We then set the text of the view to the string representation of the incoming message.
As with any other NodeMain, the RosTextView must be executed by the NodeMainExecutor. In the Using RosActivity example, we execute it in RosActivity.init and use the it to display incoming messages from the Talker node.