Controlling Light settings in Java ME
This snippet shows how to control the lighting on devices through the setLights() method of the DeviceControl class.
Article Metadata
Code Example
Source file: Media:Controlling Light Settings.zip
Tested with
Devices(s): Nokia 6131, Nokia N81, Nokia 701, Nokia Asha 305
Compatibility
Platform(s): Series 40, Symbian
Article
Keywords: com.nokia.mid.ui.DeviceControl, DeviceControl.setLights()
Created: IlGolub
(28 Nov 2008)
Reviewed: mtilli
(15 Aug 2012)
Last edited: hamishwillee
(10 Oct 2012)
Source file: ControllingLightMIDlet.java
import com.nokia.mid.ui.DeviceControl;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
/**
*
*/
public class ControllingLightMIDlet extends MIDlet implements CommandListener {
/**
* Command for setting light
*/
private Command controllingLightCommand;
/**
* exit midlet command
*/
private Command exitCommand;
private Form mainForm;
/**
* The ControllingLightMIDlet constructor.
*/
public ControllingLightMIDlet() {
//Empty implementation
}
/**
* Performs an action assigned to the Mobile Device - MIDlet Started point.
*/
public void startMIDlet() {
switchDisplayable(getForm());
}
/**
* Switches a current displayable in a display. The Display instance is
* taken from getDisplay() method. This method is used by all actions
* in the design for switching displayable.
* @param nextDisplayable the Displayable to be set
*/
public void switchDisplayable(Displayable nextDisplayable) {
Display display = getDisplay();
display.setCurrent(nextDisplayable);
}
/**
* From CommandListener.
* Called by a system to indicated that a command has been invoked on a
* particular displayable.
* @param command the Command that was invoked
* @param displayable the Displayable where the command was invoked
*/
public void commandAction(Command command, Displayable displayable) {
if (displayable == mainForm) {
if (command == exitCommand) {
exitMIDlet();
} else if(command == controllingLightCommand) {
//up light level from 0 to 100
for(int i = 0; i < 100; i++) {
// void setLights(int num, int level) is a static function of
//com.nokia.mid.ui.DeviceControl class that
//activates and deactivates the lights on the device.
//Parameter num indicates the number of the device light
//to control. Currently only one num parameter value is
//specified: num value 0 is used for controlling the
//screen backlight. Parameter level is a value between
//0-100, which indicates the brightness of the light.
//In many implementations, there are only two levels:
//on or off(Sony Ericsson). Value 0 indicates lights off
//for monochrome displays or the minimum brightness setting
//for color displays. All other level values (1-100) are
//used for turning the lights on, possibly with different
//brightness levels, depending on the value. A larger value
//always results in either the same brightness setting as a
//lower one, or a brighter setting. For many products,
//values 1-100 will just turn the lights on.
DeviceControl.setLights(0, i);
try {
Thread.sleep(30);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
}
/**
* Returns an initiliazed instance of mainForm component.
* @return the initialized component instance
*/
public Form getForm() {
if (mainForm == null) {
mainForm = new Form("Lighting test!");
exitCommand = new Command("Exit", Command.EXIT, 0);
mainForm.addCommand(exitCommand);
controllingLightCommand = new Command("Light", Command.SCREEN, 0);
mainForm.addCommand(controllingLightCommand);
mainForm.setCommandListener(this);
}
return mainForm;
}
/**
* Returns a display instance.
* @return the display instance.
*/
public Display getDisplay () {
return Display.getDisplay(this);
}
/**
* Exits MIDlet.
*/
public void exitMIDlet() {
switchDisplayable(null);
destroyApp(true);
notifyDestroyed();
}
/**
* From MIDlet.
* Called when MIDlet is started.
* Checks whether the MIDlet have been already started and
* initialize/starts or resumes the MIDlet.
*/
public void startApp() {
switchDisplayable(getForm());
}
/**
* From MIDlet.
* Called when MIDlet is paused.
*/
public void pauseApp() {
//Empty implementation
}
/**
* From MIDlet.
* Called to signal the MIDlet to terminate.
* @param unconditional if true, then the MIDlet has to be unconditionally
* terminated and all resources has to be released.
*/
public void destroyApp(boolean unconditional) {
//Empty implementation
}
}
Postconditions
This MIDlet implements a simple example of controlling the light property.
Turns off the light then raises the light level from 1 to 100.
Supplementary material
You can download this application and source code here: Controlling Light Settings.zip
Related articles:


Contents
Erajorshi - a request
good job here. but can you test this with nokia c203 please? i tried it, but i guess i am going wrong somewhere. please let me know.erajorshi 01:09, 10 October 2012 (EEST)
Hamishwillee - The author may not be present
Hi Erajorshi
I see from the article categories that it is valid for "Series 40 Developer Platform 1.1", while the C2-03 is based on the previous version (v1.0). While the absence of this earlier category tag doesn't mean that the article doesn't work for the platform, it does mean that no one has shown it does.
This article was written in 2008 so the author may no longer be part of the community, and in any case may not have the device you're referring too. The best way to verify the article on a particular device if you don't have it is to try the zip code sample out using RDA. If the phone isn't on RDA, then ask on the discussion boards, with a link to this article. Someone in the community may help you!
If you confirm that the phone does not work, it would be great if you could post here.
regards
Hamishhamishwillee 05:40, 10 October 2012 (EEST)
Erajorshi - thank you
well, i tried the above code on a nokia C2-03, and it DOES NOT work. Thank you Hamishwille, for the support.erajorshi 19:46, 16 October 2012 (EEST)
Hamishwillee - Thanks!
That's useful to know.hamishwillee 08:29, 17 October 2012 (EEST)