DevicePacketStream Class
Visual Studio 2008
Exchanges data between a desktop application and a device agent application by reading and writing objects of type Packet.
Assembly: Microsoft.SmartDevice.Connectivity (in Microsoft.SmartDevice.Connectivity.dll)
This class does not have any constructors. To get an instance, connect to a Device, create and start a RemoteAgent , and then call CreatePacketStream.
using System; using System.Collections.ObjectModel; using Microsoft.SmartDevice.Connectivity; class source { static void Main(string[] args) { // Get the datastore object DatastoreManager dsmgr = new DatastoreManager(1033); // Get the platform object Platform platform = GetPlatformByName("Windows Mobile 5.0 Smartphone SDK", dsmgr); // Get the default device in the platform, usually an emulator. Device device = platform.GetDevice(platform.GetDefaultDeviceId()); device.Connect(); if (device.IsConnected()) { // Copy and start a device agent based on the ID of its add-on package. RemoteAgent ra = device.GetRemoteAgent( new ObjectId("CAF078AE-2E10-43e2-B566-C4577F2538C8")); ra.Start("command line argument"); // Open communication channel with device agent. DevicePacketStream ps = ra.CreatePacketStream( new ObjectId("2FAD740C-B5D3-4ad0-BE23-5682503584BF")); // Create and write a packet of data. Packet packet; packet = new Packet(); for (int i = 0; i < 4; i ++) packet.WriteInt32(i); packet.WriteString("Hello Smart Device"); ps.Write(packet); // While stream is connected, try to read a packet. while (ps.IsConnected()) { if (ps.IsPacketAvailable()) { packet = ps.Read(); while (!packet.IsEndOfPacket()) { switch (packet.ReadDataType()) { case DataType.BoolType: bool boolValue = packet.ReadBool(); break; case DataType.ByteArrayType: byte[] buffer = packet.ReadBytes(); break; case DataType.ByteType: byte byteValue = packet.ReadByte(); break; case DataType.CharType: char charValue = packet.ReadChar(); break; case DataType.Int32Type: Console.WriteLine("Int32Type: " + packet.ReadInt32().ToString()); break; case DataType.StringType: Console.WriteLine("String: " + packet.ReadString()); break; default: break; } } break; } System.Threading.Thread.Sleep(1000); } ps.Close(); device.Disconnect(); Console.ReadLine(); } } // Returns a platform if the supplied name can be found in the datastore. // Returns 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; } }