RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Windows Mobile: Communicating with the Outside World : Page 4

Explore the various options available on the .NET Compact Framework that allow your apps to connect with the outside world.

A Sample Application Using Web Services
Besides the connectivity options above, you can also use web services for transferring data if you have WIFI or GPRS connectivity. In this section, you will learn how you can use web services to transfer binary data.

As you are probably aware, web services are inherently text-based as it uses XML as the messaging medium. Hence, if you want to transfer binary data using web services, you would need to "flatten" the binary data before you can send them over using XML text.

The project you will build in this section shows how you can use the built-in camera of your Windows Mobile device to take pictures and then upload the pictures to a web service.

Using Visual Studio 2008, create a new ASP.NET Web Service project and name it http://localhost/FilesUploadWS (see Figure 1).

Figure 1. Your Camera: Create a new ASP.NET Web Service project.

Import the following namespace:

using System.IO;
Define the UploadFile() web method so that users can upload the picture to the server (see Listing 5).

In the above method, the first parameter takes in a base64 encoded string (of the picture) and the second parameter is the filename of the picture. The base64 encoded string is first decoded into a byte array and then saved into the publishing directory of the web service. That's all you need to do for the web service.

You will now build the Windows Mobile application to take pictures and then send them over to the web service.

First, add a new Smart Device project to the current solution and name the project PictureTaker.

Next, add a reference to the Microsoft.WindowsMobile.Forms.dll DLL to your project. Then, add a PictureBox control to the form and create the two MenuItem controls as shown in Figure 2.

Figure 2. Form1: Populating the default Form1.

Switch to the code-behind of Form1.cs and import the following namespaces:

using System.IO;
using Microsoft.WindowsMobile.Forms;
Define the following member variables:

    public partial class Form1 : Form
        string _fileName = string.Empty;
        string _fileNameWithPath = string.Empty;
Define the ConvertToBase64() method so that you can convert a binary file into its base64 equivalent (see Listing 6).

To consume the web service project you have created earlier, add a web reference to the project. Be sure to use the IP address (or machine name) of the computer hosting the web service; specifying "localhost" will cause the application to fail during runtime. Name the web reference FilesUploadWS (see Figure 3).

Figure 3. FilesUploadWS: Adding a web reference to the project.

Double-click on the Take Picture MenuItem control to reveal its Click event handler. Code it as follows:

        private void mnuTakePicture_Click(object sender, EventArgs e)
            CameraCaptureDialog ccd = new CameraCaptureDialog()
                Resolution = new Size(100, 200),
                Mode = CameraCaptureMode.Still,
                InitialDirectory=@"\My Documents\My Pictures\"

            //---show the CameraCapture Dialog---
            if (ccd.FileName != string.Empty)
                //---save the filename---
                _fileName = ccd.FileName.Replace(ccd.InitialDirectory,"");
                _fileNameWithPath = ccd.FileName;

                //---load the image---
                pictureBox1.Image = new Bitmap(ccd.FileName);
You can use the CameraCaptureDialog class to activate the built-in camera of your Windows Mobile device and then display the picture using the PictureBox control.

To send the picture to the web service, double-click on the Upload Picture MenuItem control and code it as follows:

        private void mnuUploadPicture_Click(object sender, EventArgs e)
            FilesUploadWS.Service ws = new 
                //---upload to web service---
                ws.UploadFile(ConvertToBase64(_fileNameWithPath), _fileName);
            catch (Exception ex)
You need to encode the picture into base64 encoding first, then send it over to the web service using the UploadFile() web method.

That's it! To test the application, deploy the application to a Windows Mobile device. Ensure that the device has connectivity and is able to reach the web server hosting the web service. You can now take a picture using the camera and once a picture is taken you can send it to the web service. If you use the default web publishing directory C:\inetpub\wwwroot\ for your web service, the uploaded picture would be found from: C:\Inetpub\wwwroot\FilesUploadWS\.

Choose Wisely!
In this article, you have seen the various ways in which your Windows Mobile application can communicate with the outside world. Which technology you choose depends on the type of data and the devices with which you are trying to communicate:

  • If you need a quick and dirty way to exchange data with another Windows Mobile device, use infrared.
  • If you have some external devices (such as GPS receivers, RFID snap-ons, etc), use the serial port connection.
  • If you are on an expensive connection (such as GPRS where you pay by the amount of data you transfer), use sockets where you can send the data in the raw.
  • If you are on a free connection (such as WIFI), use web services for sending and retrieving data from servers. This is easier to develop and does not add much to your application's running cost.

Wei-Meng Lee is a Microsoft MVP and founder of Developer Learning Solutions, a technology company specializing in hands-on training on the latest Microsoft technologies. He is an established developer and trainer specializing in .NET and wireless technologies. Wei-Meng speaks regularly at international conferences and has authored and coauthored numerous books on .NET, XML, and wireless technologies. He writes extensively on topics ranging from .NET to Mac OS X. He is also the author of the .NET Compact Framework Pocket Guide, ASP.NET 2.0: A Developer's Notebook (both from O'Reilly Media, Inc.), and Programming Sudoku (Apress). Here is Wei-Meng's blog.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date