Bluetooth Server C# - 32feet

4.2k views Asked by At

I want my computer to be a bluetooth server, handlig at least 100 connections from smartphones simultaneously. This is the idea:

I start the server and it begin to listen for connections. For each connection it should store the mac adress of the device. Each device should be able to send messages to the server and vice versa.

I've been searching a lot but didnt find a certain way.

How to listen for connections and handle them? Store in array? Start A Thread?

I've been able to start a 1 client server, but coudnt evolve it.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Ports;
using InTheHand.Net.Sockets;
using System.IO;

namespace BluetoothChamada
{
    public partial class Form1 : Form
    {

        List<string> items;
        String clientConectedName, clientConectedMAC;

        public Form1()
        {
            InitializeComponent();
            items = new List<string>();
        }

        private void bGo_Click(object sender, EventArgs e)
        {
            if (serverStarted)
            {
                updateUI("---Servidor já foi Iniciado---");
                return;
            }
            if (rbServer.Checked)
            {
                connectAsServer();
            }
            else
            {
                Scan();
            }
        }

        private void startScan()
        {
            listBox1.DataSource = null;
            listBox1.Items.Clear();
            items.Clear();
            Thread bluetoothScanThread = new Thread(new ThreadStart(Scan));
            bluetoothScanThread.Start();
        }

        BluetoothDeviceInfo[] devices;
        private void Scan()
        {
            updateUI("Procurando dispositivos...");
            BluetoothClient client = new BluetoothClient();
            devices = client.DiscoverDevicesInRange();
            updateUI("Procura finalizada");
            updateUI(devices.Length.ToString() + " dispositivos encontrados");
            foreach (BluetoothDeviceInfo d in devices)
            {
                items.Add(d.DeviceName);
            }
            updateDeviceList();
        }

        private void connectAsServer()
        {
            Thread bluetoothServerThread = new Thread(new ThreadStart(ServerConnectThread));
            bluetoothServerThread.Start();
        }


        Guid mUUID = new Guid("00001101-0000-1000-8000-00805F9B34FB");
        bool serverStarted = false;
        public void ServerConnectThread()
        {

            serverStarted = true;
            BluetoothListener blueListener = new BluetoothListener(mUUID);
            blueListener.Start();
            updateUI("---Servidor Iniciado, aguardando clientes---\r\n\n");

            BluetoothClient connection = blueListener.AcceptBluetoothClient();
            getData();
            updateUI("Cliente: " + clientConectedName + "\r\nMac: " + clientConectedMAC);

            Stream mStream = connection.GetStream();
            while (true)
            {
                try
                {
                    byte[] received = new byte[1024];
                    mStream.Read(received, 0, received.Length);
                    updateUI(System.Environment.NewLine + "Recebido: " + Encoding.ASCII.GetString(received));

                    byte[] sent = Encoding.ASCII.GetBytes("FeedBack: OK \r\n");
                    mStream.Write(sent, 0, sent.Length);
                }
                catch (IOException exception)
                {
                    updateUI("Cliente foi desconectado");
                    break;
                }
            }
        }

        private void getData()
        {
            BluetoothClient client = new BluetoothClient();
            BluetoothDeviceInfo[] devices = client.DiscoverDevices();
            foreach (BluetoothDeviceInfo device in devices)
            {
                if (!device.Connected)
                {

                }
                else 
                { 
                    clientConectedName = device.DeviceName;
                    clientConectedMAC = device.DeviceAddress.ToString();
                }       
            }
        }

        private void updateUI(string message)
        {
            Func<int> del = delegate()
            {
                tbOutput.AppendText(message + System.Environment.NewLine);
                return 0;
            };
            Invoke(del);
        }

        private void updateDeviceList()
        {
            Func<int> del = delegate()
            {
                listBox1.DataSource = items;
                return 0;
            };
            Invoke(del);
        }

    }
}
0

There are 0 answers