BatteryLevel of OSLibrary is not working

masterleous

Member
Licensed User
Longtime User
Hi all,

I am using OS Library for getting Battery charge level. In OS Library almost all functions which i had tested are working except for the BatteryLevel. with its permission android.permission.BATTERY_STATS

In Library Description it is mention that we need to first call BatteryReceiver.

Upon calling this function, Java errors comes, and after disabling BatteryReceiver calling BatteryLevel gives "null" value.

Need your help

Thanks
 

melamoud

Active Member
Licensed User
Longtime User
I get a null pointer exception with this code

B4X:
Dim os As OperatingSystem
   os.Initialize("OS")
   os.BatteryReceiver
   Utilities.LogM(0,"Battery is:" & os.BatteryLevel)
the log
B4X:
** Activity (main) Create, isFirst = true **


main_activity_create (java line: 271)
java.lang.NullPointerException


   at com.rootsoft.oslibrary.OSLibrary.BatteryReceiver(OSLibrary.java:76)
   at appsright.musicnotifier.main._activity_create(main.java:271)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
   at appsright.musicnotifier.main.afterFirstLayout(main.java:89)
   at appsright.musicnotifier.main.access$100(main.java:16)
   at appsright.musicnotifier.main$WaitForLayout.run(main.java:74)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:130)
   at android.app.ActivityThread.main(ActivityThread.java:3806)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
   at dalvik.system.NativeStart.main(Native Method)
java.lang.NullPointerException
 
Upvote 0

masterleous

Member
Licensed User
Longtime User

I am facing same problem, tested with Android AVD as well as with Samsung Galaxy Gio, Don't know why this is not working. May need Erel or other user attentions
 
Upvote 0

masterleous

Member
Licensed User
Longtime User
It's a long shot but if you have Dimmed the library in Process_Globals try moving it to Globals. BatteryReceiver looks like the only method in the library that needs an activity reference and that is where it is throwing the NullPointerException.

I shall update you after try, Thanks for your advice.
 
Upvote 0

mlc

Active Member
Licensed User
Longtime User
is there any solution?

This inline java code works for me, no permission is needed:
It needs the JavaObject library.

B4X:
Sub Process_Globals
    Dim nativeMe As JavaObject
End Sub
Sub Activity_Create(FirstTime As Boolean)
  
    If FirstTime Then
        nativeMe.InitializeContext
    End If
    Msgbox(nativeMe.RunMethod("getBatteryLevel", Null)  & " %", "BatteryLevel")
End Sub

#If JAVA

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;

public float getBatteryLevel() {
    Intent batteryIntent = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);  

    return ((float)level / (float)scale) * 100.0f;
}  

#End If
 
Upvote 0

JoanRPM

Active Member
Licensed User
Longtime User
Yes it works.
I added a Java method to see if it is charging.

B4X:
public boolean getBatteryCharging()
{    Intent batteryIntent = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int status = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                          status == BatteryManager.BATTERY_STATUS_FULL;

    return ((boolean)isCharging);
}

It is also possible to do the same with this other code (needs the phone library):

B4X:
Sub Globals
    Dim P as PhoneEvents
End Sub

Sub Activity_Create(FirstTime As Boolean)
    P.Initialize("P")
End Sub

Sub P_BatteryChanged (Level As Int, Scale As Int, Plugged As Boolean, Intent As Intent)
    Log("BatteryChanged: Level = " & level & ", Scale = " & scale & ", Plugged = " & Plugged)
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…