dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Programming Windows Mobile 5.0 Applications Using the .NET Compact Framework : Page 4

Visual Studio 2005 is the premier development platform for Windows Mobile 5.0. This article explores its new capabilities in detail.


advertisement
Camera APIs
In the past, integrating your application with the built-in camera on your device was not an easy task. In Windows Mobile 5, you can now invoke the built-in camera using the CameraCaptureDialog class. (This example requires you to add a reference to the Microsoft.WindowsMobile.Forms assembly.)

 
Figure 10: Image captured using the built-in camera
   Imports Microsoft.WindowsMobile.Forms
   ...
   Private Sub btnPicture_Click( _
      ByVal sender As System.Object, _
      ByVal e As System.EventArgs) _
      Handles btnPicture.Click
      Dim ccd As New CameraCaptureDialog
      ccd.Resolution = New Size(100, 200)
      ccd.Mode = CameraCaptureMode.Still 
      ccd.ShowDialog()
      If ccd.FileName <> String.Empty Then
         PictureBox1.Image = New Bitmap(ccd.FileName)
      End If
   End Sub
Besides capturing still images, CameraCaptureDialog also supports video capture, which you can enable by setting the Mode property to CameraCaptureMode.VideoWithAudio.

Figure 10 shows a PictureBox control showing the picture that was taken using the built-in camera.

Serial Communication
 
Figure 11: A chat application using the SerialPort class.
In .NET Compact Framework 1.1, accessing the serial ports on the device involves performing a Platform Invoke (P/Invoke) operation, which is often an arduous and error-prone process. With .NET Compact Framework 2.0, you can access serial ports using the SerialPort class.

Figure 11 shows a Chat application that allows two Pocket PC users to chat over Bluetooth.

You can create it like this. First, create an instance of the SerialPort class.

   Dim WithEvents serialPort As New IO.Ports.SerialPort
When the form is loaded, open the serial port (this example assumes that your Pocket PC has an available COM4 associated with the Bluetooth connection) and initialize the SerialPort object.

   Private Sub Form6_Load( _
      ByVal sender As System.Object, _
      ByVal e As System.EventArgs) _
      Handles MyBase.Load
      If serialPort.IsOpen Then
         serialPort.Close()
      End If
      With serialPort
         .PortName = "COM4"
         .BaudRate = 9600
         .Parity = IO.Ports.Parity.None
         .DataBits = 8
         .StopBits = IO.Ports.StopBits.One
      End With
      serialPort.Open()
   End Sub
The SerialPort object has an event—DataReceived—that fires whenever incoming data is detected. When data is received, you call a delegate to update the received data in the TextBox control on the form. Note, you cannot directly update the Windows controls in this event as they are not running in the same thread as the main form.

   Private Sub DataReceived( _
      ByVal sender As Object, _
      ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
      Handles serialPort.DataReceived
      txtReceivedMessage. _
      BeginInvoke(New myDelegate(
         AddressOf updateTextBox), _
         New Object() {})
   End Sub
Define the delegate and the actual subroutine to update the received data on the form. The ReadExisting() method obtains the received data from the SerialPort object.

   Public Delegate Sub myDelegate()
   Public Sub updateTextBox()
   txtReceivedMessage.Text = serialPort.ReadExisting & _
      vbCrLf & txtReceivedMessage.Text
   End Sub
To send an outgoing message, use the WriteLine() method from the SerialPort object.

   Private Sub MenuItem1_Click( _
      ByVal sender As System.Object, _
      ByVal e As System.EventArgs) _
      Handles MenuItem1.Click
      serialPort.WriteLine(txtMessageToSend.Text)
      txtReceivedMessage.Text = ">" & txtMessageToSend.Text & _
          vbCrLf & txtReceivedMessage.Text
      txtMessageToSend.Text = String.Empty
   End Sub
 
Figure 12: Making a phone call.
To test the application, load two Windows Mobile 5.0 Pocket PCs with the application. Pair up the two devices with Bluetooth and ensure that each device has an available serial port (which I hard-coded as COM4 for simplicity). You can now chat wirelessly with each other using Bluetooth!

Telephony
For Windows Mobile 5.0 Phone Edition devices, you can integrate telephony functionality into your applications using the Phone class (located in the Microsoft.WindowsMobile.Telephony assembly).

For example, the following code segment makes a phone call by first prompting the user (see Figure 12) (the example requires you to add a reference to the Microsoft.WindowsMobile.Telephony assembly).

   Imports Microsoft.WindowsMobile.Telephony
   ...
   Dim phone As New Phone
   phone.Talk("+6512345678", True)


Thanks for your registration, follow us on our social networks to keep up-to-date