Frage

I have an app and I need to know when the windows mobile device it's running on is docked, any ideas how to do this?

War es hilfreich?

Lösung 2

A Windows Mobile device is docked when you can resolve the PPP_PEER machine name, which can be used for TCP communication between the device and the host it is docked to. This is, however, only true when the cradle is actually connected to the PC.

   

public static bool ActiveSyncConnected
{
    get
    {
        try
        {
            IPHostEntry entry = Dns.GetHostEntry("PPP_PEER");
            return true;
        }
        catch
        {
            return false;
        }
    }
}

   

Another solution (at least for industrial devices) would be to check the AC line state, i.e. whether it is currently connected to the power adapter. This is true when the device is cradled, no matter whether connected to a PC or not.

public bool CurrentlyConnectedToACLine
{
    get
    {
        SYSTEM_POWER_STATUS_EX status = new SYSTEM_POWER_STATUS_EX();
        if (GetSystemPowerStatusEx(status, true))
            return status.ACLineStatus != 0;
        else
            return false;
    }
}

[StructLayout(LayoutKind.Sequential)]
internal class SYSTEM_POWER_STATUS_EX
{
    public byte ACLineStatus = 0;
    public byte BatteryFlag = 0;
    public byte BatteryLifePercent = 0;
    public byte Reserved1 = 0;
    public uint BatteryLifeTime = 0;
    public uint BatteryFullLifeTime = 0;
    public byte Reserved2 = 0;
    public byte BackupBatteryFlag = 0;
    public byte BackupBatteryLifePercent = 0;
    public byte Reserved3 = 0;
    public uint BackupBatteryLifeTime = 0;
    public uint BackupBatteryFullLifeTime = 0;
}

[DllImport("coredll.dll")]
private static extern bool GetSystemPowerStatusEx(SYSTEM_POWER_STATUS_EX lpSystemPowerStatus, bool fUpdate);

Andere Tipps

In addition to Thorsten's suggestions, you can also look for the NOTIFICATION_EVENT_RS232_DETECTED notification. The event name is legacy from back when USB was rare so ignore the fact that it says RS232, even with USB, the RS232 event occurs.

You can use CeRunAppAtEvent to hook the notification. Again, ignore what the name of the API implies - you can also use it to set a system event which you can WaitForSingleObject on.

See the answer to this question for details and implementation.

All above answers are right but use different approaches. The question is, why your app needs to know if the device is docked.

If for using a PC ActiveSync Paasthru TCP/IP connection, then using the PPP_PEER solution is best.

If you need just to know, if the device is externally powered, then using PowerstatusEx and AC_LINE is best. This can also be used via a power notification queue, so that the app does not need to poll for the status (see also the NOTIFICATION_EVENT_RS232_DETECTED approach).

If you need just to know, that the device has been cradled, NOTIFICATION_EVENT_RS232_DETECTED approach is best. For example if you have a vehicle dock and want to start communicating with a vehicle blackbox connected to the dock.

You see, although all solutions look more or less the same at first look, they have different intentions.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top