Tuesday, November 20, 2007

Internet connection with Windows Mobile 5.0

The windows mobile 5.0 SDK for pocket PC, had great dlls.
most popular one is the MicroSoft.Windows.Mobile.State.this is one have more than 100+ properties for Windows Mobile devices.You can download it's freely from here http://www.microsoft.com/downloads/details.aspx?familyid=83A52AF2-F524-4EC5-9155-717CBE5D25ED&displaylang=en

after installed SDK, create a new project with Windows Mobile 5.0 SDK Pocket PC, like this and give any name to your project.


before write code we want to add windows mobile dll to project as references, so select Reference menu in your Project and go to .net tab(default), there can u see 5 dll like start name Microsoft windows mobile... Please select M.W.M and M.W.M.State and give ok, after return to your code window.

Note(M.W.M -Microsoft.windows.mobile)

write code like following code snippet. or copy and paste and change namespace to your project name

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.Status;
using System.Net;
using System.IO;
using System.Diagnostics;

namespace InterNet
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

bool _remoteServerReachable = false;
bool _internetReachable = false;
bool _networkConnected = false;

private void Form1_Load(object sender, EventArgs e)
{
_networkWatcher = new SystemState(SystemProperty.ConnectionsCount, false);
_networkWatcher.Changed += new ChangeEventHandler(NetworkWatcher_Changed);

DetermineNetworkState(SystemState.ConnectionsCount);
DisplayConnectionInfo();
}


private void Form1_Closing(object sender, CancelEventArgs e)
{
// When creating an instance of the SystemState class that runs on a background thread,
// you must call the Dispose method to cause the background thread to exit.
if (_networkWatcher != null)
_networkWatcher.Dispose();
}

SystemState _networkWatcher = null;

void NetworkWatcher_Changed(object sender, ChangeEventArgs args)
{
int connectionsCount = (int)args.NewValue;
DetermineNetworkState(connectionsCount);

DisplayConnectionInfo();
}

static Uri _remoteServerUrl = new Uri(@"http://somebogusurlthatwontexist.com/SQLSync/sqlcesa30.dll");
// static Uri _remoteServerUrl = new Uri(@"http://192.168.1.102/SQLSync/sqlcesa30.dll");
const string _remoteServerExpectedText = @"SQL Server";
static Uri _internetReachableUrl = new Uri(@"http://mobile.live.com/search");
const string _internetReachableExpectedText = @"Live Search";
void DetermineNetworkState(int connectionsCount)
{
if (connectionsCount > 0)
{
_networkConnected = true;
_remoteServerReachable = IsUrlReachable(_remoteServerUrl, _remoteServerExpectedText);
_internetReachable = _remoteServerReachable ? true :
IsUrlReachable(_internetReachableUrl, _internetReachableExpectedText);
}
else
{
_networkConnected = false;
_internetReachable = false;
_remoteServerReachable = false;
}
}

void DisplayConnectionInfo()
{
if (InvokeRequired)
{
Invoke((MethodInvoker)DisplayConnectionInfo);
}
else
{
txtNetworkConnected.Text = _networkConnected.ToString();
txtRemoteServerReachable.Text = _remoteServerReachable.ToString();
txtInternetReachable.Text = _internetReachable.ToString();
}
}

#if PocketPC || Smartphone
// Required for the above Invoke call
// This delegate is already defined in the full .NET Framework
delegate void MethodInvoker();
#endif

const int _maxConnectTryCount = 2;
const int _sleepTimeBetweenConnectTries = 100; // milliseconds
bool IsUrlReachable(Uri url, string expectedText)
{
bool isUrlReachable = false;
HttpWebRequest httpRequest = null;
HttpWebResponse httpResponse = null;
Stream responseStream = null;
StreamReader responseReader = null;

for (int connectTryCount = 0;
!isUrlReachable && connectTryCount < httprequest =" (HttpWebRequest)WebRequest.Create(url);" method = "GET" httpresponse =" (HttpWebResponse)httpRequest.GetResponse();" responsestream =" httpResponse.GetResponseStream();" responsereader =" new" responsetext =" responseReader.ReadToEnd();" isurlreachable =" expectedText">= 0;
}
catch (SystemException ex)
{
// write log
#if DEBUG
Debug.WriteLine("");
Debug.WriteLine(string.Format("Try:{0} | Url:{1}",
connectTryCount.ToString(), url.ToString()));
Debug.WriteLine(ex.Message);
Debug.WriteLine("");
#endif
}
finally
{
if (responseReader != null)
responseReader.Close();
if (responseStream != null)
responseStream.Close();
if (httpResponse != null)
httpResponse.Close();
}
if (!isUrlReachable)
System.Threading.Thread.Sleep(_sleepTimeBetweenConnectTries);
}
return isUrlReachable;
}

}
}

from and video demo http://msdn2.microsoft.com/en-us/netframework/bb851561.aspx

and i would like to say thanks to Jim wilson.
I think you all are happy with WM 5.0 and WM 6.0

will next articles
-RRave

3 comments:

sophos said...

Hi !
I'm really interested in this DLL, but I'm less than zero in C# programming, so I'd like to compile it in Visual Studio 2005 and to insert in my mobile software.

Unfortunately, I'm encountering some troubles in these raws:
1)
catch (SystemException ex;
{

2)
finally

return isUrlReachable;
}

4)
for (int connectTryCount = 0;
!isUrlReachable && connectTryCount < httprequest =" (HttpWebRequest)WebRequest.Create(url);"; method="GET" )httpresponse =" (HttpWebResponse)httpRequest.GetResponse();"; responsestream =" httpResponse.GetResponseStream();"; responsereader =" new"; responsetext =" responseReader.ReadToEnd();"; isurlreachable =" expectedText">= 0;
}


5)
some ";" are missing somewhere.

Thank you very much.

sophos said...

You can find the code at the msdn page.

Unknown said...

Thanks for sharing such an interesting post with us. You have made some valuable points which are very useful for all readers Internet Connection Setup Melbourne