方法 : 赤外線ファイル転送を行う

[このドキュメントはプレビュー版であり、後のリリースで変更されることがあります。 空白のトピックは、プレースホルダーとして挿入されています。]

.NET Compact Framework は、デバイス間の赤外線通信用クラスを提供します。 この例で、赤外線通信を使用してデバイス間でファイルを送受信する方法を示します。 ファイルを送信する 1 つ、受信するもう 1 つの 2 つの Pocket PC 必要があります。

次の使用例は IrDAClient のインスタンスを作成し、その DiscoverDevices メソッド範囲内での赤外線デバイスの検出を使用しています。 このメソッドは、各デバイスに関する情報を提供する IrDADeviceInfo オブジェクトの配列を返します。

例示することができますし、ボタンが表示される、ファイルの送受信用のコードを次の使用例に示します。 最低でも、1 つのデバイス用の送信アプリケーションと、その他のデバイス用の受信アプリケーションを作成する必要です。

[送信] ボタンはファイルを送信する要求のリッスンされてがデバイスにのみファイルを送信します。 したがって、受信] する必要がありますはタップ受信デバイスの [送信] ボタンは送信側デバイスでタップする前にします。 次のタスクが実行すると。

  • 送信するファイルのストリームを取得します。

  • このアプリケーションの特定のサービス名を使用して IrDAClient インスタンスを作成します。 赤外線接続は、参加しているデバイスが同じ名前を参照している任意の値をでき、サービス名を指定して行われます。 サービス名が"IrDATest"次の例です。

  • IrDAClient ストリーム、ファイルを送信するファイルのストリームを読み込みます。

[受信] ボタン、送信側デバイスの IrDAListener インスタンスと同じサービス名前を持つデバイスを待機する IrDAClient インスタンスを作成します。

次のタスクが実行すると。

  • 転送内容マイ ドキュメント] フォルダー内の受信ファイルに書き込むためのストリームを作成します。

  • デバイス ID を持つ IrDAEndPoint インスタンスを作成し、送信側デバイスの名前のサービスします。

  • IrDAListener インスタンスから IrDAEndPoint インスタンスを作成し、リッスンしているサービスを開始します。

  • IrDAClient メソッドを使用して、 IrDAListener インスタンスから AcceptIrDAClient インスタンスを作成します。

  • 転送されたファイルのデータを含む、IrDAClient インスタンスの基になるストリームを読み込みます。

  • Receive.txt ファイルのストリームにそのデータ ストリームを書き込みます。

