Monday, October 20, 2008

How to control bluetooth OBEX default service on/off?

To control the Infrared state (on/off) or bluetooth OBEX default service in a similar way as in the control pannel, and to get the current state of the infrared module use the code below:

Code:

//set irda service status
HRESULT OBEXIOCTL(DWORD dwIOCTL)
{
HANDLE hService;
BOOL fRet;
hService = CreateFile(TEXT("OBX0:"),GENERIC_READ|GENERIC_WRITE,0,
NULL,OPEN_EXISTING,0,NULL);
if (INVALID_HANDLE_VALUE == hService)
{
CloseHandle(hService);
return FALSE;
}
fRet = DeviceIoControl(hService,dwIOCTL,0,0,0,0,NULL,0);
CloseHandle(hService);
return (0 == fRet)?E_FAIL:S_OK;
}

//query irda service status
DWORD OBEXQuery()
{
HANDLE hService;
BOOL fRet;
DWORD dwStatus;
hService = CreateFile(TEXT("OBX0:"),GENERIC_READ|GENERIC_WRITE,0,
NULL,OPEN_EXISTING,0,NULL);
if (INVALID_HANDLE_VALUE == hService)
{
CloseHandle(hService);
return FALSE;
}
fRet = DeviceIoControl(hService,IOCTL_SERVICE_STATUS,0,0,&dwStatus,sizeof(DWORD),0, 0);
CloseHandle(hService);
if(0 == fRet)
return 0xFFFFFFFF;
else
return dwStatus;
}

bool GetIRDAState()
{
switch(OBEXQuery())
{
case SERVICE_STATE_SHUTTING_DOWN:
case SERVICE_STATE_UNLOADING:
case SERVICE_STATE_UNINITIALIZED:
case SERVICE_STATE_OFF:
return 0;
break;

case SERVICE_STATE_STARTING_UP:
case SERVICE_STATE_ON:
return 1;
break;
default:
break;
};
return 0;
}

Friday, October 10, 2008

C# TrayIcon Class

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Win32;
using System.Windows.Forms;

namespace XXXX
{
public class TrayIcon
{
//Declare click event
public event System.EventHandler Click;

private WindowSink windowSink;
private int uID = 11000;

//Constructor
public TrayIcon()
{
//Create instance of the MessageWindow subclass
windowSink = new WindowSink(this);
windowSink.uID = uID;
}

//Destructor
~TrayIcon()
{
Remove();
}


public void Add(IntPtr hIcon)
{
TrayMessage(windowSink.Hwnd, NIM_ADD, (uint)uID, hIcon);
}

public void Remove()
{

TrayMessage(windowSink.Hwnd, NIM_DELETE, (uint)uID, IntPtr.Zero);
}

public void Modify(IntPtr hIcon)
{

TrayMessage(windowSink.Hwnd, NIM_MODIFY, (uint)uID, hIcon);

}

private void TrayMessage(IntPtr hwnd, int dwMessage, uint uID, IntPtr hIcon)
{
NOTIFYICONDATA notdata = new NOTIFYICONDATA();

notdata.cbSize = 152;
notdata.hIcon = hIcon;
notdata.hWnd = hwnd;
notdata.uCallbackMessage = WM_NOTIFY_TRAY;
notdata.uFlags = NIF_MESSAGE | NIF_ICON;
notdata.uID = uID;

int ret = Shell_NotifyIcon(dwMessage, ref notdata);
}

#region API Declarations

internal const int WM_LBUTTONDOWN = 0x0201;
internal const int WM_RBUTTONUP = 0x0205;
//User defined message
internal const int WM_NOTIFY_TRAY = 0x0400 + 2001;

internal const int NIM_ADD = 0x00000000;
internal const int NIM_MODIFY = 0x00000001;
internal const int NIM_DELETE = 0x00000002;

const int NIF_MESSAGE = 0x00000001;
const int NIF_ICON = 0x00000002;


internal struct NOTIFYICONDATA
{
internal int cbSize;
internal IntPtr hWnd;
internal uint uID;
internal uint uFlags;
internal uint uCallbackMessage;
internal IntPtr hIcon;
//internal char[] szTip = new char[64];
//internal IntPtr szTip;
}

[DllImport("coredll.dll")]
internal static extern int Shell_NotifyIcon(
int dwMessage, ref NOTIFYICONDATA pnid);

[DllImport("coredll.dll")]
internal static extern int SetForegroundWindow(IntPtr hWnd);

[DllImport("coredll.dll")]
internal static extern int ShowWindow(
IntPtr hWnd,
int nCmdShow);

[DllImport("coredll.dll")]
internal static extern IntPtr GetFocus();

#endregion


#region WindowSink

internal class WindowSink : Microsoft.WindowsCE.Forms.MessageWindow
{
//Private members
private int m_uID = 0;
private TrayIcon trayIcon;

//Constructor
public WindowSink(TrayIcon tIcon)
{
trayIcon = tIcon;
}

public int uID
{
set
{
m_uID = value;

}
}

protected override void WndProc(ref Microsoft.WindowsCE.Forms.Message msg)
{

if (msg.Msg == WM_NOTIFY_TRAY)
{
switch ((int)msg.LParam)
{
case WM_LBUTTONDOWN:
if ((int)msg.WParam == m_uID)
{
//If somebody hooked, raise the event
if (trayIcon.Click != null)
trayIcon.Click(trayIcon, null);

}
break;

case WM_RBUTTONUP:
{
MessageBox.Show("R Button Up");
}
break;
}
}

}
}
#endregion

}
}

