Skip to main content

Playing with Mobile Sensor API, Marge and JavaFX Script

Posted by brunogh on June 10, 2008 at 9:05 PM PDT

I am disappointed... I could not go on with my tests envolving phoneME Advanced on iPhone due to iPhone SDK just runs on Mac OS X. Boring week, I bought an iPhone for that and someone thought everybody should have a Mac OS X to use it... what a nice thing! Anyway, let's change plans for now... I found something funnier: let's play with JSR 256 (Mobile Sensor API or MSAPI). But let's use Marge (Bluetooth) and JavaFX Script too!

MSAPI in NetBeans

The current Wireless Toolkit, 2.5.2, does not support MSAPI yet, so you need to make some tricks.

First: Download the JSR 256 RI. If you do not have a Forum Nokia account, go ahead and make one. Unfortunately, it seems the zip is packed for Windows due to the .bat and .exe files inside, but it looks that it is just script files, so probably works on other OS if you adapt.

Second: I suppose you already have NetBeans 6.1 with Mobility Pack, so let's add a new platform into it. Go to Tools->Java Platforms->Add Platform...->Select Java ME MIDP Platform Emulator->Click Next->Select the folder you have extracted the downloaded zip and advance until the end. If everything went well, you probably got a new platform called Prototype 2.0 S60 MIDP Emulator. Great! You are now able to create and compile applications that uses MSAPI specification.

We are not going to emulate the sensors, but if you want, go ahead and start $RI_PATH/tools/simulator/bin/start_sensorSimu.bat.

Starting with MSAPI

From the allows Java ME application developers to fetch data easily and uniformly from sensors. A sensor is any measurement data source. Sensors vary from physical sensors such as magnetometers and accelerometers to virtual sensors, which combine and manipulate the data they have received from other kinds of physical sensors. Examples of virtual sensors could include, for example, battery level sensor indicating the remaining charge in a battery, or a field intensity sensor that measures the reception level of the mobile network signal in a mobile phone. The sensor may be connected to the mobile device in different ways, represented by the connection type, examples of which are embedded, short-range wireless, wired, and remote connection type.

The classes and interfaces of this API are defined in the package javax.microedition.sensor.

To start playing with sensors, basically you need to find them in order to get a the SensorInfo references. There are two static methods in SensorManager for that: findSensors passing the sensor url and the overload one passing the quantity and the context of use (if you set null for both parameters, all the supported sensors will be returned). From our demo application:

SensorInfo[] si = SensorManager.findSensors("acceleration",

After that, you can connect into the sensor using the well known Generic Connection Framework (GCF):

String url = si[0].getUrl();
SensorConnection sensor = (SensorConnection);

Then, you can start collecting the data. There are two ways of collecting the data in MSAPI: synchronously and asynchronously. In the first, you have to call SensorConnection getData iteratively. In the asynchronous way, you have to register a DataListener, invoking setDataListener and passing a reference of it and a buffer size (this number will be determine the data collecting frequency), that will be responsible for calling the dataReceived method. This method will return the SensorConnection reference, an array containing the data channels (for example, for a 3-axis accelerometer, the array will have length 3) and usually the last parameter will be false when the listener handles the data quickly:

sensor.setDataListener(new DataListener() {
  public void dataReceived(SensorConnection sensor, Data[] data, boolean isDataLost) {
}, 10);

Our demo: Bluetooth Sensor FX Tetris

The demo of this post is pretty simple. I got the Bluetooth JavaFX Tetris Game and changed the way of interaction. You do not have to type the direction in your mobile anymore, you can now use its accelerometer. Avoid typing, you can get a RSI (Repetitive Strain Injury)! Basically, I have implemented the dataReceived method to process the data collected and send over Bluetooth to the game. It is still missing some better balance in the accelerometer as you can see, but works as a proof of concept.

The code of JavaFX Tetris Game (BluetoothJFXTetris) is under Marge's Demos repository as well as the MIDlet controller (genericbtsensomapper-mobile). Go ahead and checkout them! They are open source! I have tested this demo with my designer friend' Sony Ericsson K850i and worked... as a demo, off course! I have noticed that sometimes the game crashes, I am not sure yet why, but I suspect that is something related to threads and rendering.

Here is a terrible preview video. It was recorded using the phone mentioned in the beginning of the post, probably I will have to record it again, but I could not wait until tomorrow to post about this experiment:

Thanks to Raghavan Srinivas that pushed me up to work on this demo during our fast talk at JavaOne! Cheers!

Have a nice week! :)
Bruno Ghisi

Related Topics >>


The best way probably would directly in the spec page: If you do not find, try to contact the spec leader. Cheers!

Seems that the file you linked to for JSR256 from Nokia is now an empty zip file. Is there somewhere else I can get this from? I want to use accelerometers but netbeans is just complaining all the time and there is nothing I can do about it.

Bruno, it´s not hard to install MacOS on a PC. Do a little research and have fun. ;-) What about using the iPhone as a wheel? Map is as an analog pad and use it in racing games. :-)

Yep, looks not hard, but probably it will took some time to install and get it working in a triple boot... I liked the racing games idea, I have thought about it before, but I will not use iPhone, gonna use a SonyEricsson or Nokia mobile and Java! Cheers!

Quoting my friend Carlos: "This is how you do a wii-like using Java and no money".

This is realy cool, Thank you Bruno

This is realy cool, Thank you Bruno

Unfortunately the file for RI seems lost, and Nokia has ...

Unfortunately the file for RI seems lost, and Nokia has other interests, more along the lines of Sensor API for Symbian and now Windows Phone 7 and above.

Does anybody still have this file?