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!