.NET Components for Mobility

How to verify if bluetooth connection is alive?

Last post 10-27-2008 1:39 PM by Tonino Palmisano. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 10-16-2008 11:56 AM

    How to verify if bluetooth connection is alive?

    Hi,

    the scenrio is this: a bluetooth module search for a device, a pocket pc (windows mobile 6.1 professional based); the ppc is in listening for incoming connections. I'm writing this code to perform the listening of incoming connection:

    public void EffettuaPairing()
    {
      //dico ke sto in pairing
      inPairing = true;
     
    if (frmMain.spPort.IsOpen)
      {
       
    frmMain.spPort.Close();
     
    }
     
    //Verifico che ci sia supporto bluetooth sul dispositivo
     
    if (BluetoothRadio.PrimaryRadio != null)
     
    {
       
    //controllo se il bluetooth è attivato
       
    if (!(BluetoothRadio.PrimaryRadio.HardwareStatus == HardwareStatus.Running))
        {
         
    //Provo ad attivare il bluetooth
         
    BluetoothRadio.PrimaryRadio.Mode = RadioMode.Discoverable;
        }
       
    //Imposto l'indirizzo del modulo del politecnico
       
    addr = BluetoothAddress.Parse("XXXXXXXXXXXX");
       
    //Revoco il pin
       
    BluetoothSecurity.RevokePin(addr);
       
    //Istanzio il listener sul servizio di serial port
       
    BTlistener = new BluetoothListener(service);
       
    //Faccio partire il listener
       
    BTlistener.Start();
       
    //ciclo finchè non avviene il pairing
       
    while (!paired)
        {
         
    //Rilevo i dispositivi che richiedono il pin
          incoming = BluetoothSecurity.GetPinRequest();
          //Se ce ne sono...
          if (incoming != null)
          {
            //...controllo che sia il dispositivo giusto
            if (incoming.ToString() == addr.ToString())
            {
              //Se è quello giusto effettuo il pairing
              //Imposto il PIN
              BluetoothSecurity.SetPin(incoming, pin);
              //Open serial port
              frmMain.spPort.Open();
              paired =
    true;
              timerPairing.Enabled =
    true;
            }
          }
        }
      }
      inPairing =
    false;
      return;
    }

    In the main form i have the declaration of spPort as System.IO.Ports.SerialPort (Port Name "COM6") and the event handler spPort_DataRecieved, I parsing the bytes that arrives through the serial port and all works fine. My problem is that i need to verify periodically (with a timer) if the connection is alive: if the connection is down (because the ppc isn't in the range of bluetooth module, or because of any other problem), i need to do the pairing with "EffettuaPairing()". How can I do to verify if the connection is alive?

    Thanks at all!

  • 10-22-2008 9:24 AM In reply to

    Re: How to verify if bluetooth connection is alive?

    I'm confused.  You use a BluetoothListener to receive the incoming connection from the hardware module, but then use a serial port to communicate with it?  Or is the serial port connecting to a third device?

    If you used a connection from the BluetoothListener then you'd know immediately when the device had gone away.  You'd get an error on that (socket) connection then (well the next time you did a read or write operation -- and you could set a background read or write to know immediately).

    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.
  • 10-27-2008 1:39 PM In reply to

    Re: How to verify if bluetooth connection is alive?

    Thanks for reply,

    I hve some question about your reply:

    1. What function should I use to listen incoming connection from the specified bluetooth address? Can I use AcceptBluetoothClient() ? Where? In the internal cicle after the control on incoming address?
    2. After the use of AcceptBluetoothClient() or similar, do I use BluetoothClient class methods to connect the devices?
    3. I use the serial port spPort because I intercept the data received event to do the parsing of arrived bytes; If i don't use the serial port, what can I use?

    I'm not an expert of 32Feet.Net...

    Thanks a lot!

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