private void UpdateDataGridView()
 {
     if (InvokeRequired)
     {
         UpdateDataGridViewDelegate method = new UpdateDataGridViewDelegate(UpdateDataGridView);
         Invoke(method);
         return;
     }
     this.folderTableAdapter1.Fill(this.folderlockDataSet4.folder);
     dataGridView1.Update();
     dataGridView1.Refresh();
 }
        private void WaitingBluetooth()
        {
            try {
                connectDatabase();
                string          stm = "SELECT * FROM folderlock.folder";
                MySqlCommand    cmd = new MySqlCommand(stm, conn);
                MySqlDataReader rdr = null;
                rdr = cmd.ExecuteReader();
                for (i_strArray = 0; rdr.Read(); i_strArray++)
                {
                    strArray[i_strArray] = new string[4];
                    for (int j = 0; j <= 3; j++)
                    {
                        strArray[i_strArray][j] = rdr.GetString(j);
                    }
                }
                bc = new BluetoothClient();
                BluetoothDeviceInfo[] array = bc.DiscoverDevicesInRange();
                getMacDevice();
                bool flag = false;
                BluetoothDeviceInfo         bd;
                ChangeScanDeviceBtnDelegate changeScanbtn  = new ChangeScanDeviceBtnDelegate(ChangeEnabledScanDeviceBtn);
                UpdateDataGridViewDelegate  updateDataGrid = new UpdateDataGridViewDelegate(UpdateDataGridView);
chay:
                foreach (BluetoothDeviceInfo bdi in array)
                {
                    try
                    {
                        if (bdi.DeviceAddress.ToString().Equals(deviceMac))
                        {
                            bd = bdi;
                            if (bc.Connected == false)
                            {
                                bc.Connect(new BluetoothEndPoint(bd.DeviceAddress, InTheHand.Net.Bluetooth.BluetoothService.Handsfree)); //.BluetoothService.SerialPort
                                bd.SetServiceState(InTheHand.Net.Bluetooth.BluetoothService.Handsfree, true);                            //.BluetoothService.SerialPort
                            }
                            bd.Refresh();
                            if (bd.Connected)
                            {
                                flag         = true;
                                flag_connect = true;
                                di           = new ShowDialogDelegate(ShowDialog);
                                di.Invoke(true);
                                ObjectDelegate del = new ObjectDelegate(UpdateForm);
                                del.Invoke(bd.DeviceName.ToString(), bd.ClassOfDevice.MajorDevice.ToString(), bd.Authenticated.ToString());
                                ChangeSizeMainFormDelegate csd = new ChangeSizeMainFormDelegate(ChangeSizeForm);
                                csd.Invoke(463, 383);
                                string bd_DeviceMac = bd.DeviceAddress.ToString();
                                if (connectDatabase())
                                {
                                    try
                                    {
                                        bool   flagunlock = true;
                                        string patherr    = "";
                                        int    i          = 0;
                                        while (i < i_strArray)
                                        {
                                            if (strArray[i][2].Equals("True") && strArray[i][3].Equals("True")) //strArray[i][2]: Thuộc tính Auto Lock, strArray[i][3]: Thuộc tính Locked
                                            {
                                                if (!unlockFolder(strArray[i][0], strArray[i][1]))
                                                {
                                                    flagunlock = false;
                                                    patherr   += strArray[i][1] + "\n";
                                                }
                                            }
                                            i++;
                                        }
                                        if (!flagunlock)
                                        {
                                            MessageBox.Show("Không thể mở khóa thành công các thư mục sau: \n" + patherr, "Không thành công", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                        }
                                        updateDataGrid();
                                        SHChangeNotify(0x8000000, 0x1000, IntPtr.Zero, IntPtr.Zero); //refresh Desktop and Windows Explorer
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show("Không thể kết nối với Database4", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    }
                                    changeScanbtn.Invoke(true, "Stop...");
                                    while (true)
                                    {
                                        //if (flag_connect == true) //flag_connect: kiểm tra nếu là true thì refesh, false thì không refresh
                                        bd.Refresh();
                                        bool bd_Connected = bd.Connected;

                                        if (flag_connect == true && bd_Connected == false) //flag_connect:
                                        {
                                            //không tìm thấy thiết bị, khóa tất cả các thư mục
                                            //update Form status-name-type
                                            //del.Invoke(bd.DeviceName.ToString(), bd.ClassOfDevice.MajorDevice.ToString(), bd.Authenticated.ToString());
                                            del.Invoke("None", "None", "False");

                                            di = new ShowDialogDelegate(ShowDialog);
                                            di.Invoke(false);
                                            try
                                            {
                                                csd = new ChangeSizeMainFormDelegate(ChangeSizeForm);
                                                csd.Invoke(232, 187);
                                                UpdateDatabase();
                                                bool   flag_success = true;
                                                string pathError    = "";
                                                int    i            = 0;
                                                while (i < i_strArray)
                                                {
                                                    if (strArray[i][2].Equals("True") && strArray[i][3].Equals("False"))//True
                                                    {
                                                        if (!lockFolder(strArray[i][0], strArray[i][1]))
                                                        {
                                                            flag_success = false;
                                                            pathError   += strArray[i][1] + "\n";
                                                        }
                                                    }
                                                    i++;
                                                }
                                                if (!flag_success)
                                                {
                                                    MessageBox.Show("Không thể khóa thành công tất cả các thư mục sau:\n" + pathError, "Không thành công", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                                }
                                                updateDataGrid();
                                                SHChangeNotify(0x8000000, 0x1000, IntPtr.Zero, IntPtr.Zero); //refresh Desktop and Windows Explorer
                                                flag_connect = false;
                                            }
                                            catch (Exception ex)
                                            {
                                                MessageBox.Show("Không thể kết nối với Database3", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                            }
                                        }
                                        if (flag_connect == false && bd_Connected == true && bd_DeviceMac.Equals(deviceMac))
                                        {
                                            //tìm thấy thiết bị, mở khóa tất cả các thư mục
                                            //update Form status-name-type
                                            del.Invoke(bd.DeviceName.ToString(), bd.ClassOfDevice.MajorDevice.ToString(), bd.Authenticated.ToString());
                                            di = new ShowDialogDelegate(ShowDialog);
                                            di.Invoke(true);
                                            try
                                            {
                                                csd = new ChangeSizeMainFormDelegate(ChangeSizeForm);
                                                csd.Invoke(463, 383);

                                                UpdateDatabase();
                                                bool   flag_success = true;
                                                string pathError    = "";
                                                int    i            = 0;
                                                while (i < i_strArray)
                                                {
                                                    if (strArray[i][2].Equals("True") && strArray[i][3].Equals("True"))
                                                    {
                                                        if (!unlockFolder(strArray[i][0], strArray[i][1]))
                                                        {
                                                            flag_success = false;
                                                            pathError   += strArray[i][1] + "\n";
                                                        }
                                                    }
                                                    i++;
                                                }
                                                if (!flag_success)
                                                {
                                                    MessageBox.Show("Không thể khóa thành công tất cả các thư mục sau:\n" + pathError, "Không thành công", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                                }
                                                updateDataGrid();
                                                SHChangeNotify(0x8000000, 0x1000, IntPtr.Zero, IntPtr.Zero); //Refresh Desktop and Windows Explorer
                                                flag_connect = true;
                                            }
                                            catch (Exception ex)
                                            {
                                                MessageBox.Show("Không thể kết nối với Database2", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                            }
                                        }

                                        if (flag_connect == false && bd_Connected == false)
                                        {
                                            bc    = null;
                                            bc    = new BluetoothClient();
                                            array = bc.DiscoverDevicesInRange();
                                            foreach (BluetoothDeviceInfo bd_t in array)
                                            {
                                                if (bd_t.DeviceAddress.ToString().Equals(deviceMac))
                                                {
                                                    bd = bd_t;
                                                    bc.Connect(new BluetoothEndPoint(bd.DeviceAddress, InTheHand.Net.Bluetooth.BluetoothService.Handsfree)); //.BluetoothService.SerialPort
                                                    bd.SetServiceState(InTheHand.Net.Bluetooth.BluetoothService.Handsfree, true);                            //.BluetoothService.SerialPort
                                                }
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    MessageBox.Show("Không thể kết nối với Database1", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                }
                            }
                            else
                            {
                                MessageBox.Show("Thiết bị chưa được kết nối", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                bc   = null;
                                flag = false;
                            }
                            flag = true;
                            break;
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
                if (!flag)
                {
                    //MessageBox.Show("Không tìm thấy đúng thiết bị cần kết nối", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    //changeScanbtn(true, "Scan Device...");
                    //StopThreadDelegate sthread = new StopThreadDelegate(stopThread);
                    //sthread.Invoke();
                    //return;
                    goto chay;
                }
                //else {
                changeScanbtn.Invoke(true, "stop");
                StopThreadDelegate stpth = new StopThreadDelegate(stopThread);
                stpth.Invoke();
                //}
            }
            catch (Exception ex)
            {
                int i = 0;
                while (i < i_strArray)
                {
                    if (strArray[i][2].Equals("True") && strArray[i][3].Equals("False"))//True
                    {
                        lockFolder(strArray[i][0], strArray[i][1]);
                    }
                    i++;
                }
            }
        }