Export (0) Print
Expand All

Walkthrough: Exchanging Data between Desktop and Device Applications

Visual Studio 2008

In this walkthrough, you will create a device agent application, a desktop application, and an add-on package in the datastore. The desktop application will deploy and start the device agent, depending on information in the add-on package. The desktop application will then exchange packet data with the device agent application and terminate. You will complete the following tasks:

  • Create a managed device agent application.

  • Create an add-on package for the device agent application.

  • Create a desktop application that starts the package and communicates with the device agent.

NoteNote:

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

To create a device agent application

  1. Start Visual Studio.

  2. On the File menu, point to New, and then click Project.

    The New Project dialog box appears.

  3. (Visual C#) Expand Visual C#, and then click Smart Device. Name the project DeviceAgent and name the solution DataExchange. Click OK.

    (Visual Basic) Expand Visual Basic, and then click Smart Device. Name the project DeviceAgent and name the solution DataExchange. Click OK.

    The Add New Smart Device Project dialog box appears.

  4. Select Windows Mobile 5.0 Smartphone SDK for the target platform, click Console Application in the Templates pane, and then click OK.

    The new DeviceAgent project is displayed in Solution Explorer.

  5. (Visual C#) Right-click References in Solution Explorer and then click Add Reference.

    (Visual Basic) Right-click DeviceAgent in Solution Explorer and then click Add Reference.

    The Add Reference dialog box appears.

  6. Click the Browse tab and navigate to drive:\Program Files\Common Files\Microsoft Shared\CoreCon\1.0\Target\Lib. Click Microsoft.Smartdevice.DeviceAgentTransport.dll and then click OK.

    A reference to Microsoft.Smartdevice.DeviceAgentTransport is added to the project.

  7. Double click Program.cs (C#) or Module1.vb (Visual Basic) in Solution Explorer to open the file in the Code Editor if it is not already open.

  8. Position the cursor at the beginning of the file.

  9. Insert the following code:

    using Microsoft.SmartDevice.DeviceAgentTransport;
    

    Imports Microsoft.SmartDevice.DeviceAgentTransport
    
  10. Position the cursor inside the Main method.

  11. Insert the following code:

    string[] serviceids = {"A92866CA-AE83-4848-9438-501D8DB3CF25"};
    
    IDeviceAgentTransport transport = DeviceAgentTransportFactory.GetAgentTransport();
    
    // Don't keep it waiting.
    transport.AcknowledgeLaunch(1, serviceids);
    
    IDevicePacketStream packetstream;
    
    transport.AcceptConnectionEx(serviceids[0], out packetstream);
    IPacket packet;
    packet = PacketFactory.GetNewPacket();
    packet.WriteInt32(Environment.Version.Major);
    packet.WriteInt32(Environment.Version.Minor);
    packet.WriteInt32(Environment.Version.Build);
    packet.WriteInt32(Environment.Version.Revision);
    packetstream.Write(packet);
    

    Dim serviceids(0 To 0) As String
    serviceids(0) = "A92866CA-AE83-4848-9438-501D8DB3CF25"
    
    Dim transport As IDeviceAgentTransport = DeviceAgentTransportFactory.GetAgentTransport()
    
    ' Don't keep it waiting.
    transport.AcknowledgeLaunch(1, serviceids)
    
    Dim packetstream As IDevicePacketStream
    
    transport.AcceptConnectionEx(serviceids(0), packetstream)
    Dim packet As IPacket
    packet = PacketFactory.GetNewPacket()
    packet.WriteInt32(Environment.Version.Major)
    packet.WriteInt32(Environment.Version.Minor)
    packet.WriteInt32(Environment.Version.Build)
    packet.WriteInt32(Environment.Version.Revision)
    packetstream.Write(packet)
    

    The previous code creates an IDeviceAgentTransport object, and then accepts a connection from the desktop computer. The program sends a packet to the desktop computer that contains .NET Compact Framework version information.

  12. On the Build menu, click Build Solution.

    You now have a smart device application named DeviceAgent. You will later deploy this application to the device.

To create an add-on package in the datastore

  1. Start Notepad.

  2. Copy the following to the Notepad file:

    <?xml version="1.0" standalone="no"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:template match="/">
        <ADDONCONTAINER>
          <ADDON>
            <PACKAGECONTAINER>
              <PACKAGE ID="" NAME="DeviceAgent">
                <PROPERTYCONTAINER />
                <PACKAGETYPECONTAINER>
                  <PACKAGETYPE Name="ARMV4I" ID="ARMV4I" Protected="True">
                    <PROPERTYCONTAINER>
                      <PROPERTY ID="RemotePath" Protected="True">%CSIDL_PROGRAM_FILES%\DeviceAgent</PROPERTY>
                      <PROPERTY ID="RootPath" Protected="True">C:\DataExchange\DeviceAgent\bin\Debug</PROPERTY>
                      <PROPERTY ID="CPU" Protected="True">ARMV4I</PROPERTY>
                      <PROPERTY ID="Host" Protected="True">DeviceAgent</PROPERTY>
                    </PROPERTYCONTAINER>
                    <FILECONTAINER>
                      <FILE ID="DeviceAgent.exe" />
                      <FILE ID="Microsoft.Smartdevice.DeviceAgentTransport.dll" />
                      <FILE ID="DeviceAgentTransport.dll" />
                    </FILECONTAINER>
                  </PACKAGETYPE>
                </PACKAGETYPECONTAINER>
              </PACKAGE>
            </PACKAGECONTAINER>
          </ADDON>
        </ADDONCONTAINER>
      </xsl:template>
    </xsl:stylesheet>
    
  3. In Visual Studio, on the Tools menu, click Create GUID.

    The Create GUID dialog box appears.

  4. Select Registry Format, click Copy, and then click Exit.

  5. Paste the GUID into Notepad in the ID attribute of the Package tag and remove the braces that enclose the GUID.

    For example, <Package ID="9C50B38D-4259-40b3-AE9F-C5887DD898FF" Name="DeviceAgent">.

  6. Change the value of the <PROPERTY ID="RootPath" Protected="True"> tag to the folder where DeviceAgent.exe is located.

    For example, <PROPERTY ID="RootPath" Protected="True">c:\DataExchange\DeviceAgent\bin\debug</PROPERTY>.

    NoteNote:

    To determine the location of DeviceAgent.exe, right-click the DeviceAgent project in Solution Explorer and then click Properties. Click the Build tab and then click Browse next to Output path.

  7. Copy the drive:\Program Files\Common Files\Microsoft Shared\CoreCon\1.0\Target\wce400\armv4i\DeviceAgentTransport.dll library to the path specified under root path.

    NoteNote:

    This step is important because the Microsoft.Smartdevice.DeviceAgentTransport.dll assembly uses the DeviceAgentTransport.dll library. Both files must be deployed with our application.

  8. In Notepad, save the file.

    \ProgramData\Microsoft\corecon\1.0\addons\package.xsl (Windows Vista)

    \Documents and Settings\All Users\Application Data\Microsoft\corecon\1.0\addons\package.xsl (Other)

    NoteNote:

    If the file name package.xsl is already being used, use another name. The file name is not important because all files in the folder are processed as add-on packages.

  9. Exit and restart Visual Studio.

    Visual Studio loads the add-on package folder when it is started. If Visual Studio starts correctly, the package is free of syntax errors.

    The Datastore now contains an add-on package with the name DeviceAgent and an ID that is uniquely generated by Visual Studio. When the package is deployed, the Smart Device Connectivity API will copy three files from the output folder of the DeviceAgent project to the device's drive:\Program File\DeviceAgent directory. It will then execute DeviceAgent.exe on the device.

To create a desktop application that deploys the package and communicates with the device agent

  1. In Visual Studio, on the File menu, point to Open, and then click Project/Solution.

    The Open Project dialog box appears.

  2. Navigate to the DataExchange.sln solution, click it, and then click Open.

    Solution Explorer appears with the DeviceAgent application.

  3. On the File menu, point to Add, and then click New Project.

    The New Project dialog box appears.

  4. (Visual C#) Expand Visual C# and then click Windows.

    (Visual Basic) Expand Visual Basic and then click Windows.

  5. In the Templates pane, click Console Application.

  6. Name the project DesktopSide and then click OK.

    Solution Explorer contains two projects: DeviceAgent and DesktopSide.

  7. Right-click the DesktopSide project in Solution Explorer and then click Set as StartUp Project.

  8. (Visual C#) Right-click References in the DesktopSide project in Solution Explorer and then click Add Reference.

    (Visual Basic) Right-click DesktopSide in Solution Explorer and then click Add Reference.

    The Add Reference dialog box appears.

  9. Click the Browse tab, navigate to drive:\Program Files\Common Files\Microsoft Shared\CoreCon\1.0\Bin, click Microsoft.Smartdevice.Connectivity.dll, and then click OK.

    A reference to Microsoft.Smartdevice.Connectivity is added to the DesktopSide project.

  10. Double click Program.cs (C#) or Module1.vb (Visual Basic) in the DesktopSide project in Solution Explorer to open the file in the Code Editor if it is not already open.

  11. Position the cursor at the beginning of the file.

  12. Insert the following code:

    using Microsoft.SmartDevice.Connectivity;
    using System.Collections.ObjectModel;
    

    Imports Microsoft.SmartDevice.Connectivity
    Imports System.Collections.ObjectModel
    
  13. Position the cursor inside the Main method.

  14. Insert the following code:

    // Change the locale ID to correspond to your installation of Visual Studio.
    DatastoreManager dsmgr = new DatastoreManager(1033);
    Platform platform = GetPlatformByName("Windows Mobile 5.0 Smartphone SDK", dsmgr);
    Device emulator = platform.GetDevice(platform.GetDefaultDeviceId());
    emulator.Connect();
    // Add the GUID of your package below.
    RemoteAgent ra = emulator.GetRemoteAgent(new ObjectId(""));
    ra.Start("command line argument");
    DevicePacketStream ps = ra.CreatePacketStream(new ObjectId("A92866CA-AE83-4848-9438-501D8DB3CF25"));
    Packet packet;
    packet = new Packet();
    while (ps.IsConnected())
    {
        if (ps.IsPacketAvailable())
        {
            packet = ps.Read();
            while (!packet.IsEndOfPacket())
            {
                switch (packet.ReadDataType())
                {
                    case DataType.Int32Type:
                        Console.WriteLine("Int32Type:  " + packet.ReadInt32().ToString());
                        break;
                    case DataType.StringType:
                        Console.WriteLine("String:  " + packet.ReadString());
                        break;
                    default:
                        break;
                }
            }
            break;
        }
    }
    Console.Read();
    

    ' Change the locale ID to correspond to your installation of Visual Studio.
    Dim dsmgr As New DatastoreManager(1033)
    Dim platform As Platform = GetPlatformByName("Windows Mobile 5.0 Smartphone SDK", dsmgr)
    Dim emulator As Device = platform.GetDevice(platform.GetDefaultDeviceId())
    emulator.Connect()
    ' Add the GUID of your package below.
    Dim ra As RemoteAgent = emulator.GetRemoteAgent(New ObjectId(""))
    ra.Start("command line argument")
    Dim ps As DevicePacketStream = ra.CreatePacketStream(New ObjectId("A92866CA-AE83-4848-9438-501D8DB3CF25"))
    Dim packet As Packet
    packet = New Packet()
    While ps.IsConnected()
        If ps.IsPacketAvailable() Then
            packet = ps.Read()
            While Not packet.IsEndOfPacket()
                Select Case packet.ReadDataType()
                    Case DataType.Int32Type
                        Console.WriteLine("Int32Type:  " + packet.ReadInt32().ToString())
                    Case DataType.StringType
                        Console.WriteLine("String:  " + packet.ReadString())
                    Case Else
                End Select
            End While
            Exit While
        End If
    End While
    Console.Read()
    

    The previous code connects to a Windows Mobile 5.0 emulator and deploys the remote agent to the device. Then it establishes a connection with the device and reads packet data. The information read from the device is displayed in the console.

  15. Add ID of your package to emulator.GetRemoteAgent(new ObjectId("")).

    For example, emulator.GetRemoteAgent(new ObjectId("F85E57BA-5AE9-4FF7-8433-6AB7D991D034"))

  16. Paste the following helper method under the Main method:

    // Return a platform if the supplied name can be found in the datastore.  
    // Return null pointer if platform cannot be found.
    private static Platform GetPlatformByName(string p, DatastoreManager dsmgr)
    {
        // Get all platforms in the datastore.
        Collection<Platform> platforms = dsmgr.GetPlatforms();
    
        // Find the platform whose name matches the parameter.
        foreach (Platform platform in platforms)
        {
            if (platform.Name == p) return platform;
        }
        return null;
    }
    

    ' Return a platform if the supplied name can be found in the datastore.  
    ' Return null pointer if platform cannot be found.
    Private Function GetPlatformByName(ByVal p As String, ByVal dsmgr As DatastoreManager) As Platform 
        ' Get all platforms in the datastore.
        Dim platforms As Collection(Of Platform) = dsmgr.GetPlatforms()
            
        ' Find the platform whose name matches the parameter.
        Dim platform As Platform
        For Each platform In  platforms
            If platform.Name = p Then
                Return platform
            End If
        Next platform
        Return Nothing
        
    End Function 'GetPlatformByName
    
  17. Press F5 to debug the DesktopSide project.

    DesktopSide connects and starts the emulator if the emulator is not already started. Then it deploys the package and starts the DeviceAgent application. Then DesktopSide and DeviceAgent will exchange packet information and display it for the user.

Community Additions

ADD
Show:
© 2015 Microsoft