This project has moved. For the latest updates, please go here.
2
Vote

Broadcast from a BT server to BT clients

description

Hello,
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. Unfortunately I risk to have a large delay on the last one.
Can someone please give me an advice on how to solve this problem?

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 { }
        }

comments