Android Question get / set brightness

Similar threads

B4A Tutorial Custom transitions between activities
B4A Code Snippet Draw on top of other apps permission
B4A Code Snippet Upload files with WebView
B4A Code Snippet Advertising id
B4A Code Snippet Access Wifi Information

yaniv hanya

Active Member
Licensed User
why when I use this code

B4X:
Dim ref As Reflector
    ref.Target = ref.GetActivity
    ref.Target = ref.RunMethod("getWindow")
    ref.Target = ref.RunMethod("getAttributes")
    Dim brightness As Float = ref.GetField("screenBrightness")
I get brightness = -1
how can I get the real value?
 

yaniv hanya

Active Member
Licensed User
yes i saw this but how can i get it out from the automatic state?
I did not find it on the device settings
 

MarkusR

Well-Known Member
Licensed User
It really looks like what I'm looking for.
Do you have any idea how I implement this in b4a?
first i would test it in android studio and if it works makeing a library for b4a then.
 

Erel

Administrator
Staff member
Licensed User
The code to set the brightness mode to normal is:
B4X:
Sub SetBrightnessToNormalMode
   Dim jo As JavaObject
   jo.InitializeContext
   Dim System As JavaObject
   System.InitializeStatic("android.provider.Settings.System")
   System.RunMethod("putInt", Array( _
       jo.RunMethod("getContentResolver", Null), _
       System.GetField("SCREEN_BRIGHTNESS_MODE"), _
       System.GetField("SCREEN_BRIGHTNESS_MODE_MANUAL")))
End Sub
However this is not enough. You also need to request the special WRITE_SETTINGS permission.
You can see an example here: https://www.b4x.com/android/forum/threads/permission-write_settings.94311/#post-597465
 

MarkusR

Well-Known Member
Licensed User
You cannot get the brightness value if it is set to auto mode.
i made a test project, can't get the auto value as Erel said, only if set to manual :(
 

Attachments

yaniv hanya

Active Member
Licensed User
For some reason rp.CheckAndRequest("android.permission.WRITE_SETTINGS") did not open the permission dialogue on my device
so i get this exception
Error occurred on line: 67 (Main)
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
at b4a.example.brightness.main._setbrightnessmode(main.java:452)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA$2.run(BA.java:370)
at anywheresoftware.b4a.BA.setActivityPaused(BA.java:442)
at b4a.example.brightness.main$ResumeMessage.run(main.java:306)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6251)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
Caused by: java.lang.SecurityException: b4a.example.brightness was not granted this permission: android.permission.WRITE_SETTINGS.
at android.os.Parcel.readException(Parcel.java:1683)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:188)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
at android.content.ContentProviderProxy.call(ContentProviderNative.java:646)
at android.provider.Settings$NameValueCache.putStringForUser(Settings.java:1578)
at android.provider.Settings$System.putStringForUser(Settings.java:1958)
at android.provider.Settings$System.putIntForUser(Settings.java:2063)
at android.provider.Settings$System.putInt(Settings.java:2057)
... 19 more
** Activity (main) Resume **
I noticed it in more permissions i was requesting
Do you have any idea why?

while runnig I get this log
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
but nothing hapends
 
Last edited:

MarkusR

Well-Known Member
Licensed User
i also noticed.
i think because every user will agree all permissions (or u can not use most of all apps^^)
google hide this settings. but u can open the os permissions dialog via intent where the user can enable it manual.
 

yaniv hanya

Active Member
Licensed User
in the runtime permissions erel made and in the firs test i made for it few weeks ago it did opend.
but since then they dont show.
this is probably why the BrightnessTest.zip you posted befor don't work for me because the permission is false
but why they do not open?
 
Last edited:

jimmyF

Active Member
Licensed User
Delete the app from the device and then re-install it.
 

Erel

Administrator
Staff member
Licensed User

MarkusR

Well-Known Member
Licensed User
do you have any benefit from the sensor data itself?
it is possible to read this sensor in app game kit with GetRawLightLevel so it should also possible in b4a.
If an ambient light sensor exists, get the measurement of the surrounding light in lumels as a float.
 

yaniv hanya

Active Member
Licensed User
Thanks friends
With the help of your help, I succeeded in solving the issue.
The dialogue has not yet opened to me, but I think it's a subject for new thread.
 
Top