2

I have an issue about the server-client communication. I googled around but I did not find a solution to this. Right now I am using 32feet in order to get in touch 2 or more (till 7) BT clients to 1 BT server. I need to broadcast a message from the server to every device in the same time, but I don't know how to do it. The only way I figured out was to use the list of connection in order to send the message one per time, but it means a delay between each message sent (around 100 ms per device). Unfortunately it means to have a large delay on the last one. Can someone please give me an advice on how to solve this problem? Is there a way to broadcast the message to all devices in the same time?

If it can be helpfull, here there is the handle of connection and reading from devices.

Thanks for your help

 private void btnStartServer_Click(object sender, EventArgs e)
    {
        btnStartClient.Enabled = false;
        ConnectAsServer();
    }

    private void ConnectAsServer()
    {
        connessioniServer = new List<BluetoothClient>();

        // thread handshake
        Thread bluetoothConnectionControlThread = new Thread(new ThreadStart(ServerControlThread));
        bluetoothConnectionControlThread.IsBackground = true;
        bluetoothConnectionControlThread.Start();

        // thread connessione
        Thread bluetoothServerThread = new Thread(new ThreadStart(ServerConnectThread));
        bluetoothServerThread.IsBackground = true;
        bluetoothServerThread.Start();
    }

    private void ServerControlThread()
    {
        while (true)
        {
            foreach (BluetoothClient cc in connessioniServer)
            {
                if (!cc.Connected)
                {
                    connessioniServer.Remove(cc);
                    break;
                }
            }
            updateConnList();
            Thread.Sleep(0);
        }
    }

    Guid mUUID = new Guid("fc5ffc49-00e3-4c8b-9cf1-6b72aad1001a");
    private void ServerConnectThread()
    {
        updateUI("server started");
        BluetoothListener blueListener = new BluetoothListener(mUUID);
        blueListener.Start();
        while (true)
        {
            BluetoothClient conn = blueListener.AcceptBluetoothClient();
            connessioniServer.Add(conn);
            Thread appoggio = new Thread(new ParameterizedThreadStart(ThreadAscoltoClient));
            appoggio.IsBackground = true;
            appoggio.Start(conn);
            updateUI(conn.RemoteMachineName+" has connected");

        }
    }

    private void ThreadAscoltoClient(object obj)
    {
        BluetoothClient clientServer = (BluetoothClient)obj;
        Stream streamServer = clientServer.GetStream();
        streamServer.ReadTimeout=1000;
        while (clientServer.Connected)
        {
            try
            {
                int bytesDaLeggere = clientServer.Available;
                if (bytesDaLeggere > 0)
                {
                    byte[] bytesLetti = new byte[bytesDaLeggere];
                    int byteLetti = 0;
                    while (bytesDaLeggere > 0)
                    {
                        int bytesDavveroLetti = streamServer.Read(bytesLetti, byteLetti, bytesDaLeggere);
                        bytesDaLeggere -= bytesDavveroLetti;
                        byteLetti += bytesDavveroLetti;
                    }
                    updateUI("message sent from "+clientServer.RemoteMachineName+": " + System.Text.Encoding.Default.GetString(bytesLetti));
                }
            }
            catch { }
            Thread.Sleep(0);
        }
        updateUI(clientServer.RemoteMachineName + " has gone");
    }



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

    private void updateConnList()
    {
        Func<int> del = delegate()
        {
            listaSensori.Items.Clear();
            foreach (BluetoothClient d in connessioniServer)
            {
                listaSensori.Items.Add(d.RemoteMachineName);
            }
            return 0;
        };
        try
        {
            Invoke(del);
        }
        catch { }
    }

1 Answers1

1

I don't exactly understand how you do it right now (the italian names are not helping...) but maybe my solution can help you.

first of all, bluetooth classic does not support broadcast. so you have to deliver at one at a time. i do connect to 7 serial port devices at a time, using 7 threads. then i tell every thread to send data. this is very close to same time, but of course not exactly.

let me know if that helps or if you need a code example.

Veit
  • 101
  • 1
  • 5