Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Pocket PC 2003 File transfer - The OBEX way

0.00/5 (No votes)
1 Feb 2004 4  
A sample application that pulls files from an OBEX server.

Introduction

OBEX is standard for infrared or Bluetooth communication, best suited for small devices, and it is supported on the PocketPC platforms. Why not use it for simple data synchronization tasks?

This sample application connects to the Obex-FTP service on a desktop (or server), retrieves the file list of a special folder in the servers Obex-FTP root, and then pulls the modified files to merge them with its own database.

In this way the server application can simply write new information into this dedicated folder, and the clients (our PocketPC app) can connect and synchronize from time to time. The server may be any device with a Obex-FTP server (a desktop with a bluetooth stick, another PocketPC, a Palm, or even a SmartPhone).

Background

An OBEX server publishes a list of "services" clients can connect to. The default and most basic of this services is the OBEX Inbox (or OBEX Object Push). This service, however, does normally not allow to pull (or GET) data other than some predefined default objects (like a vCard) from the server; and because of this it is not approriate for the purpose of this sample.

The OBEX FTP service however has all the functionally we need: get a list of files and folders, get, store, and delete files, and much more. All devices with an OBEX server that supports this services - whether they use infrared or Bluetooth communication - can act as a synchronization server for our client.

There are other OBEX services like IrMCSync or SYNCML or even custom OBEX extensions which could be used for synchronization (you can download the OBEX specification here) but none of them is as simple and versatile as the FTP approach.

Using the code

The class CObexFTP is a wrapper for the system supplied IObex object; use it detect appropriate devices in range and spawn instances of CObexFTPConnection which in turn encapsulates the IObexDevice interface and adds some useful methods for file transfer applications.

Typically you will derive your own class from CObexFTP and override the virtual function OnDeviceArrived to connect to this device and perform file transfer operations. Likewise you could override the functions OnDeviceRemoved and OnDeviceChanged, which will only be called if you have an active connection and will probably better be overridden in the corresponding CObexFTPConnection objects.

Once you have successfully initialized your instance (see CObexFTP::Initialize()) you can call the functions StartDeviceEnumeration() and StopDeviceEnumeration() to start/stop getting asynchrounous notifications through the above mentioned virtual functions.

Typically in your override of OnDeviceArrived you will create a CObexFTPConnection object (or a derived class) and call the Connect method. Since this methods keeps a pointer in a linked list you should not delete this object without calling Disconnect first!

The argument of OnDeviceArrived is a pointer to a standard IPropertyBag object; if you need more information about the device before accepting the connecting, the helper class CObexDeviceProperties may be of interest.

//
// Sample override of OnDevicedArrived
//
void CMyObex::OnDeviceArrived(IPropertyBag *Props)
{
  CObexDeviceProperties DevProps(Props);
  if (0==wcsicmp(DevProps.GetName(),L"ServerName"))
  {
    CMyObexConnection *pConn=new CMyObexConnection;
    if (pConn->Connect(this,Props))
    {
      // ... work with it
      pConn->Disconnect();
      delete pConn;
    }
  }
}

The sample application

The sample application uses WTL7.1 which has built-in CE-support (you can download it for free directly from Microsoft) because the OBEX classes use ATL anyway.

It simply connects to the first arriving FTP-server, changes to the folder "SampleFolder" and shows the name of all files that have changed since the last synchronization in a listbox.

If you want to play with it (e.g. with a desktop server running the WIDCOMM bluetooth stack), you have to create this folder in your servers FTP directory and put some files in it.

You will need embedded Visual C 4.2 and the PocketPC Platform SDK to compile the source; for the sample project file (PocketObex.vcp) you further need WTL7.1 installed and integrated into eVC.

A word of caution

I developed and tested this classes and the sample application on a T-Mobile MDA-II (which is also known as O2-XDA-II or similar). This seems to be the first device with a built-in Microsoft Bluetooth stack, most other vendors seem to use the WIDCOMM stack. I doubt that the Microsoft OBEX support properly integrates with the WIDCOMM stack, but I'd love to hear otherwise (:.

BTW: If you have a MDA-II (which is a great device!) and have wondered why there is no OBEX-FTP server on your device even after the latest firmware upgrade: There is no reason other than somebody (Microsoft?, HCT?, T-Mobile?,O2?) decided not to install it. If you simply compile the sample in the Microsoft WINCE-420 and put some registry values in, it works.

Open Issues

These classes currently do not care for authentication; if you have problems connecting to your server, make sure you switch off the "Secure connection" or similar for the FTP service on the server side.

If you would have to transfer larger amounts of data, some kind of progress indicator and a CANCEL button would be nice to have.

History

  • Jan 2004: First release.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here