NullPointerException at anywheresoftware.b4a.phone.Phone.SetScreenBrightne ss

NeoTechni

Well-Known Member
Licensed User
Longtime User
I'm trying to use it to turn off the screen when the proximity sensor reads something is close to it/turn it back on when the proximity sensor no longer reads the object.

I'm using

B4X:
Sub SetScreenBrightness(Value As Float)
   Dim phone1 As Phone
   phone1.SetScreenBrightness(Value)
End Sub

Inside a code module, cause when I try the same code within an activity module it won't let me use SetScreenBrightness, just Initialize, InitializeWithPhoneState and StopListening. And it won't work inside a service module as per the documentation.

When an object is close the proximity sensor reads 0 so I use SetScreenBrightness(0) and when an object isn't close it reads mysensor.MaxValue so I use SetScreenBrightness(-1) which should be automatic brightness

And I get:

java.lang.NullPointerException
at anywheresoftware.b4a.phone.Phone.SetScreenBrightness(Phone.java:183)

Caused by: java.lang.NullPointerException
at anywheresoftware.b4a.phone.Phone.SetScreenBrightness(Phone.java:183)
 

madSac

Active Member
Licensed User
Longtime User
I'm trying to use it to turn off the screen when the proximity sensor reads something is close to it/turn it back on when the proximity sensor no longer reads the object.

I'm using

B4X:
Sub SetScreenBrightness(Value As Float)
   Dim phone1 As Phone
   phone1.SetScreenBrightness(Value)
End Sub

Inside a code module, cause when I try the same code within an activity module it won't let me use SetScreenBrightness, just Initialize, InitializeWithPhoneState and StopListening. And it won't work inside a service module as per the documentation.

When an object is close the proximity sensor reads 0 so I use SetScreenBrightness(0) and when an object isn't close it reads mysensor.MaxValue so I use SetScreenBrightness(-1) which should be automatic brightness

And I get:

java.lang.NullPointerException
at anywheresoftware.b4a.phone.Phone.SetScreenBrightness(Phone.java:183)

Caused by: java.lang.NullPointerException
at anywheresoftware.b4a.phone.Phone.SetScreenBrightness(Phone.java:183)

Changing value of screenbrightness to 0 is useless approach.You should partially lock the device.
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
I need to instantly turn the screen off and be able to turn it instantly back on
Wouldnt relocking it leave it on till the users timer expires?
 
Upvote 0

madSac

Active Member
Licensed User
Longtime User
I think setting screen brightness to 0 wouldn't lock device from just.
You can load a layout with black background.

I think that you are trying to make your application similar to phone call ?
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Setting it to zero is supposed to turn off the screen and stop it from responding to touch which is exactly what i need.
Loading a blank activity would only do one of those
 
Upvote 0

madSac

Active Member
Licensed User
Longtime User
Ok ...i got solution
i used below code and it worked
B4X:
Dim p As Phone
Dim f As Float
f=0
p.SetScreenBrightness(f)
 
Upvote 0

madSac

Active Member
Licensed User
Longtime User
Don't know why setting 0 directly gives error ...i even tried type casting to float but it didnot worked.
 
Upvote 0

madSac

Active Member
Licensed User
Longtime User
During an experiment I found that this code generates an error if used with a service. Don't know why. :signOops:
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Ok ...i got solution
i used below code and it worked
B4X:
Dim p As Phone
Dim f As Float
f=0
p.SetScreenBrightness(f)

I'm still getting a null pointer

B4X:
Sub SetScreenBrightness(Value As Float)
   Dim phone1 As Phone, temp As Float=value
   phone1.SetScreenBrightness(temp)
   Log(GetScreenBrightness)
End Sub
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
I am copy/pasting code directly from that, it's not working.

I can use:
Phone1.Initialize
Phone1.InitializeWithPhoneState
Phone1.StopListening

And nothing else from an activity.
I also need a brightness of 0 to turn the screen off. That code wont allow it
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Are you trying to run it from a service? See my last post (post #9 or #10).

No, the documentation says it cant be run from one
It wont give me setscreenbrightness from an activity
So that only leaves me with a code module, and i get null pointer
 
Upvote 0
Top