在Windows Mobile 6.5.3中禁​​用或启用手机无线电

时间:2014-06-19 09:45:33

标签: c# windows-mobile compact-framework windows-mobile-6.5 .net-cf-3.5

我想在低连接区域启用或禁用手机无线电。是否有可能做到这一点?我正在使用motorola ES400进行开发。

3 个答案:

答案 0 :(得分:2)

首先:导入这些dll

    [DllImport("ossvcs.dll", EntryPoint = "#276", CharSet = CharSet.Unicode)]
    private static extern uint GetWirelessDevice(ref IntPtr pDevice, int pDevVal);

    [DllImport("ossvcs.dll", EntryPoint = "#273", CharSet = CharSet.Unicode)]
    private static extern uint ChangeRadioState(ref RDD pDevice, int dwState, int saveAction);

    [DllImport("ossvcs.dll", EntryPoint = "#280", CharSet = CharSet.Unicode)]
    private static extern uint FreeDeviceList(IntPtr pDevice);

这是我用于摩托罗拉MC65的代码的副本,它也适用于你的代码。

    [StructLayout(LayoutKind.Auto)]
    struct RADIODEVSTATE
    {
        public const int RADIODEVICES_ON = 1;
        public const int RADIODEVICES_OFF = 0;
    }

    /*
    typedef enum _RADIODEVTYPE
    {
        RADIODEVICES_MANAGED = 1,
        RADIODEVICES_PHONE,
        RADIODEVICES_BLUETOOTH,
    } RADIODEVTYPE;
     */
    [StructLayout(LayoutKind.Auto, CharSet = CharSet.Unicode)]
    struct RADIODEVTYPE
    {
        public const int RADIODEVICES_MANAGED = 1;
        public const int RADIODEVICES_PHONE = 2;
        public const int RADIODEVICES_BLUETOOTH = 3;
    }

    /*
    typedef enum _SAVEACTION
    {
        RADIODEVICES_DONT_SAVE = 0,
        RADIODEVICES_PRE_SAVE,
        RADIODEVICES_POST_SAVE,
    } SAVEACTION;
     */
    [StructLayout(LayoutKind.Auto, CharSet = CharSet.Unicode)]
    struct SAVEACTION
    {
        public const int RADIODEVICES_DONT_SAVE = 0;
        public const int RADIODEVICES_PRE_SAVE = 1;
        public const int RADIODEVICES_POST_SAVE = 2;
    }

    /*
    struct RDD 
    {
        RDD() : pszDeviceName(NULL), pNext(NULL), pszDisplayName(NULL) {}
        ~RDD() { LocalFree(pszDeviceName); LocalFree(pszDisplayName); }
        LPTSTR   pszDeviceName;  // Device name for registry setting.
        LPTSTR   pszDisplayName; // Name to show the world
        DWORD    dwState;        // ON/off/[Discoverable for BT]
        DWORD    dwDesired;      // desired state - used for setting registry etc.
        RADIODEVTYPE    DeviceType;         // Managed, phone, BT etc.
        RDD * pNext;    // Next device in list
    }; //radio device details
     */
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    struct RDD
    {
        [MarshalAs(UnmanagedType.LPTStr)]
        public string pszDeviceName;

        [MarshalAs(UnmanagedType.LPTStr)]
        public string pszDisplayName;

        public uint dwState;
        public uint dwDesired;
        public int DeviceType;
        public IntPtr pNext;
    }


    private static bool SetDeviceState(int dwDevice, int dwState)
    {
        var pDevice = new IntPtr(0);

        //Get the first wireless device
        var result = GetWirelessDevice(ref pDevice, 0);

        if (result != 0)
            return false;

            //While we're still looking at wireless devices
            while (pDevice != IntPtr.Zero)
            {
                //Marshall the pointer into a C# structure
                var device = (RDD)System.Runtime.InteropServices.Marshal.PtrToStructure(pDevice, typeof(RDD));

                //If this device is the device we're looking for
                if (device.DeviceType == dwDevice)
                {
                    //Change the state of the radio
                    result = ChangeRadioState(ref device, dwState, SAVEACTION.RADIODEVICES_PRE_SAVE);
                }

                //Set the pointer to the next device in the linked list
                pDevice = device.pNext;
            }

            //Free the list of devices
            FreeDeviceList(pDevice);

        //Turning off radios doesn't correctly report the status, so return true anyway.
        return result == 0 || dwState == RADIODEVSTATE.RADIODEVICES_OFF;
    }

要关闭手机,只需拨打以下方法:

    /// <summary>
    /// Disables the phone radio on device
    /// </summary>
    public void DisablePhoneRadio()
    {
            SetDeviceState(RADIODEVTYPE.RADIODEVICES_PHONE, RADIODEVSTATE.RADIODEVICES_OFF);
    }

因此,只需使用所需的条件语句,并在需要禁用它时调用DisablePhoneRadio(),并且在启用手机无线电时,只需使用RADIODEVSTATE.RADIODEVICES_ON与RADIODEVSTATE.RADIODEVICES_ON一起拍摄:

    /// <summary>
    /// Enables the phone radio on device
    /// </summary>
    public void EnablePhoneRadio()
    {
            SetDeviceState(RADIODEVTYPE.RADIODEVICES_PHONE, RADIODEVSTATE.RADIODEVICES_ON);
    }

希望这有帮助!

答案 1 :(得分:1)

您需要从GetDeviceList P / Invoke ChangeRadioStateossvcs.dll。实际执行此操作的代码对于SO帖子来说有点长,所以我会留给你让它解决 - 它不是很难(有一些C code here,并且有一些C# code on CodeProject甚至,我没有用它,所以YMMV)。

另一种选择是在SDF中使用Radios类,它已经包含了这些类。

答案 2 :(得分:0)

也检查此线程。有趣的是采取线程调用并监控设备连接状态,并在Windows Mobile上打开和关闭蜂窝无线电。

http://www.codeproject.com/Messages/4117749/Re-Csharp-Wrapper.aspx