Tutorial: Android OSC Communication

Introduction

Most of the time, there are already existing apps that send readily-available sensor data from your phone to your computer via OSC. But what if you have another device that connects to your phone that you want to use as an extra sensor? You might not be able to find an app that sends the information that you want. For example, you might want your phone to send information from multiple bluetooth devices to your computer (Github repository at bottom). The following steps will show you how to write an Android app that lets you send whatever you want using OSC.

Note: The following tutorial assumes that you have already downloaded Android Studio, and have a basic project set up and running. For more information on how to use Android Studio, follow the tutorials in the training section of the Android developer’s website.

OSC (Open sound control) is a protocol for communicating between different devices. The specification can be found here: OSC specification.

In order to use OSC with Android, we need an implementation in Java. The one we will be using in this tutorial can be found here: JavaOSC.

Including the Library

First, create a new project with the default settings. In your project view, open the build.gradle file in the app folder. You want to add the following lines of code to it:

repositories{
mavenCentral()
}

These few lines tell Android Studio that we will be using a library from the Maven Repository. To actually include the library, add the following line to the dependencies block:

compile 'com.illposed.osc:javaosc-core:0.3'

When you get a message telling you to sync your project, do it and Android Studio will automatically download the libraries that you need for you.

The final build.gradle file can be found here.

Using the Library

In the MainActivity.class file, insert these lines with the rest of your imports:

import java.net.*;
import java.util.*;

import com.illposed.osc.*;

Note: At this point, if you get an error about not finding “illposed,” you haven’t included the libraries properly yet.

Since networking and communication tasks can’t be done on the main thread in Android apps, we need to create a new thread, and put all relevant code there. Then just start the thread in the onCreate() method. The example code can be found here.

Tutorial Github Repository

Github Repository for Kinecontrol Modular Music