tags:

views:

1436

answers:

4

How can I get battery status through J2ME ?

+2  A: 

I am not sure there is a generic way through J2ME alone.

With Nokia and its properties, you can (Get Battery Level in J2ME):

System.getProperty("com.nokia.mid.batterylevel");


The generic property, as illustrated here, does not always work (can return null)

Some of the system properties may return null as they may not be supported on early devices and some system properties require the MIDlet to be trusted otherwise return null...

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.location.*;

public class HelloMidp extends MIDlet implements CommandListener {
    private Command exitCommand;

    Display display;
    Form mainForm;

    public HelloMidp () {
        exitCommand = new Command("Exit", Command.EXIT, 1);
        mainForm = new Form ("HelloMidp");
        String batt = System.getProperty("batterylevel");
        mainForm.append (batt);
    }

    protected void startApp()
    {
        mainForm.addCommand(exitCommand);
        mainForm.setCommandListener(this);
        Display.getDisplay(this).setCurrent(mainForm);
    }

    protected void pauseApp() {}
    protected void destroyApp(boolean bool) {}
    public void commandAction(Command cmd, Displayable disp)
    {
        if (cmd == exitCommand) {
            destroyApp(false);
            notifyDestroyed();
        }
    }
}
VonC
Thanks. i'd also like to know if it's possible to detect Power interruption events with Nokia sets.
Attilah
AFAIK, not all nokia phones provide this. If you take the case of other phones, some may even require you to sign the builds. Lots of differences in the implementations have caused what is called "Device Fragmentation". The pity is that mobile phone vendors are still discussing on this issue, since days of MIDP 1.0
Ram
+3  A: 

Well, the only standard way of doing this is through JSR-256. The mobile Sensor API.
You can read the specifications from http://www.jcp.org/en/jsr/detail?id=256

Unfortunately it is very recent and not actually implemented in most retail phones yet.

When it ships, the Sony Ericsson Satio (or Idou) will have it.

Edit: Nokia N97 has JSR-256 and it can be installed on Nokia N85 and Nokia 5800.

Edit: Here's Attilah's other JSR-256 question with some code in the answer.

QuickRecipesOnSymbianOS
Thanks a lot man.
Attilah
A: 

I'm sure it depends on what phone and OS you're dealing with. Not having done any J2ME development, I would expect there to be some kind of "environment" or "system" service that would provide that sort of information.

TMN
A: 

This article describes how to use JSR 256 on Sony Ericson phones to read the battery level, network coverage and a few other interesting sensor data. It is support from JP8.3 and onward.

The code that does the trick:

SensorInfo[] batteryInfo = SensorManager.findSensors("battery_charge", null);
SensorConnection sensor = (SensorConnection)Connector.open(batteryInfo[0].getUrl());
Data data[] = sensor.getData(1);
String batteryLevel = "Current charge level: "+data[0].getIntValues()[0];