Using the smart device framework for Windows Mobile Development

Source: Internet
Author: User

The. NET Compact framework's class libraries are sadly inadequate for developing full-featured Windows Mobile applications, sometimes forcing you to rely upon the problemtatic platform invoke. enter smart device framework (SDF), a group of extensions designed to pick up where the Compact framework's class libraries leave off.

By Wei-Meng Lee

December 4, 2007

 

If you have been developing Windows Mobile applications using. NET Compact framework for some time, there will be a day when you will realize that not all the functionalities you need can be satisfied by the class libraries provided by Microsoft. in such cases, you have to rely on platform invoke (commonly known as P/invoke ), which is a somewhat painful thing to do due to the need to perform data processing alling between your application and the operating system services. fortunately, a group of kind souls at opennetcf Consulting has released a set. NET Compact framework extensions known as the smart device framework (SDF) to complement the set of class libraries provided by Microsoft. all the hard work of calling the OS services are now wrapped and packaged so that managed developers can have directly access to them without having to resort to P/invoke.

Two versions of the SDF are available:

  • A community edition which has des all the compiled assemblies that you can use in your project free of charge.
  • The smart device framework 2.1 extensions for Visual Studio which integrates with Visual Studio 2005 and not only has des the compiled assemblies but also has des the source code for all assemblies.
This article will focus on the Community edition, which can be downloaded here.

Specifically, this article will show you some of the cool stuff that you can do using the SDF, like:

  • Recording sound
  • Vibrating your Windows Mobile Device
  • Retrieving SIM card information
  • Retrieving phone logs
  • Manipulating Images
  • Detecting the status of removable storage devices
  • Reading strings from text files
Recording soundThe ability to record sound is very useful in situations where you need to say, record a phone conversation. to use the sound recording capability in the SDF, add a reference to the opennetcf. dll library in your project. declare the following variables:

    '---for audio recording---    Private audioRecorder As OpenNETCF.Media.WaveAudio.Recorder    '---duration to record (in seconds)---    Const RECORD_DURATION As Integer = 10000To record voice, first open a file for voice recording and then use the RecordFor() method available in the Recorder class: 

 

        '---save the recording to storage---        Dim audioStream As IO.Stream        audioStream = System.IO.File.OpenWrite("/My Documents/Record1.wav")        audioRecorder = New OpenNETCF.Media.WaveAudio.Recorder        '---save the audio to stream---        audioRecorder.RecordFor( _           audioStream, _           RECORD_DURATION, _           OpenNETCF.Media.WaveAudio.SoundFormats.Mono16bit11kHz)

The opennetcf. media. waveaudio. soundformats enumerations contain the various sound qualities that you can use for recording. also, you can specify the duration of the recording via the second parameter (in seconds ). alternatively, to stop the recording prematurely simply call the stop () method:

        '---stop recording---        audioRecorder.Stop()

Once you have finished recording, you'll be able to find the record1.wav file stored in the/My Documents/folder (see figure 1 ).

 
Figure 1.Locating the sound file.Record1.wav:

Vibrating your Windows Mobile DeviceAlmost all devices sold in the market today supports silent alert (by spinning a miniature motor inside the device, thereby vibrating the phone ). unfortunately,. NET Compact framework does not provide any library for you to vibrate your phone.

For this purpose, you can use the opennetcf. windows Mobile. dll library. if you are using a Windows Mobile 6 Standard Device (. k. A smartphone), use the play () method in the vibrate class to vibrate your phone:

        OpenNETCF.WindowsMobile.Vibrate.Play()

To stop the vibration, use the stop () method:

        OpenNETCF.WindowsMobile.Vibrate.Stop()

Unfortunately, the above lines of code do not work for Pocket PC devices (now known as Windows Mobile 6 Professional and classic devices ). for Pocket PCs, you need to use the opennetcf. windowsCE. dll library. you will use the notification led to emulate vibration.

The following code shows how to cause your Pocket PC to vibrate for half a second:

        Dim vib As New OpenNETCF.WindowsCE.Notification.Led        '---start vibration---        vib.SetLedStatus(1, OpenNETCF.WindowsCE.Notification.Led.LedState.On)        System.Threading.Thread.Sleep(500)        '---stop vibration---        vib.SetLedStatus(1, _           OpenNETCF.WindowsCE.Notification.Led.LedState.Off)

The first parameter of the setledstatus () method takes in an integer representing the LED number. You may need to trial-and-error to find the number that works correctly on your device.Retrieving SIM card informationThe SIM card that you used for your windows mobile device can be used to store your contact's information. and sometimes you might need to programmatically retrieve them from the SIM card and perhaps back them up in a storage card. to do so, you can use the classes provided in the opennetcf. phone. dll library. the following code segment prints out the name and telephone numbers of all the contacts stored in the SIM card using the sim class:

        Dim mySim As New OpenNETCF.Phone.Sim.Sim        For i As Integer = 0 To mySim.Phonebook.Count - 1            ListBox1.Items.Add(mySim.Phonebook(i).Text & "-" & _               mySim.Phonebook(i).Address)        Next
 

Figure 2 shows the contacts stored in the SIM card:

 
Figure 2. Stored in a SIM card. Contacts:

You can also find out the maximum storage capacity of your SIM card using the code below:

        Dim str As String = String.Empty        str += "Max. Phonebook address length: " & _           mySim.MaximumPhonebookAddressLength & vbCrLf        str += "Max. Phonebook index: " & _           mySim.MaximumPhonebookIndex & vbCrLf        str += "Max. Phonebook text length: " & _           mySim.MaximumPhonebookTextLength & vbCrLf        str += "Min. Phonebook index: " & _           mySim.MinimumPhonebookIndex & vbCrLf        MsgBox(str)