Tuesday, October 7, 2008

How to set device clock (set system time)?

http://msdn.microsoft.com/en-us/library/ms172517.aspx

To get or set the system time of the device, use platform invoke to call the native GetSystemTime or SetSystemTime functions.

Note that the GetSystemTime function returns Coordinated Universal Time (UTC, also known as Greenwich Mean Time). To get your local time, you must add or subtract the number of hours between your time zone and UTC. For example, 24:00 (midnight) in UTC is 19:00 in New York--an offset of minus 5 hours (UTC–5).

To determine the UTC offset for your time zone, see the Time Zone tab of Date and Time Properties.

Some device emulators do not initially set daylight-saving time correctly, which could affect your result.

Visual Basic

Public Structure SYSTEMTIME
Public wYear As UInt16
Public wMonth As UInt16
Public wDayOfWeek As UInt16
Public wDay As UInt16
Public wHour As UInt16
Public wMinute As UInt16
Public wSecond As UInt16
Public wMilliseconds As UInt16
End Structure

Declare Function GetSystemTime Lib "CoreDll.dll" _
(ByRef lpSystemTime As SYSTEMTIME) As UInt32

Declare Function SetSystemTime Lib "CoreDll.dll" _
(ByRef lpSystemTime As SYSTEMTIME) As UInt32

Public Sub GetTime
' Call the native GetSystemTime method
' with the defined structure.
Dim st As New SYSTEMTIME
GetSystemTime(st)

' Show the current time.
MessageBox.Show("Current Time: " & st.wHour.ToString() _
& ":" & st.wMinute.ToString())
End Sub

Public Sub SetTime
' Call the native GetSystemTime method
' with the defined structure.
Dim st As New SYSTEMTIME
GetSystemTime(st)

' Set the system clock ahead one hour.
st.wHour = Convert.ToUInt16(((CInt(st.wHour) + 1)) Mod 24)
SetSystemTime(st)

End Sub


C#

[DllImport("coredll.dll")]
private extern static void GetSystemTime(ref SYSTEMTIME lpSystemTime);

[DllImport("coredll.dll")]
private extern static uint SetSystemTime(ref SYSTEMTIME lpSystemTime);


private struct SYSTEMTIME
{
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
}

private void GetTime()
{
// Call the native GetSystemTime method
// with the defined structure.
SYSTEMTIME stime = new SYSTEMTIME();
GetSystemTime(ref stime);

// Show the current time.
MessageBox.Show("Current Time: " +
stime.wHour.ToString() + ":"
+ stime.wMinute.ToString());
}
private void SetTime()
{
// Call the native GetSystemTime method
// with the defined structure.
SYSTEMTIME systime = new SYSTEMTIME();
GetSystemTime(ref systime);

// Set the system clock ahead one hour.
systime.wHour = (ushort)(systime.wHour + 1 % 24);
SetSystemTime(ref systime);
MessageBox.Show("New time: " + systime.wHour.ToString() + ":"
+ systime.wMinute.ToString());
}