[Windows Mobile]行動裝置是否與電腦連線

  • 6904
  • 0
  • 2013-04-15

[Windows Mobile]行動裝置是否與電腦連線

 

1. Question

問題來自 MSDN Forums : ActiveSync 連線狀態

簡述如下

如何抓出目前是否與 ActiveSync 連線狀態,比如有連線就等於 True,沒連線就等於 False

我試過 ActiveSyncStatus.Synchronizing 以及 SystemState.ActiveSyncStatus

似乎都不是可以判斷 PDA 是否有與 ActiveSync 連線

 

2. Solution

使用 SystemState.CradlePresent 屬性: Gets a value indicating whether the device is connected to a cradle.

使用時需將 Microsoft.WindowsMobile、Microsoft.WindowsMobile.Status 加入參考

 

程式碼

以下程式碼是當 Button Click 時,取得並顯示 SystemState.CradlePresent 屬性 的狀態


using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Microsoft.WindowsMobile.Status;
namespace SmartDeviceProject7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // SystemState.CradlePresent : Gets a value indicating whether the device is connected to a cradle.
            textBox1.Text = SystemState.CradlePresent.ToString();
            // 未連線時顯示 False
            // 連線時顯示 True
        }
    }
}

 

以下程式碼是當 System Status 改變時,取得並顯示 SystemState.CradlePresent 屬性 的狀態


using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Microsoft.WindowsMobile.Status;
namespace SmartDeviceProject7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        SystemState mSystemState;

        // 當狀態改變時觸發事件
        void mSystemState_Changed(object sender, ChangeEventArgs args)
        {
            // SystemState.CradlePresent : Gets a value indicating whether the device is connected to a cradle.
            textBox1.Text = SystemState.CradlePresent.ToString();
            // 未連線時顯示 False
            // 連線時顯示 True
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // 加入事件
            mSystemState = new SystemState(SystemProperty.CradlePresent);
            mSystemState.Changed += new ChangeEventHandler(mSystemState_Changed);
            mSystemState_Changed(null, null);
        } 
    }
}

 

執行結果

未連線時

image image

 

連線時

image image

 

3. Reference

SystemState.CradlePresent 屬性

How to Get Activesync Status in Smart Device