アプリケーションを作成するには

  1. 送信側デバイス用の Pocket PC アプリケーションを作成し、フォームにボタンを追加します。 送信ボタンを名前します。

  2. マイ ドキュメント] フォルダーに Send.txt という名前のファイル作成します。

  3. 次のコードを Click は、送信ボタンのイベントを追加します。

                                ' Align the infrared ports of the devices.
                                ' Click the Receive button first, then click Send.
                                Private
                                Sub SendButton_Click(sender AsObject, e As System.EventArgs) _
        Handles SendButton.Click
    
        Dim irClient AsNew IrDAClient()
        Dim irServiceName AsString = "IrDATest"Dim irDevices() As IrDADeviceInfo
        Dim buffersize AsInteger = 256
    
        ' Create a collection of devices to discover.
        irDevices = irClient.DiscoverDevices(2)
    
        ' Show the name of the first device found.If irDevices.Length = 0 Then
            MessageBox.Show("No remote infrared devices found.")
            ReturnEndIfTryDim irEndP AsNew IrDAEndPoint(irDevices(0).DeviceID, _
                irServiceName)
            Dim irListen AsNew IrDAListener(irEndP)
            irListen.Start()
            irClient = irListen.AcceptIrDAClient()
            MessageBox.Show("Connected!")
    
        Catch exSoc As SocketException
             MessageBox.Show("Couldn't listen on service " & irServiceName & ": " _
                & exSoc.ErrorCode)
        EndTry
        ' Open a file to send and get its stream.Dim fs As Stream
        Try
            fs = New FileStream(".\My Documents\send.txt", FileMode.Open)
        Catch exFile As Exception
            MessageBox.Show("Cannot open " & exFile.ToString())
            ReturnEndTry
        ' Get the underlying stream of the client.Dim baseStream As Stream = irClient.GetStream()
    
        ' Get the size of the file to send    ' and write its size to the stream.Dim length AsByte() = BitConverter.GetBytes(fs.Length)
        baseStream.Write(length, 0, length.Length)
    
        ' Create a buffer for reading the file.Dim buffer(buffersize) AsByteDim fileLength AsInteger = CInt(fs.Length)
    
        Try        ' Read the file stream into the base stream.While fileLength > 0
                Dim numRead As Int64 = fs.Read(buffer, 0, buffer.Length)
                baseStream.Write(buffer, 0, numRead)
                fileLength -= numRead
            EndWhile
            MessageBox.Show("File sent")
        Catch exSend As Exception
            MessageBox.Show(exSend.Message)
        EndTry
    
        fs.Close()
        baseStream.Close()
        irClient.Close()
    EndSub
    
                                // Align the infrared ports of the devices.
                                // Click the Receive button first, then click Send.
                                private
                                void SendButton_Click(object sender, System.EventArgs e)
        {
            IrDAClient irClient = new IrDAClient();
            string irServiceName = "IrDATest";
            IrDADeviceInfo[] irDevices;
            int buffersize = 256;
    
            // Create a collection of devices to discover.
            irDevices = irClient.DiscoverDevices(2);
    
            // Show the name of the first device found.if ((irDevices.Length == 0)) 
            {
                MessageBox.Show("No remote infrared devices found.");
                return;
            }
            try 
            {
                IrDAEndPoint irEndP = 
                    new IrDAEndPoint(irDevices[0].DeviceID, irServiceName);
                IrDAListener irListen = new IrDAListener(irEndP);
                irListen.Start();
                irClient = irListen.AcceptIrDAClient();
                MessageBox.Show("Connected!");
            }
            catch (SocketException exSoc) 
            {
                MessageBox.Show(("Couldn\'t listen on service "
                                + (irServiceName + (": " + exSoc.ErrorCode))));
            }
    
            // Open a file to send and get its stream.
            Stream fs;
            try 
            {
                fs = new FileStream(".\\My Documents\\send.txt", FileMode.Open);
            }
            catch (Exception exFile) 
            {
                MessageBox.Show(("Cannot open " + exFile.ToString()));
                return;
            }
    
            // Get the underlying stream of the client.
            Stream baseStream = irClient.GetStream();
    
            // Get the size of the file to send// and write its size to the stream.byte[] length = BitConverter.GetBytes(fs.Length);
            baseStream.Write(length, 0, length.Length);
    
            // Create a buffer for reading the file.byte[] buffer = newbyte[buffersize];
            int fileLength = (int) fs.Length;
            try 
            {
                // Read the file stream into the base stream.while ((fileLength > 0)) 
                {
                    Int64 numRead = fs.Read(buffer, 0, buffer.Length);
                    baseStream.Write(buffer, 0, Convert.ToInt32(numRead));
                    fileLength = (fileLength - Convert.ToInt32(numRead));
                }
                MessageBox.Show("File sent");
            }
            catch (Exception exSend) 
            {
                MessageBox.Show(exSend.Message);
            }
            fs.Close();
            baseStream.Close();
            irClient.Close();
        }
    
    
  4. 受信側デバイス用の Pocket PC アプリケーションを作成し、フォームにボタンを追加します。 受信の名前。

  5. 次のコードを Click は、受信ボタンのイベントを追加します。

                                ' Align the infrared ports of the devices.
                                ' Click the Receive button first, then click Send.
                                Private
                                Sub ReceiveButton_Click(sender AsObject, e As System.EventArgs) _
        Handles ReceiveButton.Click
    
        Dim irDevices() As IrDADeviceInfo
        Dim irClient AsNew IrDAClient()
        Dim irServiceName AsString = "IrDATest"Dim buffersize AsInteger = 256
    
        ' Create a collection for discovering up to    ' three devices, although only one is needed.
        irDevices = irClient.DiscoverDevices(2)
    
        ' Cancel if no devices are found.If irDevices.Length = 0 Then
            MessageBox.Show("No remote infrared devices found.")
            ReturnEndIf
        ' Connect to the first IrDA deviceDim irEndP AsNew IrDAEndPoint(irDevices(0).DeviceID, irServiceName)
        irClient.Connect(irEndP)
    
        ' Create a stream for writing a Pocket Word file.Dim writeStream As Stream
        Try
            writeStream = New FileStream(".\My Documents\receive.txt", _
                FileMode.OpenOrCreate)
        Catch
            MessageBox.Show("Cannot open file for writing.")
            ReturnEndTry
        ' Get the underlying stream of the client.Dim baseStream As Stream = irClient.GetStream()
    
        ' Create a buffer for reading the file.Dim buffer(buffersize) AsByteDim numToRead, numRead As Int64
    
        ' Read the file into a stream 8 bytes at a time.    ' Because the stream does not support seek operations,    ' its length cannot be determined.
        numToRead = 8
    
        TryWhile numToRead > 0
                numRead = baseStream.Read(buffer, 0, numToRead)
                numToRead -= numRead
            EndWhileCatch exReadIn As Exception
            MessageBox.Show("Read in: " & exReadIn.Message)
        EndTry
        ' Get the size of the buffer to show    ' the number of bytes to write to the file.
        numToRead = BitConverter.ToInt64(buffer, 0)
    
        Try        ' Write the stream to the file until        ' there are no more bytes to read.While numToRead > 0
                numRead = baseStream.Read(buffer, 0, buffer.Length)
                numToRead -= numRead
                writeStream.Write(buffer, 0, numRead)
            EndWhile
            writeStream.Close()
            MessageBox.Show("File received.")
        Catch exWriteOut As Exception
            MessageBox.Show("Write out: " & exWriteOut.Message)
        EndTry
    
         baseStream.Close()
         irClient.Close()
    EndSub
    
                                // Align the infrared ports of the devices.
                                // Click the Receive button first, then click Send.
                                private
                                void ReceiveButton_Click(object sender, System.EventArgs e)
    {
        IrDADeviceInfo[] irDevices;
        IrDAClient irClient = new IrDAClient();
        string irServiceName = "IrDATest";
        int buffersize = 256;
    
        // Create a collection for discovering up to// three devices, although only one is needed.
        irDevices = irClient.DiscoverDevices(2);
    
        // Cancel if no devices are found.if ((irDevices.Length == 0)) 
        {
            MessageBox.Show("No remote infrared devices found.");
            return;
        }
    
        // Connect to the first IrDA device
        IrDAEndPoint irEndP = 
            new IrDAEndPoint(irDevices[0].DeviceID, irServiceName);
        irClient.Connect(irEndP);
    
        // Create a stream for writing a Pocket Word file.
        Stream writeStream;
        try 
        {
            writeStream = new FileStream(".\\My Documents\\receive.txt", 
                FileMode.OpenOrCreate);
        }
        catch (Exception Ex)
        {
            MessageBox.Show("Cannot open file for writing. " + Ex.Message);
            return;
        }
    
        // Get the underlying stream of the client.
        Stream baseStream = irClient.GetStream();
    
        // Create a buffer for reading the file.byte[] buffer = newbyte[buffersize];
        Int64 numToRead;
        Int64 numRead;
    
        // Read the file into a stream 8 bytes at a time.// Because the stream does not support seek operations, its// length cannot be determined.
        numToRead = 8;
        try 
        {
            while ((numToRead > 0)) 
            {
                numRead = baseStream.Read(buffer, 0, 
                    Convert.ToInt32(numToRead));
                numToRead = (numToRead - numRead);
            }
        }
        catch (Exception exReadIn) {
            MessageBox.Show(("Read in: " + exReadIn.Message));
        }
    
        // Get the size of the buffer to show// the number of bytes to write to the file.
        numToRead = BitConverter.ToInt64(buffer, 0);
        try 
        {
            // Write the stream to the file until// there are no more bytes to read.while ((numToRead > 0)) {
                numRead = baseStream.Read(buffer, 0, buffer.Length);
                numToRead = (numToRead - numRead);
                writeStream.Write(buffer, 0, Convert.ToInt32(numRead));
            }
            writeStream.Close();
            MessageBox.Show("File received.");
        }
        catch (Exception exWriteOut) 
        {
            MessageBox.Show(("Write out: " + exWriteOut.Message));
        }
        baseStream.Close();
        irClient.Close();
    }
    

アプリケーションを実行するには

  1. デバイスにアプリケーションを展開し、開始します。

  2. デバイスの赤外線ポートの位置を調整します。

  3. 受信側デバイスで受信ボタンをタップします。

  4. 送信側デバイス上で送信ボタンをタップします。

  5. マイ ドキュメント] フォルダーに Receive.txt が作成されているかどうかを確認します。

コードのコンパイル方法

この例では、次の名前空間への参照が必要です。

参照

概念

赤外線接続

.NET コンパクトなフレームワーク方法を説明したトピックの検索