Export (0) Print
Expand All

Microsoft.SmartDevice.DeviceAgentTransport Namespace

Visual Studio 2008

This namespace contains types that make up the device side of the Smart Device Connectivity API. Managed smart device applications that reference and use this namespace are called device agent applications. Desktop applications that use the Smart Device Connectivity API can deploy device agents to the device and communicate with them by exchanging packet data. By using this library, you can programmatically exchange data between device and desktop while letting the Smart Device Connectivity API handle device interconnectivity and transport mechanism details.

To use Microsoft.SmartDevice.DeviceAgentTransport in your smart device project, complete the following tasks:

  • In your smart device project, add a reference to the Microsoft.Smartdevice.DeviceAgentTransport.dll assembly (drive:\Program Files\Common Files\Microsoft Shared\CoreCon\1.0\Target\Microsoft.Smartdevice.DeviceAgentTransport.dll).

  • Deploy both the native DeviceAgentTransport.dll library (drive:\Program Files\Common Files\Microsoft Shared\CoreCon\1.0\Target\wce400\cpu\DeviceAgentTransport.dll) and managed Microsoft.Smartdevice.DeviceAgentTransport.dll assembly to the smart device.

For more information, see Controlling Devices with the Smart Device Connectivity API and Walkthrough: Exchanging Data between Desktop and Device Applications.

NoteNote:

To create a device agent using Visual C++, use Unmanaged Device-Side Smart Device Connectivity API.

  ClassDescription
Public classDeviceAgentTransportFactoryUsed to get objects that implement IDeviceAgentTransport.
Public classInteropUtilsContains utility functions to aid interoperability.
Public classPacketFactoryUsed to get objects that implement IPacket.

  InterfaceDescription
Public interfaceIAgentTransportShutdownCallbackDefines the methods that a shutdown callback object must implement.
Public interfaceIDeviceAgentTransportUsed to communicate with a RemoteAgent on the desktop computer.
Public interfaceIDevicePacketStreamExchanges data with a connected DevicePacketStream on the desktop computer by reading and writing objects that implement IPacket.
Public interfaceIPacketHolds data and is used by objects that implement IDevicePacketStream to transfer data between a device agent application and a desktop computer.

  EnumerationDescription
Public enumerationDataTypeIndicates the type of data in the Microsoft.SmartDevice.DeviceAgentTransport.IPacket.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SmartDevice.DeviceAgentTransport;
using System.Windows.Forms;

class Program
{
    [MTAThread]
    static void Main(string[] args)
    {
        string[] serviceids = { "2FAD740C-B5D3-4ad0-BE23-5682503584BF" };

        // Get an instance of Device Agent Transport
        IDeviceAgentTransport transport = DeviceAgentTransportFactory.GetAgentTransport();

        // Register the callback object with the Device Agent Transport.
        ShutdownCallback shutdownCallback = new ShutdownCallback();
        transport.RegisterShutdownCallback(shutdownCallback, shutdownCallback);

        // Let the desktop application know that this device agent was deployed successfully  
        // and will handle the supplied list of service IDs.
        transport.AcknowledgeLaunch(Convert.ToUInt32(serviceids.Length), serviceids);

        // Open a communcation stream with desktop application on the service.
        IDevicePacketStream packetStream;
        transport.AcceptConnectionEx(serviceids[0], out packetStream);

        IPacket packet;

        // Check for a packet while communication stream is connected. 
        while (packetStream.IsConnected())
        {
            // If a packet is found, display the string and integer data. 
            if (packetStream.IsPacketAvailable())
            {
                packetStream.Read(out packet);
                StringBuilder sb = new StringBuilder();
                while (!packet.IsEndOfPacket())
                {
                    switch (packet.ReadDataType())
                    {
                        case DataType.BoolType:
                            bool boolValue = packet.ReadBool();
                            break;
                        case DataType.ByteArrayType:
                            // Read bytes and convert IntPtr to byte[]
                            IntPtr ptr;
                            uint size = 0;
                            packet.ReadBytes(out ptr, out size);
                            byte[] buffer = InteropUtils.ConvertIntPtrToByteArray(ptr, 
                                Convert.ToInt32(size));
                            break;
                        case DataType.ByteType:
                            byte byteValue = packet.ReadByte();
                            break;
                        case DataType.CharType:
                            char charValue = packet.ReadChar();
                            break;
                        case DataType.Int32Type:
                            sb.Append("Int32Type:  " + packet.ReadInt32().ToString() + "\r\n");
                            break;
                        case DataType.StringType:
                            sb.Append("String:  " + packet.ReadString() + "\r\n");
                            break;
                        default:
                            break;
                    }
                }
                MessageBox.Show(sb.ToString());
                break;
            }
            System.Threading.Thread.Sleep(1000);
        }

        packet = PacketFactory.GetNewPacket();

        // Write the version of .NET Compact Framework into the packet.
        packet.WriteString("Hello Desktop Computer");
        packet.WriteInt32(Environment.Version.Major);
        packet.WriteInt32(Environment.Version.Minor);
        packet.WriteInt32(Environment.Version.Build);
        packet.WriteInt32(Environment.Version.Revision);

        // Pass the packet to desktop application.
        packetStream.Write(packet);
    }
}

// Define your own shutdown implementation 
class ShutdownCallback : IAgentTransportShutdownCallback
{
    public void Shutdown(object in_pUnknown)
    {
        // Insert cleanup code here
    }
}

Community Additions

ADD
Show:
© 2014 Microsoft