karstieman
asked on
Vb.net Mobile - turn display backlight on AND off again
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 )
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
ASKER
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 ?
If I use the code: --> SetPowerRequirement("BKL1:
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 ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you Pgnatyuk,
So if I'm right this should work ?
dim handle as intptr = SetPowerRequirement("BKL1: ", PowerState.D0, 1, IntPtr.Zero, 0)
ReleasePowerRequirement(ha ndle)
So if I'm right this should work ?
dim handle as intptr = SetPowerRequirement("BKL1:
ReleasePowerRequirement(ha
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.
I think I even used just CloseHandle and it was okay.
ASKER
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(ha ndle)
MsgBox(Handle.ToInt32) --> shows 2080980208
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:
MsgBox(Handle.ToInt32) --> shows 2080980208
ReleasePowerRequirement(ha
MsgBox(Handle.ToInt32) --> shows 2080980208
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/20 09/07/14/h owto-run-a n-applicat ion-period ically/
//Switch Backlight ON
hPower = SetPowerRequirement(_T("BK L1:"), 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(hP ower);
Accordingly to MSDN this code ReleasePowerRequirement(s_ hBacklight Req)) should work. The arcticle even has name "Program Applications to Turn the Smartphone Backlight Off and On" : http://msdn.microsoft.com/ en-us/libr ary/aa4551 53.aspx
In the SDK you can find Power Manager sample: http://msdn.microsoft.com/ en-us/libr ary/ms8806 69.aspx
Try it.
In the first comment I posted the link: http://www.hjgode.de/wp/20
//Switch Backlight ON
hPower = SetPowerRequirement(_T("BK
//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(hP
Accordingly to MSDN this code ReleasePowerRequirement(s_
In the SDK you can find Power Manager sample: http://msdn.microsoft.com/
Try it.
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.
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.
Yes, I do remember something about the broadcast: SendMessage(HWND_BROADCAST ,WM_SETTIG NSCHANGE,0 xF2,0)
Do you use SystemIdleTimerReset() or touched the registry settings defining the timeouts?
BTW, here is the thread about the same: http://www.experts-exchang e.com/Prog ramming/La nguages/C_ Sharp/Q_22 921526.htm l?sfQueryT ermInfo=1+ setpowerre quir
Do you use SystemIdleTimerReset() or touched the registry settings defining the timeouts?
BTW, here is the thread about the same: http://www.experts-exchang
ASKER
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(ha ndle)
Then it works.
Thanks for the help !
When calling:
dim handle as intptr = SetPowerRequirement("BKL1:
You have to wait at least 30seconds before calling:
ReleasePowerRequirement(ha
Then it works.
Thanks for the help !
ASKER
Thanks for the help ! It's appreciated !
http://www.hjgode.de/wp/20
(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("\\Windo
}
Here is an example from MSDN (but it is again C++):
http://msdn.microsoft.com/
Only here it looks like VB:
http://www.eggheadcafe.com