.NET Components for Mobility

Problem setting up simple Bluetooth application

Last post 03-18-2010 6:25 AM by alanjmcf. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 03-10-2010 6:09 PM

    • Fallen
    • Not Ranked
    • Joined on 03-10-2010
    • Posts 1

    Embarrassed [:$] Problem setting up simple Bluetooth application

    I'm writing a simple bluetooth application for sending a data from my Samsung Omnia 2 to my laptop. I'm experimenting first by sending a simple Hello message but am getting this error.

    Socket Exception

    No connection could be made because the target computer actively refused it.

    I know its a relatively common issue so can someone here who's experience help me with it? I am shamefully a beginner to programming so I don't thing I can solve this on my own.

    My code. Which you will notice is mostly butchered from the original sample code.

    Samsung Omnia 2 Side : Supposedly searches for the device when I click the button, then sends a single hello to the stream.

    private void btnConnect_Click(object sender, EventArgs e)
            {
                
                {
                    SelectBluetoothDeviceDialog dlg = new SelectBluetoothDeviceDialog();
                    DialogResult result = dlg.ShowDialog();
                    if (result != DialogResult.OK)
                    {
                        return;
                    }
                    BluetoothDeviceInfo device = dlg.SelectedDevice;
                    BluetoothAddress addr = device.DeviceAddress;
    
                    BluetoothEndPoint ep = new BluetoothEndPoint(addr, ServiceName);
                    BluetoothClient cli = new BluetoothClient();
    
                    cli.Connect(ep);
    
                    Stream peerStream = cli.GetStream();
    
                    byte[ buffer = new byte[255];
                    buffer = System.Text.ASCIIEncoding.ASCII.GetBytes("Hello");
    
                    peerStream.Write(buffer, 0, buffer.Length);
    
                }
    

    Laptop side. Just waits to receive the message.

    private void timer1_Tick(object sender, EventArgs e)
           {
                int bytesRead;
                BluetoothClient client = null;
                BluetoothListener lis = new BluetoothListener(ServiceName);
                lis.Start();
                string text;
                Stream peerstream = null;
                byte[ Buffer = null;
    
                try
                {
                    client = lis.AcceptBluetoothClient();
                    //  blocking call
                    peerstream = client.GetStream();
                    bytesRead = peerstream.Read(Buffer, 0, 100);
                    textBox1.Text = System.Text.ASCIIEncoding.ASCII.GetString(Buffer,0,bytesRead);
    
                }
                catch (Exception)
                {
                           MessageBox.Show("Error listening to incoming message");
                    
                }
    
            }
    

    I'm using

    Guid ServiceName = new Guid("{00112233-4455-6677-8899-aabbccddeeff}");
    as my ID by the way.
  • 03-18-2010 6:25 AM In reply to

    Re: Problem setting up simple Bluetooth application

    Don't know whether you've fixed this already.  The same Service Class Id (UUID/Guid) needs to be used by client and server.

    Alan J. McFarlane
    http://www.alanjmcf.me.uk/
    Please follow-up in the newsgroup for the benefit of all.
    Have I helped? Consider visiting my Amazon wishlist, see my homepage.
Page 1 of 1 (2 items)
Copyright © 2001-2010 In The Hand Ltd. All rights reserved. Terms of Use and Privacy Policy.