.NET Components for Mobility

Receive a String via SPP

Last post 03-09-2010 7:37 AM by fernandovallecordoba. 7 replies.
Page 1 of 1 (8 items)
Sort Posts: Previous Next
  • 02-22-2010 4:57 AM

    Receive a String via SPP

     

    Hello,

    I´m doing a program which keeps listening until mi mobile sends a String. I´m having problems receiving that string. What´s wrong in my code?

     

                BluetoothListener BluetoothListener1 = new BluetoothListener(BluetoothService.SerialPort);
                BluetoothListener1.LocalEndPoint.Port = 1;
                BluetoothListener1.Start();
               while(true){
                   BluetoothClient blue_clientlistener = new BluetoothClient();
                   blue_clientlistener.Connect(;
                   Stream stream = blue_clientlistener.GetStream();
                   StreamReader streamReader = new StreamReader(stream);
                   string s = streamReader.ReadToEnd();
                   MessageBox.Show("Escuchando"+s);
               }

     

    Thanks you very much in advance,

     

    Fernando.

     

     

  • 02-22-2010 1:20 PM In reply to

    Re: Receive a String via SPP

    Something's obviously gone wrong with the copy&paste here, the Connect line is broken.  However its unclear whether you're wanting to run a RFCOMM server on a client.  See the code examples in the user guide.  Have you written a program for your mobile too, or are you using something built-in?

  • 02-23-2010 4:05 AM In reply to

    Re: Receive a String via SPP

     Sorry, I didn´t explain it very good. What I want is to transfer via bluetooth a string from my mobile to my PC. I just have to do the PC part, so I have to to a program which keep listening until it listen to a string. I´m doing it in C#. I haven´t seen any example of that. There are examples of tranfering files, but I just want to receive a String.

     

    BluetoothListener BluetoothListener1 = new BluetoothListener(BluetoothService.SerialPort);
                BluetoothListener1.LocalEndPoint.Port = 1;
                BluetoothListener1.Start();
               while(true){
                   BluetoothClient blue_clientlistener = new BluetoothClient();
                   blue_clientlistener.Connect();
                   Stream stream = blue_clientlistener.GetStream();
                   StreamReader streamReader = new StreamReader(stream);
                   string s = streamReader.ReadToEnd();
                   MessageBox.Show("Escuchando"+s);
               }

     

  • 02-25-2010 5:16 AM In reply to

    Re: Receive a String via SPP

    Does that compile?  There's no Connect method that takes no parameters...   Also when using a server (BluetoothListener), one gets the connected BluetoothClient by calling AcceptBluetoothClient.  Directly from the user guide:

       Dim lsnr As New BluetoothListener(MyConsts.MyServiceUuid)
      lsnr.Start()
      ' Now accept new connections, perhaps using the thread pool to handle each
      Dim conn As BluetoothClient = lsnr.AcceptBluetoothClient()
      Dim peerStream As Stream = conn.GetStream()
      ...

    Assuming the program on the mobile sends to SPP then that should work when combined with your code.  Replace MyConsts.MyServiceUuid by BluetoothService.SerialPort, and use your StreamReader.  Note that ReadToEnd will not return until the mobile *closes* the connection.  Perhaps have the mobile send a CR+LF newline after each line and thus use ReadLine().

  • 03-02-2010 7:21 AM In reply to

    Re: Receive a String via SPP

    Thanks Alanjmcf.

    What I want to do is to keep listening until I receive a String. Once received, the program read it, sends a file to the String´s sender and starts listening again. The problem is that it works very bad, it only receive the string sometimes. Most of the times it stays listening but doesn´t get anything. I do not know if it is because I don´t close the session, the stream, the client or what...

    My code is as follow:

     

    private void backgroundWorker8_DoWork(object sender, DoWorkEventArgs e)
            {

               int bytesRead=0;
              
               System.IO.Stream stream1=null;
               byte [ Buffer=new byte [255];
              
               BluetoothListener btListener = new BluetoothListener(BluetoothService.SerialPort);
               btListener.Start();
               String str;
               String nombreDevice;
               ObexClientSession session;

               BluetoothClient client2 = new BluetoothClient();
              
               while (true)
               {

                   try
                   {
                   
                       btListener.Pending();
                       BluetoothClient client = btListener.AcceptBluetoothClient();
                       stream1 = client.GetStream();


                       bytesRead = stream1.Read(Buffer, 0, 255);
                       str = System.Text.Encoding.ASCII.GetString(Buffer, 0, bytesRead);
                       nombreDevice = client.RemoteMachineName.ToString();
                       client.Close();
                       stream1.Close();
     

                      for (int ii = 0; ii < devices.Length; ii++)
                       {
                    
                           if (String.Compare(nombreDevice, devices[ii].DeviceName) == 0 || String.Compare(nombreDevice, devices[ii].DeviceAddress.ToString()) == 0)
                           {
                             
                               BluetoothEndPoint remoteEndPoint = new BluetoothEndPoint(devices[ii].DeviceAddress, BluetoothService.ObexObjectPush);
                               client2.Connect(remoteEndPoint);
                              
                               session = new ObexClientSession(client2.GetStream(), UInt16.MaxValue);
                               session.Connect();
                             
                               switch (str)
                               {
                                   case "@001#":  
                                       session.PutFile(SPAMMovil);
                                       break;
                                   case "@002#":  
                                       session.PutFile(SPAMPC);
                                       break;
                                   case "@004#":  
                                       session.PutFile(SPAMSmartphone);
                                       break;
                                   case "@000#":   
                                       session.PutFile(SPAMPDA);
                                       break;
                                   case "@003#":   
                                       session.PutFile(SPAMPDA);
                                       break;
                               }
                               session.Disconnect();
                           }
                       }
                   }
                   catch (Exception err)
                   {

                       client2.Close();
                   }
               }
            }

     

    Any help is welcomed! Thankss you!!

     

    Fernando.

  • 03-03-2010 3:02 PM In reply to

    Re: Receive a String via SPP

    What platform is your program running on?  Win32 or WM?  MSFT stack or Widcomm?

  • 03-04-2010 5:24 AM In reply to

    Re: Receive a String via SPP

     Win32 using MSFT.

    I tried to install both but it was impossible.

    I modified the code a little bit trying to improve the results, but it is the same, not always it listen the string... Sad:

     

               byte[ Buffer;
              
               BluetoothListener btListener = new BluetoothListener(BluetoothService.SerialPort);
               btListener.Start();
               String str;
               String nombreDevice;
               ObexClientSession session;

               BluetoothClient client2 = new BluetoothClient();
              
               while (true)
               {

                   try
                   {
                       System.IO.Stream stream1 = null;
                       Buffer = new byte[5];
                       int bytesRead = 0;

                       btListener.Pending();

                       BluetoothClient client = btListener.AcceptBluetoothClient();

                       stream1 = client.GetStream();
                       bytesRead = stream1.Read(Buffer, 0, Buffer.Length);

                       str = System.Text.Encoding.ASCII.GetString(Buffer, 0, bytesRead);
                       MessageBox.Show(str);

                       nombreDevice = client.RemoteMachineName.ToString();

                       client.Close();
                       stream1.Flush();
                       stream1.Close();
                      
                       for (int ii = 0; ii < devices.Length; ii++)
                       {

                           if (String.Compare(nombreDevice, devices[ii].DeviceName) == 0 || String.Compare(nombreDevice, devices[ii].DeviceAddress.ToString()) == 0)
                           {

                               BluetoothEndPoint remoteEndPoint = new BluetoothEndPoint(devices[ii].DeviceAddress, BluetoothService.ObexObjectPush);

                               client2.Connect(remoteEndPoint);

                               session = new ObexClientSession(client2.GetStream(), UInt16.MaxValue);
                               session.Connect();


                               switch (str)
                               {
                                   case "@001#":   //aleman
                                       idioma = "aleman";
                                       break;
                                   case "@002#":   //español
                                       idioma = "español";
                                       break;
                                   case "@004#":   //frances
                                       idioma = "frances";
                                       break;
                                   case "@000#":    //japo
                                       idioma = "ingles";
                                       break;
                                   case "@003#":    //ingles
                                       idioma = "ingles";
                                       break;
                               }

                               switch (idioma)
                               {
                                   case "español":
                                       switch (dispositivo)
                                       {
                                           case 0:
                                               session.PutFile(españolMovil);
                                               break;
                                           case 1:
                                               session.PutFile(españolPC);
                                               break;
                                           case 2:
                                               session.PutFile(españolSmartphone);
                                               break;
                                           case 3:
                                               session.PutFile(españolPDA);
                                               break;

                                       }
                                       break;

                                   case "inglés":
                                       switch (dispositivo)
                                       {
                                           case 0:
                                               session.PutFile(inglesMovil);
                                               break;
                                           case 1:
                                               session.PutFile(inglesPC);
                                               break;
                                           case 2:
                                               session.PutFile(inglesSmartphone);
                                               break;
                                           case 3:
                                               session.PutFile(inglesPDA);
                                               break;
                                       }
                                       break;
                                 
                               }
                               session.Disconnect();
                           }
                       }
                
                   }
                   catch (Exception err)
                   {

                       client2.Close();

                   }
               }        }

     

     

    PD: I know bluetooth has 7 threads to work at the same time, so I can send a file up to 7 devices at the same time, but is it possible to receive a string in my pc from a device via bluetooth while I am sending files to others devices??

     

     Thankssssss

     

  • 03-09-2010 7:37 AM In reply to

    Re: Receive a String via SPP

     Ok, I already solved the problem... The thing was that I can´t not keep listening for a String and at the same time, in anothe thread, do:

    while (true){

          devices = blue_client1.DiscoverDevices();           
     }

     

    So, I need to work with two dongles at the same time... which is quite difficult or impossible... I´m working with Microsoft stack and trying to install widcomm one... any advice??

     

Page 1 of 1 (8 items)
Copyright © 2001-2012 In The Hand Ltd. All rights reserved. Terms of Use and Privacy Policy. OrcsWeb's Windows Cloud Server Hosting