Playing with Mobile Sensor API, Marge and JavaFX Script
Posted by brunogh on June 10, 2008 at 9:05 PM PDT
Controlling a JavaFX Script tetris game using a mobile over Bluetooth... with a sensor accelerometer and Marge!
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 specification:...it 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):
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:
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!