views:

348

answers:

3

i'm new to bluetooth development and i found the 32netfeet . Right now i'm able to search for bluetooth devices nearby and connect to them but how do i send a file e.g SendTest.txt? I tried buttonclick event using the OBEX but i don't understand this is my example code:

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://"+@"SendTest.txt");
                    ObexWebRequest req= new ObexWebRequest(uri);

                    ObexWebResponse rsp;


        }

I found the guide but don't really know how to convert to 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)
A: 
  1. // The host part of the URI is the device address, e.g. IrDAAddress.ToString(),
  2. // and the file part is the OBEX object name. 3.
  3. {
  4. string addr = "112233445566";
  5. Uri uri = new Uri("obex://" + addr + "/HelloWorld2.txt");
  6. ObexWebRequest req = new ObexWebRequest(uri);
  7. using (Stream content = req.GetRequestStream()) {
  8. // Using a StreamWriter to write text to the stream...
    1. using (StreamWriter wtr = new StreamWriter(content)) {
    2. wtr.WriteLine("Hello World GetRequestStream");
    3. wtr.WriteLine("Hello World GetRequestStream 2");
    4. wtr.Flush();
    5. // Set the Length header value
    6. req.ContentLength = content.Length;
    7. }
    8. }
    9. // In this case closing the StreamWriter also closed the Stream, but ...
    10. ObexWebResponse rsp = (ObexWebResponse)req.GetResponse();
    11. Console.WriteLine("Response Code: {0} (0x{0:X})", rsp.StatusCode);
    12. }
pjb
does anyone have some other samples? or could explain to me the basics of file sending etc?
cheesebunz
Are there anyone i could get help from...? it's just a simple file transfer ..
cheesebunz
Read the user guide, you may have done this already. Its in the release or at http://www.alanjmcf.me.uk/comms/bluetooth/32feet.NET%20--%20User%20Guide.htmlAsk in our forums? http://inthehand.co.uk/forums/default.aspx?GroupID=29
alanjmcf
A: 

ans:

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();
cheesebunz
A: 

The C4F (Coding 4 Fun Kit) has an implementation of obex - http://blogs.msdn.com/b/coding4fun/archive/2007/08/02/4192571.aspx

I've used it and found it to be quite reliable.

obelix