HOW TO:進行紅外線檔案傳輸
更新:2007 年 11 月
.NET Compact Framework 會提供裝置間進行紅外線通訊所用的類別。這個範例將示範如何使用紅外線通訊,在裝置間傳送和接收檔案。您需要兩部 Pocket PC,一部用來傳送檔案,一部則用來接收檔案。
這個範例會建立 IrDAClient 的執行個體,並使用其 DiscoverDevices 方法,來探索範圍內的紅外線裝置。這個方法會傳回 IrDADeviceInfo 物件陣列,這些物件提供每一個裝置的相關資訊。
這個範例會提供傳送和接收檔案的程式碼,您可以利用 [傳送] 和 [接收] 按鈕,來示範如何傳送和接收檔案。您最少需要為其中一個裝置建立傳送應用程式,並為另一個裝置建立接收應用程式。
[傳送] 按鈕只會將檔案傳送至已在接聽是否有傳送檔案要求的裝置。因此,必須在傳送裝置上點選 [傳送] 按鈕之前,在接收裝置上點選 [接收] 按鈕。執行下列工作:
取得要傳送之檔案的資料流。
使用為這個應用程式所決定的服務名稱來建立 IrDAClient 執行個體。指定服務名稱來建立紅外線連線,這個服務名稱可以是任何值,前提是參與的裝置必須參照到相同名稱。在這個範例中,服務名稱是 "IrDATest"。
將檔案的資料流讀入傳送檔案的 IrDAClient 資料流。
[接收] 按鈕會建立 IrDAListener 執行個體,以接聽是否有裝置具有與傳送裝置上 IrDAClient 執行個體一樣的服務名稱。
執行下列工作:
建立資料流,將已傳輸的內容寫入至 [我的文件] 資料夾中的接收檔案。
利用傳送裝置的裝置 ID 和服務名稱,來建立 IrDAEndPoint 執行個體。
從 IrDAEndPoint 執行個體建立 IrDAListener 執行個體,然後啟動接聽服務。
使用 AcceptIrDAClient 方法,從 IrDAListener 執行個體建立 IrDAClient 執行個體。
讀取 IrDAClient 執行個體的基礎資料流,其中包含已傳輸的檔案資料。
將資料流寫入至 Receive.txt 檔案的資料流。
若要建立應用程式
為傳送裝置建立 Pocket PC 應用程式,並在表單中加入按鈕。將該按鈕命名為 [傳送]。
在 [我的文件] 資料夾中,建立名為 Send.txt 的檔案。
將下列程式碼加入 [傳送] 按鈕的 Click 事件。
' Align the infrared ports of the devices. ' Click the Receive button first, then click Send. Private Sub SendButton_Click(sender As Object, e As System.EventArgs) _ Handles SendButton.Click Dim irClient As New IrDAClient() Dim irServiceName As String = "IrDATest" Dim irDevices() As IrDADeviceInfo Dim buffersize As Integer = 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.") Return End If Try Dim irEndP As New IrDAEndPoint(irDevices(0).DeviceID, _ irServiceName) Dim irListen As New IrDAListener(irEndP) irListen.Start() irClient = irListen.AcceptIrDAClient() MessageBox.Show("Connected!") Catch exSoc As SocketException MessageBox.Show("Couldn't listen on service " & irServiceName & ": " _ & exSoc.ErrorCode) End Try ' 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()) Return End Try ' 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 As Byte() = BitConverter.GetBytes(fs.Length) baseStream.Write(length, 0, length.Length) ' Create a buffer for reading the file. Dim buffer(buffersize) As Byte Dim fileLength As Integer = 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 End While MessageBox.Show("File sent") Catch exSend As Exception MessageBox.Show(exSend.Message) End Try fs.Close() baseStream.Close() irClient.Close() End Sub
// 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 = new byte[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(); }
為接收裝置建立 Pocket PC 應用程式,並在表單中加入按鈕。將該按鈕命名為 [接收]。
將下列程式碼加入 [接收] 按鈕的 Click 事件。
' Align the infrared ports of the devices. ' Click the Receive button first, then click Send. Private Sub ReceiveButton_Click(sender As Object, e As System.EventArgs) _ Handles ReceiveButton.Click Dim irDevices() As IrDADeviceInfo Dim irClient As New IrDAClient() Dim irServiceName As String = "IrDATest" Dim buffersize As Integer = 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.") Return End If ' Connect to the first IrDA device Dim irEndP As New 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.") Return End Try ' Get the underlying stream of the client. Dim baseStream As Stream = irClient.GetStream() ' Create a buffer for reading the file. Dim buffer(buffersize) As Byte Dim 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 Try While numToRead > 0 numRead = baseStream.Read(buffer, 0, numToRead) numToRead -= numRead End While Catch exReadIn As Exception MessageBox.Show("Read in: " & exReadIn.Message) End Try ' 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) End While writeStream.Close() MessageBox.Show("File received.") Catch exWriteOut As Exception MessageBox.Show("Write out: " & exWriteOut.Message) End Try baseStream.Close() irClient.Close() End Sub
// 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 = new byte[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(); }
若要執行應用程式
將應用程式部署至裝置並啟動它們。
調整裝置的紅外線連接埠。
點選接收裝置上的 [接收] 按鈕。
點選傳送裝置上的 [傳送] 按鈕。
查看是否已在 [我的文件] 資料夾中建立了 Receive.txt。
編譯程式碼
這個範例需要下列命名空間的參考: