2010-05-26 117 views
2

我是新來的藍牙開發,我發現了32netfeet。現在我能夠搜索附近的藍牙設備並連接到它們,但是如何發送文件,例如SendTest.txt?我試圖ButtonClick事件使用OBEX,但我不明白這是我的示例代碼:藍牙文件發送

using InTheHand.Net; 
using InTheHand.Net.Sockets; 
using InTheHand.Net.Bluetooth; 

namespace BluetoothIntheHand 
{ 
    public partial class Form2 : Form 
    { 
     private Guid service = BluetoothService.DialupNetworking; 
     private BluetoothClient bluetoothClient; 

     public Form2() 
     { 
      InitializeComponent(); 
     } 

     private void btnSearch_Click(object sender, EventArgs e) 
     { 

       BluetoothRadio.PrimaryRadio.Mode = RadioMode.Discoverable; 
       BluetoothRadio myRadio = BluetoothRadio.PrimaryRadio; 
       lblSearch.Text = "" + myRadio.LocalAddress.ToString(); 

       bluetoothClient = new BluetoothClient(); 
       Cursor.Current = Cursors.WaitCursor; 
       BluetoothDeviceInfo[] bluetoothDeviceInfo = { }; 
       bluetoothDeviceInfo = bluetoothClient.DiscoverDevices(10); 
       comboBox1.DataSource = bluetoothDeviceInfo; 
       comboBox1.DisplayMember = "DeviceName"; 
       comboBox1.ValueMember = "DeviceAddress"; 
       comboBox1.Focus(); 
       Cursor.Current = Cursors.Default; 

     } 

     private void btnConnect_Click(object sender, EventArgs e) 
     { 
      if (comboBox1.SelectedValue != null) 
      { 
       try 
       { 
        bluetoothClient.Connect(new BluetoothEndPoint((BluetoothAddress)comboBox1.SelectedValue, service)); 
        MessageBox.Show("Connected"); 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show(ex.Message); 
       } 
      } 
     } 


    private void btnSend_Click(object sender, EventArgs e) 
     { 

        bluetoothClient.Connect(new BluetoothEndPoint((BluetoothAddress)comboBox1.SelectedValue, service)); 
        String addr = "112233445566"; 
        Uri uri = new Uri("obex://"[email protected]"SendTest.txt"); 
        ObexWebRequest req= new ObexWebRequest(uri); 

        ObexWebResponse rsp; 


     } 

我找到了指導,但真的不知道該如何轉換爲C#

' The host part of the URI is the device address, e.g. IrDAAddress.ToString(), 
' and the file part is the OBEX object name. 
Dim addr As String = "112233445566" 
Dim uri As New Uri("obex://" & addr & "/HelloWorld2.txt") 
Dim req As New ObexWebRequest(uri) 
Using content As Stream = req.GetRequestStream() 
    ' Using a StreamWriter to write text to the stream... 
    Using wtr As New StreamWriter(content) 
     wtr.WriteLine("Hello World GetRequestStream") 
     wtr.WriteLine("Hello World GetRequestStream 2") 
     wtr.Flush() 
     ' Set the Length header value 
     req.ContentLength = content.Length 
    End Using 
    ' In this case closing the StreamWriter also closed the Stream, but ... 
End Using 
Dim rsp As ObexWebResponse = CType(req.GetResponse(),ObexWebResponse) 
Console.WriteLine("Response Code: {0} (0x{0:X})", rsp.StatusCode) 

回答

0
// The host part of the URI is the device address, e.g. IrDAAddress.ToString(), 
// and the file part is the OBEX object name. 

{ 
    string addr = "112233445566"; 
    Uri uri = new Uri("obex://" + addr + "/HelloWorld2.txt"); 
    ObexWebRequest req = new ObexWebRequest(uri); 
    using (Stream content = req.GetRequestStream()) { 
     // Using a StreamWriter to write text to the stream... 
     using (StreamWriter wtr = new StreamWriter(content)) { 
      wtr.WriteLine("Hello World GetRequestStream"); 
      wtr.WriteLine("Hello World GetRequestStream 2"); 
      wtr.Flush(); 
      // Set the Length header value 
      req.ContentLength = content.Length; 
     } 
    } 
    // In this case closing the StreamWriter also closed the Stream, but ... 
    ObexWebResponse rsp = (ObexWebResponse)req.GetResponse(); 
    Console.WriteLine("Response Code: {0} (0x{0:X})", rsp.StatusCode); 
} 
+0

沒有人有其他一些樣品嗎?或者可以向我解釋文件發送的基礎知識等? – cheesebunz 2010-05-26 06:24:27

+0

有沒有人可以從...獲得幫助?它只是一個簡單的文件傳輸。 – cheesebunz 2010-05-26 07:25:15

+0

閱讀用戶指南,你可能已經做到了這一點。它在發佈或http://www.alanjmcf.me.uk/comms/bluetooth/32feet.NET%20--%20User%20Guide.html 在我們的論壇提問? http://inthehand.co.uk/forums/default.aspx?GroupID=29 – alanjmcf 2010-05-27 19:29:28

0

答:

string fileName = @"SendTest.txt"; 

String adr = "0025677FB346"; 
Uri uri = new Uri("obex://" + adr + "/" + System.IO.Path.GetFileName(fileName)); 
ObexWebRequest request = new ObexWebRequest(uri); 
request.ReadFile(fileName); 

ObexWebResponse response = (ObexWebResponse)request.GetResponse(); 
MessageBox.Show(response.StatusCode.ToString()); 
response.Close();