请帮助我,使用源代码C#蓝牙程序 [英] Help me, please, with source code C# Bluetooth program

查看:131
本文介绍了请帮助我,使用源代码C#蓝牙程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#上编写了一个程序,我需要自动将文件从我的PC /笔记本电脑发送到区域内所有打开的蓝牙设备。所以我无法理解如何自动查找设备并自动发送所有文件,他们需要在他们身边确认。所以这是我的程序的一个例子,它可以找到设备并手动发送文件。请帮帮我,或者给我一个链接,我可以找到解决方案。



I write a program on C#, i need to automatically send files from my PC/Laptop to all turned on bluetooth devices in area. So i cant understand how can i do automatically finding for devices and automatically sending them all my file, ofc they need to confirm it on their side. So here is an example of my program, it can find device and send a file by hand. Help me, please, or send me a link where i can find solution.

using InTheHand.Net;
using InTheHand.Net.Sockets;
using InTheHand.Net.Bluetooth;
using InTheHand.Windows.Forms;
using InTheHand.Net.Ports;
using System.Threading;

namespace Bluetooth
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        InTheHand.Net.BluetoothAddress[] address_array = new BluetoothAddress[1000];
        private Thread thrSend; 
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            InTheHand.Net.Sockets.BluetoothClient bc = new InTheHand.Net.Sockets.BluetoothClient();

            InTheHand.Net.Sockets.BluetoothDeviceInfo[] array = bc.DiscoverDevices();
            for (int i = 0; i < array.Length; i++)
            {
                this.address_array[i] = array[i].DeviceAddress;
                this.lstDevices.Items.Add(array[i].DeviceName);
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.lstDevices.SelectedIndex == -1)
            {
                MessageBox.Show("Please select a device.");
                return;
            }
            int index = this.lstDevices.SelectedIndex;
            string mac = this.address_array[index].ToString();
            string nap = this.address_array[index].Nap.ToString();
            string sap = this.address_array[index].Sap.ToString();
            MAC.Text = "MAC : " + mac.ToString();
            NAP.Text = "NAP :" + nap.ToString();
            SAP.Text = "SAP :" + sap.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.thrSend = new Thread(new ThreadStart(sendfile));
            this.thrSend.Start();

        }
        private void sendfile()
        {
            SelectBluetoothDeviceDialog dialog = new SelectBluetoothDeviceDialog();
            dialog.ShowAuthenticated = true;
            dialog.ShowRemembered = true;
            dialog.ShowUnknown = true;
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Select File";
            ofd.Filter = "All Files (*.*)|.*";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    Cursor.Current = Cursors.WaitCursor;
                    System.Uri uri = new Uri("obex://" + dialog.SelectedDevice.ToString() + "/" + ofd.FileName);
                    ObexWebRequest request = new ObexWebRequest(uri);
                    request.ReadFile(ofd.FileName);
                    ObexWebResponse response = (ObexWebResponse)request.GetResponse();
                    MessageBox.Show(response.StatusCode.ToString());
                    response.Close();
                    Cursor.Current = Cursors.Default;
                }
                else
                {
                    MessageBox.Show("File Not Selected");
                }
            }
            else
            {
                MessageBox.Show("Device Not Selected");
            }
        }
    }
}

推荐答案

有人反对发送数据到不需要它的应用程序。 所有打开区域内的蓝牙设备都是潜在的大量设备。想想进入星巴克 - 你会看到所有其他PC - 没有一个想要你的消息。
There is an objection to sending data to an application that doesn't want it. "all turned on bluetooth devices in area" is a potentially large number of devices. Think of being in a Starbucks - you would see all other PC's - none of which want your message.


这篇关于请帮助我,使用源代码C#蓝牙程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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