JSR 256 battery events

后端 未结 2 1053
孤独总比滥情好
孤独总比滥情好 2020-12-11 12:31

How can I detect whenever the power cord is unplugged from electrical socket using JSR 256?

相关标签:
2条回答
  • 2020-12-11 13:18

    From a quick look at the specifications of the JSR:

    (you might want to look for code examples, starting with Appendix D of the spec itself, the latest JavaME SDK, Sony Ericsson developer website, then google)

    As always, I would be worried about fragmentation in the diverse implementations of the JSR, but here's my first idea:

    import javax.microedition.sensor.*;
    
    SensorInfo[] powerSensorInfoArray = SensorManager.findSensors("power","ambient");
    
    //let's assume there is one SensorInfo in the array.
    
    //open a connection to the sensor.
    
    SensorConnection connection = (SensorConnection)Connector.open(powerSensorInfoArray[0].getUrl(), Connector.READ);
    
    // add a DataListener to the connection
    
    connection.setDataListener(new MyDataListener(), 1);
    
    // implement the data listener
    
    public class MyDataListener implements DataListener {
    
    public void dataReceived(SensorConnection aSensor, Data[] aDataArray, boolean isDataLost) {
    
    //let's assume there is only one channel for the sensor and no data was lost.
    
    // figure out what kind of data the channel provides.
    
    int dataType = aDataArray[0].getChannelInfo().getDataType();
    
    //now, I suggest you switch on dataType and print the value on the screen
    
    // experimentation on the JSR256 implementation you're targetting seems to be
    
    // the only way to figure out out power data is formatted and what values mean.
    
    //only one of the following 3 lines will work:
    
    double[] valueArray = aDataArray[0].getDoubleValues();
    int[] valueArray = aDataArray[0].getIntValues();
    Object[] valueArray = aDataArray[0].getObjectValues();
    
    // let's assume one value in the valueArray
    
    String valueToPrint = "" + valueArray[0];
    
    // see what happens with that and you plug or unplug the power supply cable.
    
    }
    
    }
    

    You'll need to add javax.microedition.io.Connector.sensor to your MIDlet permissions.

    -------EDIT------

    Documentation from the JSR-256 implementation on Sony-Ericsson Satio phone (S60 5th edition):

    The battery charge sensor has the following characteristics:

    • Quantity: battery_charge

    • Context type: device

    • URL: sensor:battery_charge;contextType=device;model=SonyEricsson

    • Channels: (index: name, range, unit)

    • 0: battery_charge, 0-100, percent

    • 1: charger_state, 0-1, boolean

    0 讨论(0)
  • 2020-12-11 13:29

    You would add javax.microedition.io.Connector.sensor to the API Permissions tab of the Application Descriptor of the project properties.

    0 讨论(0)
提交回复
热议问题