×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Super Contributor
    Join Date
    Mar 2003
    Posts
    580

    Unhappy Detecting battery level with Javaphone API

    I wrote a simple program which SHOULD just show the battery level.
    But it does not work, and I can't understand why. I always obtain the same value, 0x7fff.ffff (2147486347).Why? Which initialization do I lack?

    Is there an alternate method to read the battery level?
    The battery indicator on my UIQ phone is ugly, it just shows if it is full, empty, 1/3 or 2/3 charged!

    This is the source; I tried to use both com.symbian.javax.power.monitor and just javax.power.monitos, without any success.

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.io.*;
    
    import javax.power.monitor.*;
    import com.symbian.javax.power.monitor.*;
    
    public class jbattery extends Frame implements ActionListener, PowerMonitorListener {
    
        private Label lab1,lab2;
        private Button exitButton, goButton;
        private PowerMonitor pMon = null;
        private EpocHalPowerMonitor epoc = null;
    
        public jbattery(){
    
              super("Battery Check");
    
              int remaining, level;
    
              this.addWindowListener(
                 new WindowAdapter(){
                       public void windowClosing(WindowEvent e){
                          System.exit(0);
                       }
                    }
                 );
    
              initGui();
    
              pMon = PowerMonitor.getInstance();
              epoc = new EpocHalPowerMonitor();
              epoc.startEventServer();
              // Attacco il listener
              pMon.addPowerMonitorListener(this);
    
              setSize(200,280);
              updateInfo();
    
              setVisible(true);
        }
    
    
        private void initGui(){
    
                 setLayout(null);
                 lab1 = new Label("%");
                 lab2=new Label("secs");
                 exitButton = new Button("Exit");
                 goButton = new Button("Update");
    
                 //******** Set controls positions.
                 lab1.setBounds(10 , 30 , 150, 20);
                 lab2.setBounds(10 , 50 , 150, 20);
                 exitButton.setBounds(70,250,50,20);
                 exitButton.addActionListener(this);
                 goButton.setBounds(145,70,50,20);
                 goButton.addActionListener(this);
    
                 //******** Add control to frame.
                 add(lab1);
                 add(lab2);
                 add(exitButton);
                 add(goButton);
       }
    
       // Re-fattorizzazione e miglioramenti
       private void updateInfo(){
    
                   int remaining,level;
                   int hour, min, sec;
    
                   if (pMon!=null){
                    //remaining = pMon.getEstimatedSecondsRemaining();
                   // level = pMon.getBatteryLevel();
                    remaining = epoc.getEstimatedSecondsRemaining();
                    level = epoc.getBatteryLevel();
    
                    hour = remaining /3600;
                    min = (remaining % 3600) /60;
                    sec = remaining - hour*3600 - min*60;
    
                    lab1.setText( remaining+ " - "+hour+" h "+min+" min "+ sec +" sec left.");
                    // oppure se non funziona la riga sopra sostituiscila con questa sotto
                    // lab1.setText(new Integer(remaining).toString()+" estimated time left.");
                    lab2.setText(new Integer(level).toString()+"% charge level.");
                 }else{
                    lab1.setText("Estimated time left: n/a");
                    lab2.setText("Charge level: n/a");
                 }
    
       }
    
       // implementazione dell'interfaccia ActionListener
       public void actionPerformed(ActionEvent evt)
       {
           if (evt.getSource().equals(goButton)){
               updateInfo();
           }
    
           if (evt.getSource().equals(exitButton)) {
              System.exit(0);
           }
       }
    
       // implementazione dell'interfaccia PowerMonitoListener
       public void powerWarning(int estimatedSecondsRemaining, PowerWarningType warningType)
       {
           if (warningType == PowerWarningType.BatteryLow){
                 lab1.setText("Batteria bassa!");
           }
    
           if (warningType == PowerWarningType.ExternalPowerSourceChange){
                 lab1.setText("In fase di ricarica");
           }
       }
    
       public static void main(String[] args){
              new jbattery();
       }
    
    } // program end.

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    37

    Re: Detecting battery level with Javaphone API

    I did not know there is actually java code which does battery monitoring. Is it J2ME/CLDC or CDC? Thanks!

  3. #3
    Super Contributor
    Join Date
    Mar 2003
    Posts
    580

    Re: Detecting battery level with Javaphone API

    Quote Originally Posted by qmei
    I did not know there is actually java code which does battery monitoring. Is it J2ME/CLDC or CDC? Thanks!
    It's Personal Java, and it is not working (on my Motorola a1000)! :-(((

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    37

    Re: Detecting battery level with Javaphone API

    I am using A1000 personally. I guess Personal Java is not supported on that phone but only a guess. I suppose you have got manuals etc which indicates PJava is supported on this phone.

  5. #5
    Super Contributor
    Join Date
    Mar 2003
    Posts
    580

    Re: Detecting battery level with Javaphone API

    Quote Originally Posted by qmei
    I am using A1000 personally. I guess Personal Java is not supported on that phone but only a guess. I suppose you have got manuals etc which indicates PJava is supported on this phone.
    A1000 DOES support PersonalJava (just give a try to my jUIQdiz application! ;-) ), but it looks like its PJava implementation does not fully support PJava standards (javaphone library , if i remember correctly), so this program always returns a default value.

    BTW, I really think PJ is implemented in a1000 BY MISTAKE, as Motorola itself looked like not to know about it when I asked them for info, and they refused giving me support about it: they only give (gave...) free supoport for j2me on a1000...

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    37

    Re: Detecting battery level with Javaphone API

    On the other hand, it is really a SHAME that J2ME does not support querying battery level and signal strength. Now even the Flash Lite, a scripting language, supports these functions.

  7. #7
    Super Contributor
    Join Date
    Mar 2003
    Posts
    580

    Re: Detecting battery level with Javaphone API

    Quote Originally Posted by qmei
    On the other hand, it is really a SHAME that J2ME does not support querying battery level and signal strength. Now even the Flash Lite, a scripting language, supports these functions.
    BTW, as this is another way I'm exploring, what do you know about developing in flash lite 1.1 without Adobe IDE? I know about MTASC opensource compiler, but I can't yet understand how to write and compile my first "hello world" program.
    If flashlite 1.1 does support battery reading, this could be my workaround for lacky personaljava...

  8. #8
    Super Contributor
    Join Date
    Apr 2003
    Location
    USA, CA
    Posts
    7,191

    Re: Detecting battery level with Javaphone API

    GetBatteyLevel is available on Flash Lite 2.x and Flash Lite 1.1

    http://livedocs.macromedia.com/flash...=00000328.html

    Hartti

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    37

    Re: Detecting battery level with Javaphone API

    Quote Originally Posted by cassioli
    BTW, as this is another way I'm exploring, what do you know about developing in flash lite 1.1 without Adobe IDE? I know about MTASC opensource compiler, but I can't yet understand how to write and compile my first "hello world" program.
    If flashlite 1.1 does support battery reading, this could be my workaround for lacky personaljava...
    You can check Adobe Flash Lite site. It has got everything you need - only a few steps. I am surprised how much the flash lite can do.

    I have not used open source Flash. Not sure if it is as good as the macromedia Flash IDE.

  10. #10
    Super Contributor
    Join Date
    Mar 2003
    Posts
    580

    Re: Detecting battery level with Javaphone API

    Quote Originally Posted by qmei
    You can check Adobe Flash Lite site. It has got everything you need - only a few steps. I am surprised how much the flash lite can do.

    I have not used open source Flash. Not sure if it is as good as the macromedia Flash IDE.
    by sure it is not as EXPENSIVE as Macromedia's....

  11. #11
    Super Contributor
    Join Date
    Mar 2003
    Posts
    580

    Re: Detecting battery level with Javaphone API

    Quote Originally Posted by qmei
    You can check Adobe Flash Lite site. It has got everything you need - only a few steps. I am surprised how much the flash lite can do.
    Are you able to write for me a micro-source in ActionScript 1.0 which just displays on the screen the result of this assignment?
    Code:
    battLevel = fscommand2("GetBatteryLevel");
    I don't know where to start from to write something on the screen!

  12. #12
    Registered User
    Join Date
    Mar 2007
    Posts
    2

    Re: Detecting battery level with Javaphone API

    Hi jumpjack Are you sure that this is correct power? I'm studing this personaljava about 3h and my Java knowlage is 0, but maybe this is a problem. I downloaded specyfication for javaphone 1.0 and there are two packages: power.monitor and power.managment
    In my opinion this "monitor" word gave You fake picture

    From javax.power.managment :

    The Power Management API provides the following features:
    -System power states be identified
    -Application can receive notifications of Power state changes

    Code:
    /*
     * Copyright 1999 Sun Microsystems Inc, All rights reserved.
     */
    import javax.power.management.*;
    /**
     * Wait for and print changes in the power state.
     * Exit when the system is suspended or put to sleep
     * or 24 hours have passed.
     */
    public class PowerSample2 implements PowerStateListener {
        // Get the system power manager.
        static PowerManager powermgr = PowerManager.getInstance();
        /**
         * Program to get and print the battery level and time remaining.
         */
        public static void main(String[] args) {
            PowerSample2 listener = new PowerSample2();
            // Register to be notified on power state changes
            powermgr.addPowerStateListener(listener);
            // Get the current time and battery level
            PowerState state = powermgr.getPowerState();
            System.out.println("Power state is " + state);
            try {                        // Wait up to a day
                Thread.sleep(24 * 60 * 60 * 1000);
            } catch (InterruptedException e) {
            }
            // Unregister for notifications
    JavaPhone Specification 1.02
    
    22 Mar 2000
            powermgr.removePowerStateListener(listener);
        }
        /**
         * Gets notified of Power Manager state changes.
         */
        public void powerStateChange(PowerStateEvent event) {
            System.out.println("Changing power state from " +
                               event.getPrevState() +
                               " to " + event.getNewState());
            if (event.getNewState() == PowerState.Suspend ||
                event.getNewState() == PowerState.Sleep) {
                System.exit(0);
            }
        }
    }
    So I think, there may be an anwser. But for now, i'm still looking on google for this package, cause in javaphone.jar that is on a1000 there is only monitor package:/

    Hope it helps

  13. #13
    Super Contributor
    Join Date
    Mar 2003
    Posts
    580

    Re: Detecting battery level with Javaphone API

    Sounds interesting, hope you find something useful.
    In the meantime, maybe you want to give a try to this source I found:

    Code:
    ClASSE ApriSeriale.java
    
    import java.io.*;
    import javax.comm.*;
    import java.util.*;
    
    
    public class ApriSeriale {
        
        
        //COSTRUTTORE
        public ApriSeriale() throws  IOException,NoSuchPortException,UnsupportedCommOpe
    rationException,PortInUseException {
          
          OutputStream os;
          InputStream is;
          SerialPort sp=null;
          String str;
        
          Enumeration en=CommPortIdentifier.getPortIdentifiers();
        
          while(en.hasMoreElements()){
          
            CommPortIdentifier cpi=(CommPortIdentifier)en.nextElement();
            
            if(cpi.getPortType()==CommPortIdentifier.PORT_SERIAL){
            
              System.out.println("Sto usando la seguente porta seriale: "+cpi.getName());
              str=cpi.getName();
              try{
              
                sp=(SerialPort)cpi.open("ApriSeriale",500);
              }
              catch(PortInUseException ex){
                System.out.println("Port Already In Use By "+ex);
              }
            
            //Acquisisco input e output dalla seriale
            is=sp.getInputStream();
            os=sp.getOutputStream();
            
            //Setto i parametri della seriale
            sp.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
            
            //Stampo i parametri appena settati
            System.out.print("\n\n");
            System.out.println("****************************************");
            System.out.println("PORTA SELEZIONATA: "+sp.getName());
            System.out.println("BAUD RATE: "+sp.getBaudRate());
            System.out.println("DATABITS: "+sp.getDataBits());
            System.out.println("STOPBITS: "+sp.getStopBits());
            System.out.println("PARITY: "+sp.getParity());
            System.out.println("****************************************");
            System.out.println("\n\n");
            
           
            System.out.println("Sto scrivendo ATD");
           
              os.write("AT\r\n".getBytes());
    
            System.out.println("Scritto");
            
           
            
            sp.notifyOnDataAvailable(true);
            
           
        
          try{
          
          System.out.println("Ora sto entrando in Logger");
          sp.addEventListener(new Logger(str,sp));	
          
          }
          catch(TooManyListenersException ex){
          System.out.println("Eccezione di tipo TooManyListenersException");
          }
            
            }
          }
        }
    
    }
    --------------------------------------------------------------------------------
    
    
    CLASSE Logger.java
    
    code:--------------------------------------------------------------------------------
    import java.io.*;
    import javax.comm.*;
    import java.util.*;
    
    public class Logger implements SerialPortEventListener{
    
    	    String portName;
    	    SerialPort thePort;
    	    BufferedReader br;
    	    InputStream in;
    	    
                public Logger(String name,SerialPort port)throws IOException{
    		    
    		portName= name;
    		thePort= port;
    		br=new BufferedReader(new InputStreamReader(thePort.getInputStream()));  
                    in=thePort.getInputStream();            
    	    }
    	    
    	    
                //Gestore Eventi Dati Disponibili
                public void serialEvent (SerialPortEvent spe){
    		if(spe.getEventType()==SerialPortEvent.DATA_AVAILABLE){
                      System.out.println("DATI DISPONIBILI");
                    System.out.println();
                    byte[] readBuffer = new byte[20];
                try {
                
                  while (in.available() > 0) {
                    int numBytes = in.read(readBuffer);
                  
                  if(numBytes>0)
                    System.out.println("NUMBYTES MAGGIORE DI ZERO: "+numBytes);
                  
                  /*char strng =(char)numBytes;
                  System.out.println("STRNG:  "+strng);*/
                  for(int i=0;i<20;i++)
                    System.out.print(readBuffer[i]+" ");
                  /*
                  System.out.println(readBuffer[0]);
                  System.out.println(readBuffer[1]);
                  System.out.println(readBuffer[2]);*/
                }            
                
                System.out.print("BUFFER:  "+new String(readBuffer));
              
                
                } catch (IOException e) {thePort.close();
                    System.exit(0);}
                    
                    
                    }
                }	    
    }
    --------------------------------------------------------------------------------
    
    
    CLASSE Test.java
    
    code:--------------------------------------------------------------------------------
    import java.io.*;
    import javax.comm.*;
    
    
    public class Test{
    
    
      public static void main(String args[])throws  IOException,NoSuchPortException,UnsupportedCommOpe
    rationException,PortInUseException{
      
        ApriSeriale as =new ApriSeriale();
      
        
      
      }
    
    }
    It allows sending AT commands to modem. I don't know if java programs on a1000 can communicate with internal modem, but if it's true, we could try the AT command to get battery level! (sorry, can't remember it right now).

    Take also a look to my blog for tips&tricks about javaphone ;-)
    http://jumpjack.wordpress.com/tag/sy...otorola-a1000/

    This post shows how to list available ports:
    http://jumpjack.wordpress.com/2007/0...javaphone-api/

    We should try all of them to find the internal modem (but also finding "by mistake" how to communicate with PC would be cool!)

  14. #14
    Nokia Developer Champion
    Join Date
    Apr 2005
    Location
    Boston
    Posts
    517

    Re: Detecting battery level with Javaphone API

    Ciao

    about Flash Lite, check here I have a list of components:
    http://www.biskero.org/?page_id=333

    The battery component check everything you need for battery and signal:
    http://www.flashmobile.it/index.php?page=lab

    Also you might want to check some tutorials for developing in Flash Lite.
    http://www.biskero.org/?page_id=342

    Alessandro

  15. #15
    Registered User
    Join Date
    Dec 2004
    Posts
    369

    Thumbs up Re: Detecting battery level with Javaphone API

    cassioli
    if you are using motorola i remember they have support for battery level query in j2me via getProperty

    the syntax is
    System.getPropery("batterylevel")
    it only returns 3 modes high,(middle somthing) and low.
    i dont know if this is implemented on a1000
    but on v3xx i've tested it and worked

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×