Internet connection with Windows Mobile 5.0

No.of Views1467
Bookmarked0 times
Downloads 
Votes2
By  RRaveen   On  15 Feb 2010 22:02:46
Tag : Windows Mobile , How to
Internet connection with Windows Mobile 5.0
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction


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 free 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.

{codecitation class="brush: c#; gutter: true;" width="600px"}

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 = 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;
}

}
}

{/codecitation}

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

and i would like to say thanks to Jim wilson.I hope you like this articles and Please send you all comment to info@codgain.com.

Thank you

RRaveen


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
 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top