I have again become interested in what you can do with Android applications. After some discussion with a housemate (who actually owns an Android, unlike me) about augmented reality applications and how they could be easily done (to some degree) if you could reasonably accurately track a phones location in a building (something that is hard to do with GPS). I wanted to see if the phones accelerometer could be used (at least for short distances) to work out how a phone had moved since entering a building. Rather than attempt to make an augmented reality app, I decided to go for a slightly easier project of making a tape measure, i.e. an app that could measure distance by moving the phone. Before anyone thinks this will actually be useful I should point out that this was a rather complete failure and the resulting app is fairly useless. However, the code may be useful to anyone looking at writing an Android app that uses any of the internal sensors, it would also be interesting to see if any other phones (I only tested the app on a G1) are any more successful.
So straight to the code, after (briefly) reading the Android documentation it seemed there was very little info about using the sensors, so I turned to Google, this article looked like what I wanted and linked to this code which is the basis of my app. So I updated the code to use the non-deprecated API, changing its functionality slightly and ended up with:
package uk.co.connorhd.android.tapemeasure; import android.app.Activity; import android.os.Bundle; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class TapeMeasure extends Activity implements SensorEventListener, OnClickListener { private SensorManager sensorMgr; private Sensor sensorAccel; private TextView accuracyLabel; private TextView xLabel, yLabel, zLabel; private Button calibrateButton; private float moved = 0; private float speed = 0; private float accel = 0; private float accelDiff = 0; private float[] a; private long lastUpdate = 0; private float curAccel = 0; private int accelUpdates = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); accuracyLabel = (TextView) findViewById(R.id.accuracy_label); xLabel = (TextView) findViewById(R.id.x_label); yLabel = (TextView) findViewById(R.id.y_label); zLabel = (TextView) findViewById(R.id.z_label); calibrateButton = (Button) findViewById(R.id.calibrate_button); calibrateButton.setOnClickListener(this); } @Override protected void onPause() { super.onPause(); sensorMgr.unregisterListener(this, sensorAccel); sensorMgr = null; } @Override protected void onResume() { super.onResume(); sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE); sensorAccel = sensorMgr.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0); boolean accelSupported = sensorMgr.registerListener(this, sensorAccel, SensorManager.SENSOR_DELAY_FASTEST); if (!accelSupported) { // on accelerometer on this device sensorMgr.unregisterListener(this, sensorAccel); accuracyLabel.setText(R.string.no_accelerometer); } } public void onClick(View v) { if (v == calibrateButton) { // Clicked button moved = 0; speed = 0; accelDiff = accel+accelDiff; accel = 0; } } public void onAccuracyChanged(Sensor sensor, int accuracy) { // this method is called very rarely, so we don't have to // limit our updates as we do in onSensorChanged(...) if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) { switch (accuracy) { case SensorManager.SENSOR_STATUS_UNRELIABLE: accuracyLabel.setText(R.string.accuracy_unreliable); break; case SensorManager.SENSOR_STATUS_ACCURACY_LOW: accuracyLabel.setText(R.string.accuracy_low); break; case SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM: accuracyLabel.setText(R.string.accuracy_medium); break; case SensorManager.SENSOR_STATUS_ACCURACY_HIGH: accuracyLabel.setText(R.string.accuracy_high); break; } } } public void onSensorChanged(SensorEvent event) { int sensorType = event.sensor.getType(); if (sensorType == Sensor.TYPE_ACCELEROMETER) { a = event.values; accel = (float) (Math.sqrt((a[0]*a[0]+a[1]*a[1]+a[2]*a[2]))-SensorManager.GRAVITY_EARTH)-accelDiff; curAccel += accel; accelUpdates++; long curTime = event.timestamp - lastUpdate; if (curTime > 1000000000) { curAccel = curAccel/accelUpdates; speed += curAccel*curTime*10e-10; moved += speed*curTime*10e-10; lastUpdate = event.timestamp; xLabel.setText(String.format("Moved: %+2.20f", moved)); yLabel.setText(String.format("Speed: %+2.20f", speed)); zLabel.setText(String.format("Accel: %+2.20f Time: %+2.3f", curAccel, curTime*10e-10)); curAccel = 0; accelUpdates = 0; } } } }
This calculates the current acceleration of the phone, subtracts gravity (which is added to the raw data), then derives the speed and distance travelled. The calibrate button resets the speed and distance, and assumes the phone is not moving, generating a bias to add to the acceleration in the case of the phones accelerometer being badly calibrated. If you want to try the app it can be downloaded and installed from here. I suggest launching the app, placing the phone on a stable surface and pressing calibrate, if you get any interesting results let me know.
Despite the phone reporting the accelerometer accuracy as “high” (with no description of what high means) and returning values in excess of 10 significant figures, the acceleration data with the phone sat on a table varied in the range of about 0.5m/s^2 (although this did appear to change depending on the room I was in). This resulted in a massively wrong distance value very quickly increasing to several metres without moving the phone, making any kind of testing almost impossible. It seems the current purpose of accelerometers is to work out if some violent action (such as shaking) has happened to the phone, and not anything much more interesting. All in all a bit of a waste of time, but maybe future phones will have better sensors, and hopefully someone will find the code snippet above useful.
At the risk of being boring, another node related link. A collection of articles written about node (mainly as tutorials) by the node community.
A site I came across some time ago, Try Ruby gives you an interactive (AJAX) ruby shell, with a simple tutorial to follow. In my opinion an awesome way to get started with a new language, I find learning is much easier by trying things out.
If you like this then you may also be interested in Try MongoDB (a similar MongoDB tutorial) and also codepad (a pastebin that allows you to execute certain programming languages and view the result).
Not that interesting, but an awesome site for finding fonts. Currently has over 10,000 free fonts, including the one used for the slogan on this site!
After thinking about a JavaScript API to allow alerts on an Android phone in this post, I came up with a method of adding this using the current Java Android app API. The solution involves starting a Webkit instance in a custom app, and intercepting links with a certain prefix. In this example android://alert/description creates an alert with title “alert” and description “description”.
To demonstrate here are some screenshots of a sample page using a sample application (if you have an Android phone you can use that link to download it and try it yourself):
The application when you load it:
When clicking go an alert is created:
Viewing the created alert:
The example page is very simple javascript:
function go() { window.location = 'android://'+document.getElementById('title').value +'/'+document.getElementById('text').value; }
The source for the app follows and is also fairly simple, most of the code is the rather complex way alerts are generated on Android:
package uk.co.connorhd.android.webapp; import java.net.URLDecoder; import uk.co.connorhd.android.webapp.R; import uk.co.connorhd.android.webapp.WebApp; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; public class WebApp extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); WebView webview = new WebView(this); // We're testing, clear the cache. webview.clearCache(true); setContentView(webview); webview.getSettings().setJavaScriptEnabled(true); final Activity activity = this; webview.setWebViewClient(new WebViewClient() { int alert = 1; public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show(); } public boolean shouldOverrideUrlLoading(WebView view, String url) { // Is it a hack? if (url.startsWith("android")) { String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); int icon = R.drawable.icon; CharSequence tickerText = "WebApp Alert!"; long when = System.currentTimeMillis(); Notification notification = new Notification(icon, tickerText, when); notification.flags = Notification.FLAG_AUTO_CANCEL; Context context = getApplicationContext(); String[] split = url.split("/"); CharSequence contentTitle = URLDecoder.decode(split[2]); CharSequence contentText = URLDecoder.decode(split[3]); Intent notificationIntent = new Intent(activity, WebApp.class); PendingIntent contentIntent = PendingIntent.getActivity( activity, 0, notificationIntent, 0); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); mNotificationManager.notify(alert++, notification); } else { view.loadUrl(url); } return true; } }); webview.loadUrl("http://connorhd.co.uk/files/WebApp.htm"); } }
This could easily be extended (and I may try to do this myself for Ircster) to provide all the standard browser functionality (for example the back button doesn’t currently work) and a more complete API to interface with Android.
First of all, most applications for smartphones (in this post smartphone mainly means iPhone or Android) are actually just interfaces (often simplified) to websites, i.e. Twitter, Google Maps, etc. However, both the Android and iPhone appear to fail quite badly at allowing websites to directly integrate with the phone, and I have some thoughts on how they could work better. When thinking about these ideas I am mainly thinking of what features I would like to see when developing phone specific features for Ircster (a fairly major project I am working on that I will eventually get round to writing blog post(s) about), but I think the same features would be useful to a large number of developers and lead to a better platform for users and developers alike.
Looking at the iPhone first, it does allow websites to be directly added as “applications” (applications meaning buttons on the iPhones menu). It also allows some limited customisation of the way the website is displayed when it is accessed as an application using html meta tags (i.e. <meta content="yes" name="apple-mobile-web-app-capable" />). This is a good start, however, the iPhone seems to be completely let down here by not allowing background applications, meaning there is no way to run a website constantly and alert the user on specific events, or receive sensor information (i.e. GPS) while the phone is being used for another purpose.
Android on the other hand supports background applications, however it does not allow (as far as I can tell) for websites to be added as applications, and does not have a way for websites to interact with the phones features. What I would like to see is a JavaScript API allowing phone features such as alerts to be triggered by the website (possibly similar to the message passing feature in Chrome extensions), and also for the phone to reply to the app with sensor information such as GPS location. This seems to fit in very well with the ideas behind Chrome OS and applications being simply websites. (While considering this it occurred to me some of this functionality could be hacked into Android using its current Java app platform, something I have started working on and will demonstrate in another blog post.)
Desktop applications definitely seem to be being replaced by websites, I don’t see why the opposite should be true on phones (websites should not need to write specific phone apps). I’d like to see APIs included with smartphone browsers (Google Gears is a potential start, although this appears to be deprecated on Android) that provide more direct interaction with the phones features (alerts, access to sensors, local storage and local serving of content) and allow complete applications to be developed as websites for multiple platforms.
A useful domain name finding tool I came across recently, Domainr lets you search less well known domain name extensions and tries to find the shortest possible domain for any word you enter. Also helpfully shows the availability of the domains it finds.
Another nosql project, mongodb provides more than just key-value pair storage. It is a document orientated database (along the lines of relational databases, but without a fixed structure for tables, or relational queries). That is, you can store objects (documents) that contain any combination of fields and data (data being strings, numbers, arrays or objects themselves), and then perform queries on them.
I have been interested in using mongo as a logging system for Ircster for a while now (it supports large sets of data with full text search and very fast queries), however until recently there was no reasonable driver for node. There are now two drivers available node-mongodb (bindings to the C driver) and node-mongodb-native (a JS implementation of the driver). The native driver especially interests me as I can more easily understand and work with the code.
I’ve been aware of a number of so called nosql projects (databases that aren’t relational) for some time and I finally have an opportunity to use one for a good reason.
Redis is an extremely fast key-value pair database (a hash table basically) that also includes fast, atomic set and list operations. I’m planning to use it as part of my group software project at uni, the project requires us to handle large amounts of data with a large number of requests per second, which makes 110,000 writes per second (one of the features of Redis) very appealing.
I’d also like to use my blog to write about a few of my personal projects. Many of these aren’t very useful and very few actually reach any usable state, however I feel node_debug is actually something quite useful.
Before reading this post you should probably know what node.js is.
node_debug allows you to evaluate statements and view their result (much like a REPL) from your favourite browser (assuming your favourite browser isn’t IE). It also allows you to browse objects if the result of your statement is an object, for example the global object in node is process so executing process; will return a browsable tree structure of the global scope.
Additionally it exports a log function that allows you to asynchronously output to any active console, for example setTimeout(function () { debug.log("Hello") }, 5000); will output Hello to any open consoles after 5 seconds, allowing useful debug output when using asynchronous functions.
To get started using node_debug you need to have a server running node, and to grab the latest node_debug from github. There is an example.js included to show how you would include it in your own scripts, the idea being you can simply include this in your own project in order to easily debug issues.
Future features I would like to add include:
- A debug.scope object that allows you to import other objects into the debuggers scope (currently only the global scope is available which can cause confusion due to closures in JavaScript hiding a lot of variables from the debugger).
- debug.error/warn/info functions in line with the browser console object provided by firebug.
- Detect disconnection from node and attempt reconnection.
- Caching of asynchronous messages that can be shown when connecting to the console, in order to log things without having an active browser window all the time.





