Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Vb.net Mobile - turn display backlight on AND off again

Posted on 2009-07-14
11
Medium Priority
?
1,679 Views
Last Modified: 2013-12-21
I'm working on a Windows Mobile application using vb.net (VS2008)

The application has 2 buttons.
Button1 should turn backlight fully on and leave it that way
Button2 should release the settings from button1 and give power control back to the mobile device.

I am a beginner in mobile programming and would appreciate if the help would come as the exact code. Thanks in advance !

I already have a class that should be able to do this, but I don't know how to use it. ( see code )
Public Class Backlight
 
 
 
'ensure the power requirement is released
 
Protected Overrides Sub Finalize()
 
Release()
 
End Sub
 
'handle to the power requirement
 
Private handle As IntPtr
 
 
 
Private Enum PowerState
 
PwrDeviceUnspecified = -1
 
'full on
 
D0 = 0
 
'low power
 
D1 = 1
 
'standby
 
D2 = 2
 
'sleep
 
D3 = 3
 
'off
 
D4 = 4
 
PwrDeviceMaximum = 5
 
End Enum
 
 
 
'keep the backlight lit
 
Public Sub Activate()
 
'request full power
 
handle = SetPowerRequirement("BKL1:", PowerState.D0, 1, IntPtr.Zero, 0)
 
End Sub
 
 
 
'release power requirement
 
Public Sub Release()
 
If handle.ToInt32() <> 0 Then
 
Dim result As Integer
 
result = ReleasePowerRequirement(handle)
 
handle = IntPtr.Zero
 
End If
 
End Sub
 
Private Declare Function SetPowerRequirement Lib "coredll.dll" (ByVal pvDevice As String, ByVal DeviceState As PowerState, ByVal DeviceFlags As Integer, ByVal pvSystemState As IntPtr, ByVal StateFlags As Integer) As IntPtr
 
Private Declare Function ReleasePowerRequirement Lib "coredll.dll" (ByVal handle As IntPtr) As Integer
 
End Class

Open in new window

0
Comment
Question by:karstieman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
11 Comments
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 24860647
I'm not sure it will help.  It is not VB. But it is something:
http://www.hjgode.de/wp/2009/07/14/howto-run-an-application-periodically/
(please read all - in the end the code with the backlight)
In order to wake up the device, you can that:
void RunAtTime()
{

SYSTEMTIME t; memset(&t, 0, sizeof(SYSTEMTIME));

t.wMonth = 7;
t.wDay = 15;
t.wYear = 2009;
t.wHour = 18;
t.wMinute = 45;

CeRunAppAtTime(_T("\\Windows\\iexplorer.exe", &t);

}
 Here is an example from MSDN (but it is again C++):
http://msdn.microsoft.com/en-us/library/aa455153.aspx
Only here it looks like VB:
http://www.eggheadcafe.com/software/aspnet/29621074/why-does-this-backlight-t.aspx
 
0
 
LVL 5

Author Comment

by:karstieman
ID: 24876940
Ok, i've got a bit further.
If I use the code: --> SetPowerRequirement("BKL1:", PowerState.D0, 1, IntPtr.Zero, 0) <--
Then the screen we be lit and keep lit.

But what code can I use to turn the light of again so the mobile device goes back to normal ?
0
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 1500 total points
ID: 24877145
ReleasePowerRequirement(handle from SetPower here);
 
0
Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

 
LVL 5

Author Comment

by:karstieman
ID: 24877164
Thank you Pgnatyuk,

So if I'm right this should work ?

dim handle as intptr = SetPowerRequirement("BKL1:", PowerState.D0, 1, IntPtr.Zero, 0)
ReleasePowerRequirement(handle)
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 24877171
I hope so. I don't know VB to confirm that this ok. It looks correct.
I think I even used just CloseHandle and it was okay.
0
 
LVL 5

Author Comment

by:karstieman
ID: 24877543
Just tested and it doesn't work... :-(

How do you use CloseHandle ?

Bye the way, should the handle always be the same ?
Because by using the following code , I found that handle is always the same...

dim handle as intptr = intptr.zero
MsgBox(Handle.ToInt32)  --> shows  2080980208
SetPowerRequirement("BKL1:", PowerState.D0, 1, IntPtr.Zero, 0)
MsgBox(Handle.ToInt32)  --> shows   2080980208
ReleasePowerRequirement(handle)
MsgBox(Handle.ToInt32)  --> shows  2080980208
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 24878844
The handle is ok. You could changed other parameters, so it's always on. When you close the app, the backlight behaves well?
In the first comment I posted the link: http://www.hjgode.de/wp/2009/07/14/howto-run-an-application-periodically/
 //Switch Backlight ON
hPower = SetPowerRequirement(_T("BKL1:"), D0, POWER_NAME, NULL, 0); //POWER_FORCE
//set to full power
SetSystemPowerState(NULL, POWER_STATE_ON, POWER_FORCE);

 Then a background thread with SystemIdleTimerReset():
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
 bThreadEnded=false;
 while(bRunThread){
  SystemIdleTimerReset();
  Sleep(1000);
 }
 bThreadEnded=true;
 return 0;
}
For the backlight: dont forget to release the power request:
if(hPower != NULL)
 HRESULT result = ReleasePowerRequirement(hPower);
 
Accordingly to MSDN this code ReleasePowerRequirement(s_hBacklightReq)) should work. The arcticle even has name "Program Applications to Turn the Smartphone Backlight Off and On" : http://msdn.microsoft.com/en-us/library/aa455153.aspx
In the SDK you can find Power Manager sample: http://msdn.microsoft.com/en-us/library/ms880669.aspx
Try it.
 
0
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 24906598
the sad fact is that once SetPowerRequirement() is called, it quite often never comes back to normal :)

you might try to broadcast WM_SETTINGCHANGED or WM_WININICHANGED with magic number 0xF2 as WPARAM, but it may not work. It seems you need to kick off device.exe (as it deals with drivers) to get it 'reset' its state.
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 24907295
Yes, I do remember something about the broadcast: SendMessage(HWND_BROADCAST,WM_SETTIGNSCHANGE,0xF2,0)
Do you use SystemIdleTimerReset() or touched the registry settings defining the timeouts?
BTW, here is the thread about the same: http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_22921526.html?sfQueryTermInfo=1+setpowerrequir
 
0
 
LVL 5

Author Comment

by:karstieman
ID: 24912760
I've got it working now. The solution is really stupid though...

When calling:
dim handle as intptr = SetPowerRequirement("BKL1:", PowerState.D0, 1, IntPtr.Zero, 0)

You have to wait at least 30seconds before calling:
ReleasePowerRequirement(handle)

Then it works.
Thanks for the help !
0
 
LVL 5

Author Closing Comment

by:karstieman
ID: 31603246
Thanks for the help ! It's appreciated !
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question