This documentation is archived and is not being maintained.

Packet Class

Visual Studio 2008

Holds data and is used by the DevicePacketStream class to transfer data between a smart device and a desktop computer.

Namespace:  Microsoft.SmartDevice.Connectivity
Assembly:  Microsoft.SmartDevice.Connectivity (in Microsoft.SmartDevice.Connectivity.dll)

'Declaration
Public Class Packet
'Usage
Dim instance As Packet

The Packet class can only hold objects of type byte, byte[], string, int, and char. The Packet class is not extensible to read and write custom objects. However, users can still transfer custom objects across the stream by either splitting the custom data type into basic data types or serializing the custom data types into a byte array.

Imports System
Imports System.Collections.ObjectModel
Imports Microsoft.SmartDevice.Connectivity



Class [source]

    Shared Sub Main(ByVal args() As String) 
        ' Get the datastore object 
        Dim dsmgr As New DatastoreManager(1033)

        ' Get the platform object 
        Dim platform As Platform = GetPlatformByName("Windows Mobile 5.0 Smartphone SDK", dsmgr)

        ' Get the default device in the platform, usually an emulator. 
        Dim device As Device = platform.GetDevice(platform.GetDefaultDeviceId())

        device.Connect()

        If device.IsConnected() Then 
            ' Copy and start a device agent based on the ID of its add-on package. 
            Dim ra As RemoteAgent = device.GetRemoteAgent( _
                New ObjectId("CAF078AE-2E10-43e2-B566-C4577F2538C7"))
            ra.Start("command line argument")
            ' Open communication channel with device agent. 
            Dim ps As DevicePacketStream = ra.CreatePacketStream( _
                New ObjectId("2FAD740C-B5D3-4ad0-BE23-5682503584BF"))

            ' Create and write a packet of data. 
            Dim packet As Packet
            packet = New Packet()
            Dim i As Integer 
            For i = 0 To 3
                packet.WriteInt32(i)
            Next i
            packet.WriteString("Hello Smart Device")
            ps.Write(packet)
            ' While stream is connected, try to read a packet. 
            While ps.IsConnected()
                If ps.IsPacketAvailable() Then
                    packet = ps.Read()
                    While Not packet.IsEndOfPacket()
                        Select Case packet.ReadDataType()
                            Case DataType.BoolType
                                Dim boolValue As Boolean = packet.ReadBool()
                            Case DataType.ByteArrayType
                                Dim buffer As Byte() = packet.ReadBytes()
                            Case DataType.ByteType
                                Dim byteValue As Byte = packet.ReadByte()
                            Case DataType.CharType
                                Dim charValue As Char = packet.ReadChar()
                            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
                System.Threading.Thread.Sleep(1000)
            End While
            ps.Close()
            device.Disconnect()
            Console.ReadLine()
        End If 

    End Sub 'Main

    ' Returns a platform if the supplied name can be found in the datastore. 
    ' Returns null pointer if platform cannot be found 
    Private Shared 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
End Class '[source]

System.Object
  Microsoft.SmartDevice.Connectivity.Packet

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show: