Skip to content

Using AbstractIOIOActivity (deprecated)

ytai edited this page Feb 27, 2012 · 1 revision

Using the AbstractIOIOActivity Utility

This page is kept here as reference for legacy applications. This class is deprecated, and replaced by the ioio.lib.util.android.IOIOActivity class. For more details, see this page.

Very often IOIO-based applications will have the following traits:

  • A single thread is dedicated to creating the IOIO instance, establishing connection and then controlling the IOIO. This is possibly done while communicating with other threads, such as the UI thread.
  • This thread will be created in onResume() and aborted in onPause() of an Activity.
  • Aborting the thread involves disconnecting from the IOIO or otherwise cancelling an on-going attempt to connect.
  • In this thread, there will be some operations done once, right after a connection is established. Typically, these will include creating sub-instances of the IOIO object, by calling some of the openXYZ() methods.
  • Then, there will be on-going tasks done repetitively.

While this behavior surely doesn't cover every possible application, it does cover quite a few of them. So in order to save the boiler-plate code for achieving just that, a utility class has been created, called AbstractIOIOActivity, under the ioio.util package. This is simply an abstract class, extending Activity which does exactly what's written above.

It leaves you, the application author, to:

  • Create your Activity class by extending AbstractIOIOActivity.
  • Create a (possibly inner-) IOIOThread class extending AbstractIOIOActivity.IOIOThread.
  • In your thread class, implement the setup() method, which gets called once the IOIO connection is established. It should use the ioio_ field as its IOIO instance.
  • In addition, implement the loop() method, which gets called repetitively as long as IOIO is connected. It should use the ioio_ field as its IOIO instance.
  • Let any ConnectionLostException get thrown from either of these methods - it will be caught and properly handled automatically.
  • Finally, implement the createIOIOThread() method in your main class, which simply returns a new instance of your thread class.

The HelloIOIO example, provided along with IOIOLib, is a good example of using AbstractIOIOActivity.

Multi-IOIO Applications

AbstractIOIOActivity supports writing applications with multiple IOIO boards connected. Each can connect over one of the available connection channels (e.g. USB, Bluetooth). In such a case, AbstractIOIOActivity will call your createIOIOThread() once for every possible connection channel. Your implementation can then create a thread for each IOIO, or possibly return null if the given channel is irrelevant. In order to distinguish between the different channels (e.g. in order to refuse creation for some connection types), implement createIOIOThread(String, Object[]) instead. The arguments will provide information on the connection types.

For more information, please see the Javadocs of AbstractIOIOActivity.