smart solutions for small devices
[ start | index | login ]
start > J2ME > Using the N95 accelerometer in Java - kind of

Using the N95 accelerometer in Java - kind of

Created by bjoernQ. Last edited by bjoernQ, 7 years and 324 days ago. Viewed 1,467 times. #6
[diff] [history] [edit] [rdf]
Screenshot0001.jpg (108995)
Screenshot0002.jpg (33168) (18907)
I was very excited about the newly discovered accelerometer in the N95. Unfortunately there's no sensor API for us JME developers. But fortunately a Python module is available. (aXYZ module, see >> ).

So I decided to write a small socket server in Python which emits the raw sensor data. Then my midlet can connect to this Python server and use the provided data. Each dataset consists of the X,Y and Z values separated by "," and a "*" to mark the end of the data tuple.

The server has a number of limitations (single-threaded, stops after the client disconnects) because I'm not a Python programmer. The JME code is also very inefficient but I think it's much easier to understand than if it were when it's more optimized.

In order to try it out you have to start the server first and then you can use the MIDlet.


Here is the code of the server

import socket
import axyz

clientSocket = None

def printout(x,y,z): if clientSocket: clientSocket.sendall( "%i,%i,%i*"%(x,y,z) )

HOST = '' PORT = 12008 print "define the socket" s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print "bind the socket" s.bind((HOST, PORT)) s.listen(1) print "waiting of the client to connect" conn, addr = s.accept() print 'Connected by', addr clientSocket = conn axyz.connect(printout) while 1: data = conn.recv(1024) if not data: break conn.send(data) clientSocket = None axyz.disconnect() conn.close()

On the Java side I have created the class "XYZConnect". It extends the Thread-class and connects to the local server. You have to provide a callback which is called each time a dataset arrives.


	connector = new XYZConnect(new Callback(){
		public void callback(int x, int y, int z) {

}); connector.start(); }

The full source code can be found >>here. (Python + Java source.)

no comments | post comment | Bjoern Quentin