Click here to Skip to main content
15,885,780 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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");
            }
        }
    }
}
Posted
Comments
[no name] 25-May-14 21:03pm    
Okay, I have read this several times and I am hoping that I am misinterpreting what it is that you are trying to do. Could you rephrase your question and clearly explain what it is that you are trying to do? You start your application and then what is it that you think happens? And try not to use meaningless acronyms like "ofc" unless you define what it is that you mean. What happens "automatically" based on what?
Станислав Игоревич 26-May-14 2:40am    
when i start application and push the button my program must start looking for bluetooth devices and sending them file, its all must be automatically.
[no name] 26-May-14 6:04am    
Okay.... and? You do realize that we can't read your mind right? Create some functions and start calling them when you click "the button".
Станислав Игоревич 26-May-14 9:53am    
i simply need to use timer or something else like that?
eg i push "Start" button and the program begin to find devices and send them file every 30sec, for example, or what do you mean?
gggustafson 26-May-14 17:17pm    
You need a client AND A SERVER. I do not see both.

1 solution

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.
 
Share this answer
 
Comments
[no name] 27-May-14 10:31am    
My 5. But please consider, maybe the application is not released for Starbucks :)
Станислав Игоревич 28-May-14 4:27am    
it's none of your business, gggustafson! i just want to see the code of parameter that can give me automatic finding and sending.
will there be 10 or 100 devices that's not your thing, I did not ask you to think about it.
no offense, bro!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900