2

I am using Bluetooth communication between an Android app and a C# Windows form app. The Android app works as a client and the C# app as a server. I can handle only on connection on the server (laptop) and when I disconnect and try to connect again nothing seems to be happening. I think that the client side works properly because I haved tested it with another mobile and the bug is in server code.

This is the server code:

public partial class Form1 : Form
{
    Thread connectserver;
    Stream mstream;
    BluetoothClient client;
    BluetoothListener bluelisten;
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (serverstart)
        {
            updateui("server already started");
        }
        connectasserver();
    }

    private void connectasserver()
    {
         connectserver=new Thread(new ThreadStart (serverconnectedthread));
        connectserver.Start();

    }
    Guid muuid = new Guid("00001101-0000-1000-8000-00805F9B34FB");
    bool serverstart = false;
    public void serverconnectedthread()
    {
        serverstart = true;
        updateui("waiting for connections of clients\n");
         bluelisten = new BluetoothListener(muuid);
        bluelisten.Start();
        client = new BluetoothClient();
         client = bluelisten.AcceptBluetoothClient();
        updateui("client has connected\n");
        mstream=client.GetStream();

        while(true)
        {
            try
            {
                byte[] recieved = new byte[1024];
                mstream.Read(recieved, 0, recieved.Length);

                updateui("recieved: " + Encoding.ASCII.GetString(recieved));
                byte[] sent = Encoding.ASCII.GetBytes(" hello world\n");
                mstream.Write(sent, 0, sent.Length);
            }
            catch(IOException except)
            {

                updateui("client has been disconnected\n");
                connectserver.Abort();
                client.Close();
                mstream.Flush();
                break;
            }
       }

    }

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

    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void textBox3_TextChanged()
    {

    }
}
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
nayirmicheal
  • 23
  • 1
  • 7
  • Your question does not seem clear enough, do you mean you would have to restart the c# software after disconnecting for you to connect again? – oziomajnr Jun 12 '16 at 15:25
  • I want after I didconnected first device connected yo server to connect again with the same device or othet device and this doesn't happen with this code. This code only can have one connect with it and after I disconnect it ,it can't listen to any other connection from any other device or the same device – nayirmicheal Jun 12 '16 at 15:41
  • Which error do u get when u try to listen for connection the second time? – oziomajnr Jun 12 '16 at 21:08
  • I got no error but i couldn't listen for any other connection (only the first connection) – nayirmicheal Jun 13 '16 at 00:15

1 Answers1

1

catch(IOException except) {

            updateui("client has been disconnected\n");
            connectserver.Abort();
             severstart = false;      // add this line to the catch block.
            client.Close();
            mstream.Flush();
            break;
        }

Set severstart equal to false so that it does not assume it has already started server.

oziomajnr
  • 1,671
  • 1
  • 15
  • 39