Sunday, January 2, 2011

Android Wii Balance Board Scale (WiiScale)

I recently published a new application to the Android market. This application is called WiiScale. The application utilizes the Wii Balance Board to sync with a supported Android device, and display the weight to the user, in short, it allows the Wii Balance Board to be used as a scale with your Android device. The purpose of this post is to show how another external Android application can integrate with the WiiScale application using Intents. I have provided a sample Android application that contains all of the functionality outlined in this that can be used as a reference or starting point if necessary. The source can be found on github.

It is actually very straightforward to create and call an Intent to request the users weight from WiiScale. The following code will do a few things. It will first call a method to check if the required intent for WiiScale is available on the users phone. If the intent is available it calls the intent, if it is not, it directs the user to the Android market to download the application.

The uri is specific uri defined by the WiiScale application for receiving intents. The unit parameter can have two values. The valid values are 'lbs' or 'kg' depending on whether your application would like the weight in pounds or kilograms. The WEIGHT_REQUEST variable is just a static Integer datatype to uniquely identify the intent when we get the result back. Note the call to 'startActivityForResult', this will allow your application to get a result back in the onActivityResult method. This method looks like the following.

This simply checks to make sure the activity result if for our WEIGHT_REQUEST from WiiScale intent call, and that it returned successfully. WiiScale returns two values from the intent. The unit of measure, either 'kg' or 'lbs' and the weight in the given unit measurement. Your application can then use this data as you please. It couldn't be simpler to integrate WiiScale into your own application. Please see the source on github for for details as well as how to check if the intent is available on your phone.