Device.GetRemoteAgent 方法

更新:2007 年 11 月

返回与所提供的数据包 ID 关联的 RemoteAgent

命名空间:  Microsoft.SmartDevice.Connectivity
程序集:  Microsoft.SmartDevice.Connectivity(在 Microsoft.SmartDevice.Connectivity.dll 中)

语法

声明
Public Function GetRemoteAgent ( _
    packageId As ObjectId _
) As RemoteAgent
用法
Dim instance As Device
Dim packageId As ObjectId
Dim returnValue As RemoteAgent

returnValue = instance.GetRemoteAgent(packageId)
public RemoteAgent GetRemoteAgent(
    ObjectId packageId
)
public:
RemoteAgent^ GetRemoteAgent(
    ObjectId^ packageId
)
public function GetRemoteAgent(
    packageId : ObjectId
) : RemoteAgent

参数

返回值

类型:Microsoft.SmartDevice.Connectivity.RemoteAgent

一个 RemoteAgent 类型的对象。

异常

异常 条件
DeviceNotConnectedException

未连接设备。

SmartDeviceException

基础 COM 组件引发了一个 COM 异常。

备注

必须连接该设备。

数据包确定在调用 RemoteAgent.Start 时将部署到设备并予以执行的文件。远程代理主要用于与设备代理交换流数据。为了实现此目的,远程代理会将一个设备代理可执行文件部署到设备上,并基于 packageid 参数指定的数据包执行该文件。当设备代理在设备上运行后,远程代理就可以创建 DevicePacketStream 以与设备代理交换 Packet 数据。有关更多信息,请参见加载项包概述

示例

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]
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;

    }
}

权限

另请参见

参考

Device 类

Device 成员

Microsoft.SmartDevice.Connectivity 命名空间