Blocking Bluetooth

Topics: Bluetooth - Microsoft
May 29, 2012 at 7:59 AM

Hi we are developing a bt communication using c# and 32feet ITH. 

We got blocking socket. using this code:

private void button3_Click(object sender, RoutedEventArgs e) 
        {
            BluetoothListener lsnr = new BluetoothListener(BluetoothService.SerialPort);
            lsnr.Start();         
            BluetoothClient conn = new BluetoothClient();
            conn = lsnr.AcceptBluetoothClient();
            System.IO.Stream peerStream = conn.GetStream();

How to avoid it?

any example?

thanks a lot

May 29, 2012 at 8:31 AM

any example how to use async methods?

public System.IAsyncResult BeginAcceptBluetoothClient(System.AsyncCallback callback, object state)

public InTheHand.Net.Sockets.BluetoothClient EndAcceptBluetoothClient(System.IAsyncResult asyncResult)

 

Developer
May 29, 2012 at 8:46 PM

For Accept I often just use a separate thread for looping on Accept.  Since Accept is so promptly handled it often doesn't see worthwhile doing the complex APM for it; and that single thread fires off a new thread or async handling of each individual new connection.

See the code in sample BluetoothChat2 which does:

private void StartListener()
{
   var lsnr = new BluetoothListener(OurServiceClassId);

   lsnr.ServiceName = OurServiceName;
   lsnr.Start();
   _lsnr = lsnr;
   ThreadPool.QueueUserWorkItem(ListenerAccept_Runner, lsnr);

}

void ListenerAccept_Runner(object state)
{
   var lsnr = (BluetoothListener)_lsnr;
 
  
// We will accept only one incoming connection at a time. So just
     
   // accept the connection and loop until it closes.
 
  
// To handle multiple connections we would need one threads for
  

   // each or async code.
 
   while
(true) {

      var
conn = lsnr.AcceptBluetoothClient();

      var
peer = conn.GetStream();

      SetConnection(peer,

         false
, conn.RemoteEndPoint);
As to BeginAcceptBluetoothClient etc. We use this pattern in the SdpBrowser samples.  Those methods use the standard .NET APM (Async Programming Model) pattern.  Everything MSDN says about that can be used here.  e.g. http://msdn.microsoft.com/en-us/library/ms228963  For AcceptXXXXClient see some (not very helpful -- ignore the use of the ManualResetEvent object) sample code at e.g. http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.beginaccepttcpclient.aspx 

 


      ReadMessagesToEnd(peer);
   }
}

 

May 30, 2012 at 4:22 PM

first of all thanks a lot. I used your suggest to adjust my WPF program..but 

here's the code (WPF):

void ShowValues(string freq) { EventHandler action = delegate {

this.lblfreq.Content = freq;

}; ThreadSafeRun(action); }

 

 

private void ThreadSafeRun(EventHandler action) { if (this.lblfreq.Dispatcher.CheckAccess()) { this.lblfreq.Dispatcher.BeginInvoke(action,null); } else { action(null, null); }

[here's your original code bluetoothchat2]

private void ThreadSafeRun(EventHandler action)
        {
            Control c = this.textBox1;
            if (c.InvokeRequired) {
                c.BeginInvoke(action);
            } else {
                action(null, null);
            }
        }

   void AddMessage(MessageSource source, string message)
        {
            EventHandler action = delegate {
                
                AssertOnUiThread();
                this.textBox1.Text =
                     message + "\r\n"
                    + this.textBox1.Text;
            };
            ThreadSafeRun(action);
        }

 

[if] condition (wpf code) is not true so it doesn't invoke anything...

any ideas?

 


Developer
Jun 3, 2012 at 11:01 PM

Haven't you got the condition backwards. i.e.  ASSERT(Dispatcher.CheckAccess() != Control.InvokeRequired)

So

 private void ThreadSafeRun(EventHandler action)
        {          
           if (!this.lblfreq.Dispatcher.CheckAccess())
            {
                this.lblfreq.Dispatcher.BeginInvoke(action,null);
            }
            else
            {
                action(null, null);
            }
}