Class Battery


  • public class Battery
    extends ApiBindBase
    This class provides battery related APIs.

    Java Sample Code:

      package com.mitac.api;
    
      import android.app.Activity;
      import android.os.Bundle;
    
      import com.mitac.api.libs.Battery;
      import com.mitac.api.libs.ServiceStatusCallback;
    
      public class MainActivity extends Activity {
          private Battery mBattery;
          private BroadcastReceiver mBatteryRcvr;
    
          // Extra for Intent.ACTION_BATTERY_CHANGED:
          // String value set of battery swell trip status.
          private static finally String EXTRA_SWELL_STATUS = "swell_status";
    
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
    
              // when creating the instance the service is auto binding
              // register a callback to listen service status
              mBattery = new Battery(getApplicationContext(), new ServiceStatusCallback() {
                  @Override
                  public void ready() {
                      startTest();
                  }
              });
              mBatteryRcvr = new BroadcastReceiver() {
    
                 @Override
                 public void onReceive(Context context, Intent intent) {
                      // You can get battery swell status here
                      String status = intent.getStringExtra(EXTRA_SWELL_STATUS);
                 }
             };
             registerReceiver(mBatteryRcvr, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
          }
    
          private void startTest() {
              if (mBattery != null) {
                  String val = mBattery.getBatterySwellStatus(BatteryStatusType.SWELL_STATUS);
                  if(val == "0"){
                      // The battery is good
                  }else if(val == "1"){
                      // The battery swells!
                  }else{
                      // unhandled exception found, try again later.
                  }
    
                  Log.d("BatteryTest", "Battery status: " + val);
              }
          }
    
          @Override
          protected void onStop() {
              super.onStop();
    
              if (mBattery != null) {
                  mBattery.unbindService();
              }
              unregisterReceiver(mBatteryRcvr);
          }
      }
     

    Kotlin Sample Code:

     package com.mitac.kotlinapitest
    
     import android.content.Context
     import androidx.appcompat.app.AppCompatActivity
     import android.os.Bundle
     import android.util.Log
     import com.mitac.api.libs.Battery
     import com.mitac.api.libs.ServiceStatusCallback
    
     class MainActivity : AppCompatActivity() {
         val TAG: String = "BatteryAPI"
         var battery: Battery? = null
    
         override fun onCreate(savedInstanceState: Bundle?) {
             super.onCreate(savedInstanceState)
             setContentView(R.layout.activity_main)
             testBatteryApi(applicationContext)
         }
    
         fun testBatteryApi(context: Context) {
             battery = Battery(context, object: ServiceStatusCallback {
                 override fun ready() {
                     Log.d(TAG, "service is ready")
                     testBatteryObject(battery!!)
                 }
    
                 override fun stopped() {
                     Log.d(TAG, "service is stopped")
                 }
             })
         }
    
         fun testBatteryObject(batteryObj: Battery) {
             var status = batteryObj!!.getBatterySwellStatus()
         }
     }
     
    • Constructor Detail

      • Battery

        public Battery​(android.content.Context context)
        This constructor will not bind the service automatically. Please call bindService() at least once before using APIs.
        Parameters:
        context - better to use Context.getApplicationContext() to prevent memory leaks
        Throws:
        java.lang.IllegalArgumentException - if context is null.
      • Battery

        public Battery​(android.content.Context context,
                       ServiceStatusCallback callback)
        This constructor will bind the service automatically.
        Parameters:
        context - better to use Context.getApplicationContext() to prevent memory leaks
        callback - used to get the notification when service is ready.
        Throws:
        java.lang.IllegalArgumentException - if context or callback is null.
        See Also:
        ServiceStatusCallback
    • Method Detail

      • isServiceReady

        public boolean isServiceReady()
        Check if service is ready.
        Returns:
        true if ready; false otherwise.
      • getBatterySwellStatus

        public java.lang.String getBatterySwellStatus​(BatteryStatusType type)
        Get battery swell status
        Parameters:
        type - , a value of battery swell status BatteryStatusType e.g BatteryStatusType.SWELL_STATUS:swell status
        Returns:
        String value returned, -1 means input type not correct
      • getBatteryInfo

        public java.lang.String getBatteryInfo​(BatteryInfoType type)
        Get battery info value
        Parameters:
        type - , a value of battery info BatteryInfoType e.g BatteryInfoPath.SOH_DATA:the Battery state of health path
        Returns:
        String value of Battery info returned, -1 mean input type not correct null means read file failed
      • getChargeOnHoldStatus

        public java.lang.String getChargeOnHoldStatus()
        Get battery charge on hold status value
        Returns:
        string value of battery charge on holds returned:
        returns a hexadecimal data bit,every conditions can exist at the same time, reference below
        • 80--1000 0000 --bit(7) battery well
        • 40--0100 0000 --bit(6) Reserved
        • 20--0010 0000 --bit(5) charge error
        • 10--0001 0000 --bit(4) Reserved
        • 8 --0000 1000 --bit(3) temp out of range
        • 4 --0000 0100 --bit(2) Reserved
        • 2 --0000 0010 --bit(1) smart charge
        • 1 --0000 0001 --bit(0) Reserved
        • 0 battery status good
        • null means read file failed
      • getBatteryChargeICType

        public java.lang.String getBatteryChargeICType()
        Get battery charge IC type
        Returns:
        string value of charge IC type name returned, It may be A1141, Bq27542, Unknown. null means failed.
      • getBatteryCoverStatus

        public java.lang.String getBatteryCoverStatus()
        Get battery cover status
        Returns:
        string value of cover status returned, 0 means cover is opened, 1 means cover is closed, otherwise are failure
      • getCPUTemperature

        public java.lang.String getCPUTemperature()
        Get cpu temperature
        Returns:
        string value of cpu temperature returned, the obtained value is divided by 1000 to obtain the Celsius temperature value decimal data 27300 means cpu temperature is 27.3℃
      • doBatteryFWUpdate

        public java.lang.String doBatteryFWUpdate()
        Not public API do battery fw update for abnormal behavior
        Returns:
        return the status string from update application