Reputation: 1466
I would like to enable or disable phone radio in low connectivity areas. Is it possible to do this? I am using motorola ES400 for development.
Upvotes: 1
Views: 1978
Reputation: 1205
Check this thread as well. Interesting take on threaded calls and monitoring the device connectivity state and powering on and off the cellular radio on windows mobile.
http://www.codeproject.com/Messages/4117749/Re-Csharp-Wrapper.aspx
Upvotes: 0
Reputation: 41
First : Import these dlls
[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);
And here's a copy of the code I use for the Motorola MC65, which should work for yours as well.
[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;
}
And to turn off the phone simply call following method:
/// <summary>
/// Disables the phone radio on device
/// </summary>
public void DisablePhoneRadio()
{
SetDeviceState(RADIODEVTYPE.RADIODEVICES_PHONE, RADIODEVSTATE.RADIODEVICES_OFF);
}
So just use whatever conditional statements are needed and call DisablePhoneRadio() whenever you need to disable it, and when enabling the phone radio simply swat out the RADIODEVSTATE.RADIODEVICES_OFF with RADIODEVSTATE.RADIODEVICES_ON like so:
/// <summary>
/// Enables the phone radio on device
/// </summary>
public void EnablePhoneRadio()
{
SetDeviceState(RADIODEVTYPE.RADIODEVICES_PHONE, RADIODEVSTATE.RADIODEVICES_ON);
}
Hope this helps!
Upvotes: 2
Reputation: 67198
You need to P/Invoke GetDeviceList
and ChangeRadioState
from ossvcs.dll
. The code to actually do this is a bit long for a SO post, so I'll leave it to you to get it worked out - it's not terribly hard (there's some C code here, and there's some C# code on CodeProject even, I've not used it so YMMV).
Another alternative is to use the Radios
class in the SDF, which already has these wrapped.
Upvotes: 1