Search

Thursday, January 7, 2010

Windows Mobile .NETCF Vibrate alert implementation

i was recently searching for way to vibrate mobile device through c# code in windows mobile professional and i found a clean and interesting code using win32 dlls in msdn by heliosdev so though of re-posting it here as the collection of sample codes in my blog and as the reference for future


using System;
using System.Runtime.InteropServices;
using System.Threading;

namespace Util
{
public class LED
{
class NLED_SETTINGS_INFO
{
public uint LedNum;
public uint OffOnBlink;
public int TotalCycleTime;
public int OnTime;
public int OffTime;
public int MetaCycleOn;
public int MetaCycleOff;
}

class NLED_COUNT_INFO
{
public int cLeds;
}

const int NLED_COUNT_INFO_ID = 0;
const int NLED_SETTINGS_INFO_ID = 2;

public enum Status
{
OFF = 0,
ON,
BLINK
}

[DllImport("coredll.dll")]
private static extern bool NLedGetDeviceInfo(uint nID, NLED_COUNT_INFO pOutput);

[DllImport("coredll.dll")]
private static extern bool NLedSetDevice(uint nID, NLED_SETTINGS_INFO pOutput);

private int _ledCount;

public LED()
{
_ledCount = GetLedCount();
}

public void SetLedStatus(Status status)
{
NLED_SETTINGS_INFO nsi = new NLED_SETTINGS_INFO();
nsi.OffOnBlink = (uint)status;
for (int i = 0; i < _ledCount; i++)
{
nsi.LedNum = (uint)i;
NLedSetDevice(NLED_SETTINGS_INFO_ID, nsi);
}
}

public void Vibrate(int millisecondsTimeout)
{
SetLedStatus(Status.ON);
Thread.Sleep(millisecondsTimeout);
SetLedStatus(Status.OFF);
}

private int GetLedCount()
{
int count = 0;
NLED_COUNT_INFO nci = new NLED_COUNT_INFO();
if (NLedGetDeviceInfo(NLED_COUNT_INFO_ID, nci))
{
count = nci.cLeds;
}
return count;
}
}
}

Just call vibrate(1000) from any function i made it to vibrate for a second though i did not found any managed code to do it, above code is working like a charm without any issues if there is any managed API for vibrate do let us know.

0 comments:

Post a Comment

Other Interesting Articles



Related Article Widget by Yogith

Search Powered by Google