Building the Pocket PC Application
First, let's build the Pocket PC application that allows users to send an image to the desktop using infrared.
Using Visual Studio 2005, create a new Windows Mobile 5.0 Pocket PC application and name it Infrared. Populate the default Form1 with the following controls (see Figure 1):
|Figure 1. Form1: Populated with the various controls.|
Next, add a reference to the System.Net.IrDa library.
Switch to the code-behind of Form1 and import the following namespaces:
Define the following constants and member variable:
Public Class Form1
'---define the constants---
Const DATA_BLOCK_SIZE As Integer = 8192
Const MAX_TRIES As Integer = 3
Private ServiceName As String = "default"
constant is the maximum size of data exchanged, and MAX_TRIES
is the maximum number of tries to send a block of data before giving up. The ServiceName
variable is a unique identifier used by the two communicating parties participating in an infrared communication session.
Code the "Select image to send” button as follows:
Private Sub btnSelectImage_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.Filter = "JPEG files (*.jpg)|*.jpg"
If openFileDialog1.ShowDialog() = _
'---load the PictueBox control with the select image---
PictureBox1.Image = New Bitmap(openFileDialog1.FileName)
The above code basically displays the Open File dialog that allows users to select a .jpg
image. Once the image is selected, it is displayed in the PictureBox
Next, define the SendData() subroutine, which sends the image to the PC over an infrared connection (shown in Listing 1).
First, the SendData() subroutine tries to establish a connection with the other device (using the IrDAClient class) until the number of retries is exceeded. Once a connection is established, it writes to the other device using an IO Stream object. Finally, it closes the stream and the connection.
Define the UpdateStatus() subroutine and its delegate as follows:
Private Delegate Sub myDelegate(ByVal str As String)
Private Sub UpdateStatus(ByVal str As String)
'---delegate to update the statusbar control
StatusBar1.Text = str
Finally, code the Send
menu item as follows:
event handler first converts the image in the PictureBox
control into a byte array and then sends it to the PC by calling the SendData()
subroutine, defined earlier in Listing 1