.NET Components for Mobility

Send works but I never get a response (no matter how I listen)

Last post 07-30-2009 12:56 AM by Hochman. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 06-24-2009 9:29 AM

    • tonerm
    • Not Ranked
    • Joined on 06-22-2009
    • Posts 2

    Send works but I never get a response (no matter how I listen)

    Hi,

    I have a serial device that has a brainbox serial/bluetooth adapter on the end. I can send data to the serial device from a PDA (and I know the data is correct and a light on the device flashes to let me know that it is valid) but I can never get a response. The thread just sits at the accept command and, as I'm a newbie, I don't know what I am doing wrong. I have tried 2 or 3 different PDAs running Windows mobile 6.

    I think I've seen both "incoming" and "outgoing" ports on Windows mobile devices but the ones I have tried only list "outgoing". (I appreciate that incoming/outgoing refers to who initiates the connection but thought I'd mention it)

    I've a presentation to do in 7 days where I've to get a PDA to turn motors and do exciting things but it'll be dead in the water if I can't get this working so I would really appreciate any help/advice you can give. I've posted the code below and I tried different combinations of this code.

     Thanks,Michael

     

    namespace Select_BareBones{    public partial class Form1 : Form    {         public const string bl_device_name = "brainbox";        public const string bl_device_desc = "Brainbox";         private Thread thread;        Guid ServiceName = new Guid("{E075D486-E23D-4887-8AF5-DAA1F6A5B172}");        private bool run = true;         public clSelectSerialCodes select = new clSelectSerialCodes();         public Form1()        {            InitializeComponent();            BluetoothRadio.PrimaryRadio.Mode = RadioMode.Connectable;            Program.bc = new BluetoothClient();            StartScan();        }         private void menuItem1_Click(object sender, EventArgs e)

            {

                //Menu option that sends serial code over bluetooth            lblScanning.Text = select.SendStatusCommand(11);        }         private void StartScan()        {                 Program.brainbox = null;                BluetoothDeviceInfo[ bdi = Program.bc.DiscoverDevices(12);                 foreach (BluetoothDeviceInfo bd in bdi)                {                    if (bd.DeviceName.ToLower().IndexOf(bl_device_name.ToLower()) != -1)                    {                        Program.brainbox = bd;                    }                 }                 if (Program.brainbox != null)                {                    lblScanning.Text = "Connecting to the bluetooth adapter";                    if (Program.brainbox.InstalledServices == null)                    {                        lblScanning.Text = "Serial port service on the bluetooth adapter failed";                    }                    else                    {                            Program.be = new BluetoothEndPoint(Program.brainbox.DeviceAddress, BluetoothService.SerialPort);                            Program.bc.SetPin(Program.brainbox.DeviceAddress, "1234");                            Program.bc.Connect(Program.be);                               Program.bs = InTheHand.Net.Ports.BluetoothSerialPort.CreateClient(Program.be); 

                                lblScanning.Text = "ConnectionSuccess";

                                thread = new Thread(new ThreadStart(runThread));                            thread.Start();                     }                }                else                {                    lblScanning.Text = "The bluetooth adapter could not be found";                }            }        }         private void runThread()        {                run = true;                Program.bl = new BluetoothListener(Program.be);                Program.bl.SetPin(Program.brainbox.DeviceAddress, "1234");                 Program.bl.Start();                while (run)                {                    BluetoothClient btc = Program.bl.AcceptBluetoothClient();                    StreamReader sr = new StreamReader(btc.GetStream());                                        /*NEVER GETS THIS FAR */                    sr.Close();                 }         }    }

    }

     

  • 06-25-2009 6:23 AM In reply to

    Re: Send works but I never get a response (no matter how I listen)

    You likely just need a simple BluetoothClient and nothing else. :-)  You not only send the data over the BluetoothClient connection, but also receive it there too.  Like in the user's guide:

    Dim cli As New BluetoothClient
    cli.Connect(ep)
    Dim peerStream As Stream = cli.GetStream()
    peerStream.Write/Read ... 

    So just create your StreamReader on the peerStream variable above.

    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.
  • 06-26-2009 6:49 AM In reply to

    • tonerm
    • Not Ranked
    • Joined on 06-22-2009
    • Posts 2

    Re: Send works but I never get a response (no matter how I listen)

    Thanks for your reply. Sorry that my posting of code came out even more ribberish than I expected.

     I've got it working now using both your example (thanks again) and a thread.

     It turns out I was issuing a command that doesn't recieve a response which didn't help. It doesn't matter how much you convince yourself you are not doing something stupid...9 times out of 10, you are.

    Cheers

    Michael

  • 06-26-2009 9:12 AM In reply to

    Re: Send works but I never get a response (no matter how I listen)

    :-)

    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.
  • 07-21-2009 6:28 PM In reply to

    Re: Send works but I never get a response (no matter how I listen)

    Hi everyone.. I have been looking at Alan's example at http://32feet.net/forums/t/2571.aspx  and the explanation..

    Dim cli As New BluetoothClient
    cli.Connect(ep)
    Dim peerStream As Stream = cli.GetStream()
    peerStream.Write/Read ... 

    So just create your StreamReader on the peerStream variable above.

    But actually, I have tried that and it does not work...

    My Client application:

    BluetoothClient btClient.Connect(btAddress, BackupGUID);

    Stream peerStream = btClient.GetStream();

    StreamReader btStreamReader = new StreamReader(peerStream);

    StreamWriter btStreamWriter = new StreamWriter(peerStream);

    And my server application:

    BluetoothClient btClient = btListener.AcceptBluetoothClient();

    Stream peerStream = btClient.GetStream();

    StreamReader btStreamReader = new StreamReader(peerStream);

    StreamWriter btStreamWriter = new StreamWriter(peerStream);

    But then ONLY sending from client to server is working !!! It is not bidirectional.. Am I missing something please?? or should I create another stream in the opposite direction (client also listening and then acceptBluetoothClient) ??

    I'd be happy if you could help me...

    Hochman
  • 07-27-2009 11:05 AM In reply to

    Re: Send works but I never get a response (no matter how I listen)

    Have you made any progress with this?  Connections are bi-directional, so...
    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.
  • 07-30-2009 12:56 AM In reply to

    Re: Send works but I never get a response (no matter how I listen)

    Yep Alan, it is working now..

    The problem was with Flush().. It was missing.... But I am still confused about WriteLine() and ReadLine().. I have been looking for the maximum length of each but in vain.. I mean that if I have this example:

    string s = "fgfdghfdjghjfdg.........................hghghghg";  // VERY VERY long string (I need to send details about ALL files that exist in a crowded folder !!)

    Client:     sw.WriteLine(s);   sw.Flush();

    Server:    string received = sr.ReadLine(); 

    will it work ?? Does STREAM mean it is like  sending in real-time no matter what the length of the message ?? or does it mean there is still a buffer (in this case what is the max size.. I could not find it) ??.. In the worst case, is it better if I use a loop !!??? I wanted to avoid a LOOP for sending/receiving in order to decrease the Bluetooth overhead  (I expect the transmission will be quicker if I send ALL data at once without using a loop)...

    I'd be happy if you could help...

     Thnx..

    Hochman
Page 1 of 1 (7 items)
Copyright © 2001-2010 In The Hand Ltd. All rights reserved. Terms of Use and Privacy Policy.