How detect correctly when bluetooth device is disconnected and sub process associate end?

  • Thread starter Thread starter asl331
  • Start date Start date
A

asl331

Guest
I'm building a project with <g class="gr_ gr_9 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="9" id="9">arduino</g> and <g class="gr_ gr_10 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="10" id="10">bluetooth</g> module, the device sends data for the serial port and the software building in c# received the data correctly, I need to detect when the device is disconnected and show a message to the user , but when I disconnect the device, the application don't launch <g class="gr_ gr_8 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" data-gr-id="8" id="8">any</g> exception and no one of the error messages is showing.

I'm using Background workers, I try to detect if the device is disconnected in an infinite loop because I need to show data constantly and save that data <g class="gr_ gr_7 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" data-gr-id="7" id="7">in determinate</g> hours. I also used <g class="gr_ gr_12 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" data-gr-id="12" id="12">async</g> method but the result is the same.

getDataNow=true;
var worktest = new BackgroundWorker()
{
WorkerReportsProgress = true,
WorkerSupportsCancellation = true
};

worktest.DoWork += UpdateData;
worktest.RunWorkerAsync();



private void UpdateData(object sender, DoWorkEventArgs e)
{
while (getDataNow)
{
try
{
string serialData = "";
Console.WriteLine("status " + serialPort.IsOpen);
if (serialPort.IsOpen == true)
{
serialData = serialPort.ReadLine();

}
else
{
BtnDetectStyle("detect");
MessageBox.Show("El dispositivo ha sido desconectado", device,
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
getDataNow = false;
}

Console.WriteLine("serial port data " + serialData);

if (serialData.Length > 0)
{
GetDataSensor(serialData, out string temperature, out string humidity);
this.Invoke((MethodInvoker)delegate ()
{
this.lblHum.Text = humidity;
this.lblTemp.Text = temperature;
});

Console.Write("7");

if (DateTime.Now.ToString("h:mm tt") == conf.GetDateMorning)
{
Thread.Sleep(60000);
}

if (DateTime.Now.ToString("h:mm tt") == conf.GetDateAfternoon)
{
Thread.Sleep(60000);
}
}
}
catch (TimeoutException te)
{
Console.WriteLine(te);
}
}
}

I expect an exception or "the device is disconnected " message, but I get "Thread xx ended with code 0"

Continue reading...
 
Back
Top