蓝牙发送文件 [英] bluetooth file send

查看:218
本文介绍了蓝牙发送文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的蓝牙开发,我发现32netfeet。现在,我可以到附近搜索蓝牙设备并连接到他们,但我如何发送文件例如SendTest.txt?我试着ButtonClick事件使用OBEX,但我不明白这是我的例子code:

 使用InTheHand.Net;
使用InTheHand.Net.Sockets;
使用InTheHand.Net.Bluetooth;命名空间BluetoothIntheHand
{
    公共部分类窗体2:表
    {
        私人的Guid =服务BluetoothService.DialupNetworking;
        私人BluetoothClient bluetoothClient;        公共窗体2()
        {
            的InitializeComponent();
        }        私人无效btnSearch_Click(对象发件人,EventArgs的发送)
        {                BluetoothRadio.PrimaryRad​​io.Mode = RadioMode.Discoverable;
                BluetoothRadio myRadio = BluetoothRadio.PrimaryRad​​io;
                lblSearch.Text =+ myRadio.LocalAddress.ToString();                bluetoothClient =新BluetoothClient();
                Cursor.Current = Cursors.WaitCursor;
                BluetoothDeviceInfo [] bluetoothDeviceInfo = {};
                bluetoothDeviceInfo = bluetoothClient.DiscoverDevices(10);
                comboBox1.DataSource = bluetoothDeviceInfo;
                comboBox1.DisplayMember =设备名称;
                comboBox1.ValueMember =DeviceAddress;
                comboBox1.Focus();
                Cursor.Current = Cursors.Default;        }        私人无效btnConnect_Click(对象发件人,EventArgs的发送)
        {
            如果(comboBox1.SelectedValue!= NULL)
            {
                尝试
                {
                    bluetoothClient.Connect(新BluetoothEndPoint((BluetoothAddress)comboBox1.SelectedValue,服务));
                    MessageBox.Show(已连接);
                }
                赶上(异常前)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
   私人无效btnSend_Click(对象发件人,EventArgs的发送)
        {                    bluetoothClient.Connect(新BluetoothEndPoint((BluetoothAddress)comboBox1.SelectedValue,服务));
                    字符串地址=112233445566;
                    开放的我们的uri =新的URI(OBEX://+ @SendTest.txt);
                    ObexWebRequest REQ =新ObexWebRequest(URI);                    ObexWebResponse RSP;
        }

我找到了指南,但真的不知道如何转换为C#

 URI的主机部分是设备地址,例如IrDAAddress.ToString(),
'和文件部分是OBEX对象名称。
昏暗地址作为字符串=112233445566
昏暗URI作为新的URI(OBEX://&放大器;地址&安培;/HelloWorld2.txt)
昏暗的REQ作为新ObexWebRequest(URI)
使用内容作为流= req.GetRequestStream()
   使用的StreamWriter写入文本流中...
   使用WTR作为新的StreamWriter(内容)
      wtr.WriteLine(的Hello World GetRequestStream)
      wtr.WriteLine(的Hello World GetRequestStream 2)
      wtr.Flush()
      设定长度标头值
      req.Co​​ntentLength = content.Length
   使用完
   在这种情况下,关闭的StreamWriter还关闭了流,但...
使用完
昏暗RSP作为ObexWebResponse = CTYPE(req.GetResponse(),ObexWebResponse)
Console.WriteLine(响应code:{0}(0X {0:X}),rsp.Status code)


解决方案

  // URI的主机部分是设备地址,例如IrDAAddress.ToString(),
//和文件部分是OBEX对象名称。{
    字符串地址=112233445566;
    开放的我们的uri =新的URI(OBEX://+地址+/HelloWorld2.txt);
    ObexWebRequest REQ =新ObexWebRequest(URI);
    使用(流内容= req.GetRequestStream()){
        //使用的StreamWriter写入文本流中...
        使用(WTR的StreamWriter =新的StreamWriter(内容)){
            wtr.WriteLine(的Hello World GetRequestStream);
            wtr.WriteLine(的Hello World GetRequestStream 2);
            wtr.Flush();
            //设置长度标头值
            req.Co​​ntentLength = content.Length;
        }
    }
    //在这种情况下关闭的StreamWriter还关闭了流,但...
    ObexWebResponse RSP =(ObexWebResponse)req.GetResponse();
    Console.WriteLine(响应code:{0}(0X {0:X}),rsp.Status code);
}

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)

解决方案

// 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);
}

这篇关于蓝牙发送文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