Figure 3 shows the output for the SIM card.

 
Figure 3. The storage capacity of a SIM card. Output:

Retrieving Phone Logs
Every call that you make or receive is logged in your Windows Mobile device. But how do you get all this information out of the device? Well, you simply use the CallLog class, which is in the OpenNETCF.Phone.dll library, like this:

        Dim CallLog As New OpenNETCF.Phone.CallLog        For i As Integer = 0 To CallLog.Count - 1            Dim CallInfo As String            If CallLog(i).Outgoing Then                CallInfo = "Outgoing: "            Else                CallInfo = "Incoming: "            End If            CallInfo &= CallLog(i).Name & "(" & CallLog(i).Number & ")"            ListBox1.Items.Add(CallInfo)        Next

Besides retrieving the name of the caller (the phone tries to match the number with the contacts stored in the phone; if it is not found it will then compare with the contacts stored in the SIM card) and its number, you can also find out about the start time (including the date as well), end time, roaming (whether the call is a roaming call), and call type (whether the call was missed, answered or outgoing).

Manipulating Images
The .NET Compact Framework does not provide much functionality for image manipulations, so if you need to perform simple image processing, you are stuck. Fortunately, the OpenNETCF.Drawing.dll library provides some image utilities that you can use.

For example, you have a PictureBox control containing an image:

        PictureBox1.Image = New Bitmap( _           "/My Documents/My Pictures/Waterfall.jpg")

If you want to rotate the image 90 degrees clockwise, you can use the Rotate() method in the ImageUtils class, as follows:

        Dim RotatedBitmap As Bitmap = _           OpenNETCF.Drawing.Imaging.ImageUtils.Rotate(PictureBox1.Image, 90)        PictureBox1.Image = RotatedBitmap

What about image scaling? For example, you want to scale the image down to a quarter of its original size, you can half its width and height, and then use the CreateThumbnail() method to reduce the image:

        Dim fs As New System.IO.FileStream( _           "/My Documents/My Pictures/Waterfall.jpg", IO.FileMode.Open)        Dim NewSize As Size = PictureBox1.Size        With NewSize            .Width /= 2            .Height /= 2        End With        Dim bi As OpenNETCF.Drawing.Imaging.IBitmapImage        bi = OpenNETCF.Drawing.Imaging.ImageUtils.CreateThumbnail( _           fs, NewSize)        PictureBox1.Image = _           OpenNETCF.Drawing.Imaging.ImageUtils.IBitmapImageToBitmap(bi)
Manipulating ImagesMost Windows Mobile devices support external storage, such as Compact Flash, SD cards, etc. One common request is how do you detect whether the user has inserted (or removed) one? You can do so via the OpenNETCF.Net.dll library. 

First, declare a member variable of type DeviceStatusMonitor:

 

Dim WithEvents DiskMonitor As New OpenNETCF.Net.DeviceStatusMonitor( _ OpenNETCF.Net.DeviceStatusMonitor.FATFS_MOUNT_GUID, True)

 

Here, you indicate the type of file systems you want to monitor. Possible types of monitors are: 
    
    
  • BLOCK_DRIVER_GUID: This fires when the block driver for a Storage Manager device is loaded.

     

  • CDDA_MOUNT_GUID: This fires when a CDDA file system is loaded.

     

  • CDFS_MOUNT_GUID: This fires when a CDFS file system is loaded.

     

  • FATFS_MOUNT_GUID: This fires when a FAT file system is loaded for a device.

     

  • STORE_MOUNT_GUID: This fires when the store, managed by the Storage Manager, is ready.

     

  • UDFS_MOUNT_GUID: This fires when a UDFS file system is loaded.
The second parameter of the DeviceStatusMonitor class indicates if you should receive notifications for devices that are currently attached before you start the monitoring.

To start the monitoring, call the StartStatusMonitoring() method:

        DiskMonitor.StartStatusMonitoring()
Whenever a storage card is inserted into the device, the DeviceNotification event will fire. Hence, you just need to service this event, like this:
    Private Sub DiskMonitor_DeviceNotification( _       ByVal sender As Object, _       ByVal e As OpenNETCF.Net.DeviceNotificationArgs) _       Handles DiskMonitor.DeviceNotification        MsgBox("Device Name: " & e.DeviceName & ". Attached: " & _        e.DeviceAttached.ToString)    End Sub
Here, you can retrieve the name of the storage card as well as indicate whether it is attached (inserted) or not (removed). Figure 4 shows the prompt when you remove a SD card from the Windows Mobile device.

 
Figure 4. This shows when a SD card is removed from the Windows Mobile device. The Prompt:

Reading Strings from Text Files
One common task in the Windows Mobile development is reading contents from a text file into a string variable. Unlike the desktop version of the .NET framework, you cannot use the My namespace, like this:

'---does not work as the My namespace is not supported in .NET CF---fileContents = My.Computer.FileSystem.ReadAllText("C:/Test.txt") 
You need to use a StreamReader class to read from the file, like this (which is a little lengthy):
        Dim s As New System.IO.StreamReader("C:/Test.txt")        Dim fileContents As String = s.ReadToEnd()
To simplify this task, you can use the ReadAllText() method from the FileHelper class located in the OpenNetCF.dll library, like this:
        fileContents = OpenNETCF.IO.FileHelper.ReadAllText( _            "C:/Test.txt", System.Text.Encoding.ASCII)
Goodbye Platform Invokes!
Now you're familiar with some of the classes in the Smart Device Framework and how they help solve your programming problems for which you'd usually perform Platform Invokes. Why not download the framework today and explore the wealth of classes contained within it? You will be grateful you did!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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.