Help with Bluetooth Connection

Sep 27, 2013 at 8:09 AM
Edited Sep 27, 2013 at 8:11 AM
Hey guys im pretty new to C# bluetooth connection.
I wrote an Server and Client code, but doesnt work. Its pretty difficult to find my own fault.
It should be a simple chat, but i fail with connection them.. (they are paired).
Its a connection between my Win7 Notebook and a Win8 tablet pc (not winNT!)

Hope someone can help me.
Greetings


Client:
namespace BluetoothClient
{
    public partial class Form1 : Form
    {

        List<string> items;

        private static string Addr = "9c:2a:70:2c:26:6e";
        private static string Pin = "1234";
        BluetoothAddress Address = BluetoothAddress.Parse(Addr);

        Guid UUID = new Guid("6DAEA2BB-0642-4D03-92FE-B9A48885466F");

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

        private void GoClient_Click(object sender, EventArgs e)
        {
            if (PairDevice())
            {
                listBox1.DataSource = null;
                listBox1.Items.Clear();
                items.Clear();
                updateUI("Device paired");
                Thread Scan = new Thread(new ThreadStart(ScanThread));
                Scan.Start();
            }
            else 
            {
                updateUI("Pairing failed");
            }
        }

        BluetoothDeviceInfo[] Devices;

        private void ScanThread()
        {
            
            updateUI("Starting scan");
            InTheHand.Net.Sockets.BluetoothClient Client = new InTheHand.Net.Sockets.BluetoothClient();
            Devices = Client.DiscoverDevicesInRange();
            updateUI("Scan complete");

            foreach (BluetoothDeviceInfo d in Devices)
            {
                items.Add(d.DeviceName);
            }

            updateDeviceList();
                     //Client.BeginConnect(Address, UUID, this.BluetoothClientConnectCallback, Client);
        }
               private bool PairDevice()
        {
            BluetoothDeviceInfo Info = new BluetoothDeviceInfo(Address);

            if (!Info.Authenticated)
            {
                if(!BluetoothSecurity.PairRequest(Address, Pin))
                    return false;
            }
            return true;
        }

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

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

        BluetoothDeviceInfo DeviceInfo;
        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            DeviceInfo = Devices.ElementAt(listBox1.SelectedIndex);
            updateUI(DeviceInfo.DeviceName + "was selected");

            Thread ClientConnect = new Thread(new ThreadStart(ConnectThread));
            ClientConnect.Start();
        }

        private void ConnectThread()
        {
            InTheHand.Net.Sockets.BluetoothClient Client = new InTheHand.Net.Sockets.BluetoothClient();
            updateUI("Attempting connect..");
            try
            {
                Client.Connect(DeviceInfo.DeviceAddress, UUID);
                updateUI("Success!!");
            }
            catch (System.Net.Sockets.SocketException ex)
            {
                updateUI(ex.ToString());
            }
        }


    }
}
Server (Listener):
namespace BluetoothServer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Go_Click(object sender, EventArgs e)
        {
            if (ServerRunning)
                updateUI("Server is already running!");
            else
            {
                Thread ServerThread = new Thread(new ThreadStart(ServerConnectThread));
                ServerThread.Start();
            }
        }

        Guid UUID = new Guid("6DAEA2BB-0642-4D03-92FE-B9A48885466F");

        bool ServerRunning = false;

        public void ServerConnectThread()
        {
            updateUI("Server started");
            updateUI("Waiting for client..");
            BluetoothListener BTListener = new BluetoothListener(UUID);
            BTListener.Start();
            ServerRunning = true;
            BluetoothClient conn = BTListener.AcceptBluetoothClient();
            updateUI("Client connected");

            Stream mStream = conn.GetStream();

            while (ServerRunning)
            {
                try
                {
                    byte[] received = new byte[1024];
                    mStream.Read(received, 0, received.Length);
                    updateUI("Message Received. " + Encoding.ASCII.GetString(received) + System.Environment.NewLine);
                    byte[] sent = Encoding.ASCII.GetBytes("Hello!");
                    mStream.Write(sent, 0, sent.Length);
                }
                catch (IOException ex)
                {
                    updateUI("Client disconnected");
                }

            }

        }

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