c# - 蓝牙文件发送

标签 c# bluetooth windows-mobile-6 obex

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

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;


        }

我找到了指南,但不太知道如何转换为 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)

最佳答案

和:

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();

关于c# - 蓝牙文件发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2910022/

相关文章:

c# - Regular Expressions-VAlidation String-REAL菜鸟问题:)

c# - 如何在 Windows Mobile 6.5 上终止进程

android - OBDSim 在 Windows 8.1 上通过蓝牙与 Android 设备连接

android - android 2.1(三星 spica i5700)上的蓝牙问题,配对有效但连接无效

c# - 在C#.Net中自动调用函数

c# - 我如何知道 Windows Mobile 是否支持此方法?

c# - WinUI 3 运行时本地化

c# - 使用restsharp发布到休息服务

c# - 我需要返回匹配变量值的数据

java - 蓝牙传输应用程序在使用 InputStream.read() 后停止,没有错误