TheoJones
TheoJones

Reputation: 359

How do I tell if a windows mobile device is connected to external power using VB.Net?

Windows Mobile devices have different behaviour for suspending when the device is on battery power, or on external power.

in my application, written using VB.net, I need to be able to determine whether the device has external power connected.

is there a method to get this status from the Compact framework?

Upvotes: 3

Views: 2290

Answers (4)

Febraiz
Febraiz

Reputation: 31

First, implement all this by copying it into your code (module or class): https://msdn.microsoft.com/en-us/library/aa457088.aspx

Then:

Public Function isOnCharge() As Boolean
  Dim status As New SYSTEM_POWER_STATUS_EX2
  GetSystemPowerStatusEx2(status, Convert.ToUInt32(Marshal.SizeOf(status)), True)    

  If status.BatteryCurrent < 10000 Then
       return true 'plugged in
  else return false 'Unplugged
  End If
End Function

I did this because samples with SystemState.PowerBatteryState & BatteryState.Charging didn't work, so i managed another way to make it work.

If my device was plugged, status.batteryCurrent was less than 10000, but if it was unplugged, its value was beyond 4000000. You can do some test on yours if you want to have more precise values. Cheers !

Upvotes: 1

Joel
Joel

Reputation: 2361

This may go beyond what you were asking but I wrote something on the Windows Mobile Powermanagement APIS. It uses the same APIs that Chris just referenced but is .Net oriented (sorry, it is in C#, not VB.Net).

http://www.codeproject.com/KB/mobile/WiMoPower1.aspx

Upvotes: 0

ctacke
ctacke

Reputation: 67198

If you're using WIndowsMobile 5.0 and later only, the State and Notification Broker is where to look, specifically at the Status namespace.

For broader support, you can detect the state transition (to or from AC power) calling CeRunAppAtEvent (this can set a named event rather than just running an app) with the NOTIFICATION_EVENT_AC_APPLIED or NOTIFICATION_EVENT_AC_REMOVED event codes. This is what the DeviceManagement class in the Smart Device Framework does.

You can detect current state (instead of transitions) by calling GetSystemPowerStatusEx2.

Upvotes: 3

bezmax
bezmax

Reputation: 26152

if (SystemState.PowerBatteryState & BatteryState.Charging) ...

Upvotes: 1

Related Questions