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

  • Question

  • I'm building a project with <g class="gr_ gr_11 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="11" id="11">arduino</g> and <g class="gr_ gr_22 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="22" id="22">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_41 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" data-gr-id="41" id="41">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 <g class="gr_ gr_57 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" data-gr-id="57" id="57">data  </g><g class="gr_ gr_58 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" data-gr-id="58" id="58"><g class="gr_ gr_57 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Style multiReplace" data-gr-id="57" id="57">determinate</g>  hours</g>. I also <g class="gr_ gr_71 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" data-gr-id="71" id="71">used  </g><g class="gr_ gr_72 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" data-gr-id="72" id="72"><g class="gr_ gr_71 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Style multiReplace" data-gr-id="71" id="71"><g class="gr_ gr_69 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" data-gr-id="69" id="69">async</g></g>  method</g> 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"



    Tuesday, April 9, 2019 3:16 AM

All replies

  • Hello asl331,

    This forum is for "Discuss general issues about developing applications for Windows." It is Windows API (win32) C++ focused.

    Since you use C# I'll move this thread to the right forum for more professional support.

    Best regards,

    Rita


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 9, 2019 6:11 AM
  • Thanks for your help!
    Tuesday, April 9, 2019 2:23 PM
  • Hi

    Thank you for posting here.

    For your question, could you provide the related code about the following keywords?

    serialPort ,BtnDetectStyle ,device ,GetDataSensor, conf

    It will be better to solve your problem if you provide the information.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 10, 2019 8:38 AM
    Moderator
  • Hello, thanks for the reply, of course, I will provide you with that information.

    This method <g class="gr_ gr_545 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" data-gr-id="545" id="545">BtnDetectStyle</g><g class="gr_ gr_545 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Style multiReplace" data-gr-id="545" id="545"> is</g> only to change the style of a button when the device is paired up.

    private void BtnDetectStyle( string style)
    
            {
                switch (style)
                {
                    case "detect":
                        btnDetect.BackColor = Color.FromArgb(241, 0, 0);
                        btnDetect.Text = "Detectar  ";
                        btnDetect.Enabled = true;
                        break;
                    case "detected":
                        btnDetect.BackColor = Color.FromArgb(56, 228, 6);
                        btnDetect.Text = " OK";
                        btnDetect.Enabled = false;
                        break;
                    default:
                        Console.WriteLine("Default case"); btnDetect.BackColor = Color.FromArgb(241, 0, 0);
                        btnDetect.Text = "Detectar  ";
                        btnDetect.Enabled = true;
                        break;
                }
            }
    

    In this method I configure the serialPort variable, I set the port name and initialize the background job.

    public void GetSerialData()
            {
                Cursor.Current = Cursors.WaitCursor;
                string[] bluetoothInfo = new string[20];
                var worktest = new BackgroundWorker()
                {
                    WorkerReportsProgress = true,
                    WorkerSupportsCancellation = true
                };
    
                BtnDetectStyle("detect");
                serialPort = new SerialPort
                {
                    BaudRate = 9600
                };
                 bluetoothInfo = GetBluetoothPort();
                if (bluetoothInfo.Length > 0)
                { 
                    if (bluetoothInfo[0].Contains(value: device))
                    {
                        serialPort.PortName = bluetoothInfo[1];
                        try {
                            if (!serialPort.IsOpen)
                            {
                                Console.Write("here");
                                serialPort.DtrEnable = true;
                                serialPort.RtsEnable = true;
                                serialPort.Open();
                            }
                           
                            getDataNow = true;
                            BtnDetectStyle("detected");
                            Cursor.Current = Cursors.Default;
                        }
                        catch (Exception e )
                        {
    
                            Console.Write("exception bluee"+e);
    
    
                            getDataNow = false;
                            BtnDetectStyle("detect");
                            Console.write("no device detected")
                        }
                    }
                }
                else 
                {
                   Console.write("no device detected")
    
                    getDataNow = false;
                    BtnDetectStyle("detect");
                }
    
                worktest.DoWork += UpdateData;
                worktest.RunWorkerAsync();
            }
    
    
    

    This method GetDataSensor is to separate the data coming from the serial port.

     private void GetDataSensor(string serialData, out string temperature, out string humidity)
            {
                int position;
                position= serialData.IndexOf(";");
                temperature = serialData.Substring(0, position);
                humidity = serialData.Substring(position+1, serialData.Length - (position + 1));
            }
    device variable is only the name of the Bluetooth device and conf is a class where I get data like a path from files and dates.


    thanks for your help.

    Of course I will provide you with that information
    Of <g class="gr_ gr_219 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Punctuation only-ins replaceWithoutSep" data-gr-id="219" id="219">course</g> I will provide you with that information
    Thursday, April 11, 2019 4:16 AM
  • Hi

    I want to know where it occurs the exception.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 11, 2019 6:42 AM
    Moderator
  • Hi, There is really no exception, what happens is that the window where I show the data is frozen, and the following is shown in the console:

    serial port data 28,00;60,00
    serial port data 28,00;60,00
    serial port data 30,00;61,00
    //at this point i disconnected the device
    The thread 0x2cb8 has exited with code 0 (0x0)
    The thread 0x2c30 has exited with code 0 (0x0)
    The thread 0x22f8 has exited with code 0 (0x0)
    The thread 0x1178 has exited with code 0 (0x0)
    The thread 0x6a4 has exited with code 0 (0x0)
    The thread 0x218c has exited with code 0 (0x0)
    The thread 0x2ea0 has exited with code 0 (0x0)
    The thread 0x10b4 has exited with code 0 (0x0)

    Thursday, April 11, 2019 2:46 PM
  • Hi

    I could not reproduce your problem due to the computer lacking a Bluetooth device. However, you could post in the following place.

    https://stackoverflow.com/questions/tagged/bluetooth

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 12, 2019 7:13 AM
    Moderator