Tuesday, December 2, 2008

Device Specific Library Part Three

Device Control

You can control of the extra physical of Nokia devices using the com.nokia.mid.ui.DeviceControl class. The table shows the methods available.
The two device elements you can control are the lights and vibration. To temporarily flash the lights on and off, use the flashLights method. For example :

import com.nokia.mid.ul.DeviceControl;
.......

This code will cause the lights (such as the LEDs) to flash. If there is no support for this feature, then nothing will happen (duh). The integer value specifies the length of time (in milliseconds) to keep the light on, althought the device might override this value if you specify a number that is too large.

The other method relating to lights, setLights, allows you to control any of the lights on the device individually, such as the backlight or the LEDs ... in theory. In reality, Nokia only gives you the abality to control the device's backlight (if it has one). To do this, call the method with the light number (the first integer) set to 0 for the bachklight and the level integer set to a number between 0 and 100 (where 0 is off and 100 is the brightest level).

For device that don't support graded backlighting, all values between 1 and 100 just translate to on. Here's an example of setLights in action :

DeviceControl.setLights(0,100);

Controlling the vibration of the phone is one of those cool things you really want to do Crashing into a side railing or taking, shield damage feels much cooler if you give the phone a jiggle when it happens. To start the phone vibrating, use ;

DeviceControl.startVibra(10,100);

The first parameter, the frequency, is an integer in the range of 0 to 100. This number represents how violent the shaking should be. The second integer is the duration of the vibration in milliseconds. You can vary these numbers depending on what's happening in the game. For example, you might make the phone vibrate violently for bigger shield hits or for a longer duration as the shield weakens.

A call to startVibra will return immediately, regardless of the duration of the call. If the device does not support vibration it will throw an IllegalStateExpection. This can happen even on a device that supports vibration if, for example, it's docked in a cradle.

To immediately stop any current vibration, use the stopVibra method.

No comments: