BTool.DeviceTabsForm.btnGATTReadValue_Click C# (CSharp) Method

btnGATTReadValue_Click() private method

private btnGATTReadValue_Click ( object sender, EventArgs e ) : void
sender object
e System.EventArgs
return void
        private void btnGATTReadValue_Click(object sender, EventArgs e)
        {
            bool flag = false;
            if (cbReadType.SelectedIndex == 0)
            {
                HCICmds.GATTCmds.GATT_ReadCharValue gattReadCharValue = new HCICmds.GATTCmds.GATT_ReadCharValue();
                tbReadValue.Tag = string.Empty;
                tbReadValue.Text = "";
                tbReadStatus.Text = "Reading...";
                try
                {
                    gattReadCharValue.connHandle = Convert.ToUInt16(tbReadConnHandle.Text.Trim(), 16);
                }
                catch (Exception ex)
                {
                    string msg = string.Format("Invalid Connection Handle\nFormat: 0x0000\n\n{0}\n", ex.Message);
                    msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, msg);
                    tbReadConnHandle.Focus();
                    flag = true;
                }
                try
                {
                    gattReadCharValue.handle = Convert.ToUInt16(tbReadAttrHandle.Text.Trim(), 16);
                }
                catch (Exception ex)
                {
                    string msg = string.Format("Invalid Characteristic Value Handle(s)\nFormat: 0x0000\n\n{0}\n", ex.Message);
                    msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, msg);
                    tbReadAttrHandle.Focus();
                    flag = true;
                }
                if (!flag)
                    devForm.sendCmds.SendGATT(gattReadCharValue, TxDataOut.CmdTypes.General, (SendCmds.SendCmdResult)null);
                else
                    tbReadStatus.Text = "Error!!!";
            }
            else if (cbReadType.SelectedIndex == 1)
            {
                HCICmds.GATTCmds.GATT_ReadUsingCharUUID readUsingCharUuid = new HCICmds.GATTCmds.GATT_ReadUsingCharUUID();
                tbReadValue.Tag = string.Empty;
                tbReadValue.Text = "";
                tbReadStatus.Text = "Reading...";
                try
                {
                    readUsingCharUuid.connHandle = Convert.ToUInt16(tbReadConnHandle.Text.Trim(), 16);
                }
                catch (Exception ex)
                {
                    string msg = string.Format("Invalid Connection Handle\nFormat: 0x0000\n\n{0}\n", ex.Message);
                    msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, msg);
                    tbReadConnHandle.Focus();
                    flag = true;
                }
                try
                {
                    readUsingCharUuid.startHandle = Convert.ToUInt16(tbReadStartHandle.Text.Trim(), 16);
                }
                catch (Exception ex)
                {
                    string msg = string.Format("Invalid Start Handle\nFormat: 0x0000\n\n{0}\n", ex.Message);
                    msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, msg);
                    tbReadStartHandle.Focus();
                    flag = true;
                }
                try
                {
                    readUsingCharUuid.endHandle = Convert.ToUInt16(tbReadEndHandle.Text.Trim(), 16);
                }
                catch (Exception ex)
                {
                    string msg = string.Format("Invalid End Handle\nFormat: 0x0000\n\n{0}\n", ex.Message);
                    msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, msg);
                    tbReadEndHandle.Focus();
                    flag = true;
                }
                try
                {
                    switch (tbReadUUID.Text.Length)
                    {
                        case 5:
                        case 47:
                            readUsingCharUuid.type = tbReadUUID.Text;
                            break;
                        default:
                            throw new ArgumentOutOfRangeException();
                    }
                }
                catch (Exception ex)
                {
                    string msg = string.Format("Invalid UUID Entry.\nFormat Is Either 00:00 or 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00\n\n{0}\n", ex.Message);
                    msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, msg);
                    tbReadUUID.Focus();
                    flag = true;
                }
                if (!flag)
                    devForm.sendCmds.SendGATT(readUsingCharUuid);
                else
                    tbReadStatus.Text = "Error!!!";
            }
            else if (cbReadType.SelectedIndex == 2)
            {
                HCICmds.GATTCmds.GATT_ReadMultiCharValues readMultiCharValues = new HCICmds.GATTCmds.GATT_ReadMultiCharValues();
                tbReadValue.Tag = string.Empty;
                tbReadValue.Text = "";
                tbReadStatus.Text = "Reading...";
                try
                {
                    readMultiCharValues.connHandle = Convert.ToUInt16(tbReadConnHandle.Text.Trim(), 16);
                }
                catch (Exception ex)
                {
                    string msg = string.Format("Invalid Connection Handle\nFormat: 0x0000\n\n{0}\n", ex.Message);
                    msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, msg);
                    tbReadConnHandle.Focus();
                    flag = true;
                }
                try
                {
                    readMultiCharValues.handles = tbReadAttrHandle.Text.Trim();
                }
                catch (Exception ex)
                {
                    string msg = string.Format("Invalid Characteristic Value Handle(s)\nFormat: 0x0001;0x0002\n\n{0}\n", ex.Message);
                    msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, msg);
                    tbReadAttrHandle.Focus();
                    flag = true;
                }
                if (!flag)
                    devForm.sendCmds.SendGATT(readMultiCharValues);
                else
                    tbReadStatus.Text = "Error!!!";
            }
            else
            {
                if (cbReadType.SelectedIndex != 3)
                    return;
                HCICmds.GATTCmds.GATT_DiscCharsByUUID gattDiscCharsByUuid = new HCICmds.GATTCmds.GATT_DiscCharsByUUID();
                tbReadValue.Tag = string.Empty;
                tbReadValue.Text = "";
                tbReadStatus.Text = "Reading...";
                try
                {
                    gattDiscCharsByUuid.connHandle = Convert.ToUInt16(tbReadConnHandle.Text.Trim(), 16);
                }
                catch (Exception ex)
                {
                    string msg = string.Format("Invalid Connection Handle\nFormat: 0x0000\n\n{0}\n", ex.Message);
                    msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, msg);
                    tbReadConnHandle.Focus();
                    flag = true;
                }
                try
                {
                    gattDiscCharsByUuid.startHandle = Convert.ToUInt16(tbReadStartHandle.Text.Trim(), 16);
                }
                catch (Exception ex)
                {
                    string msg = string.Format("Invalid Start Handle\nFormat: 0x0000\n\n{0}\n", ex.Message);
                    msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, msg);
                    tbReadStartHandle.Focus();
                    flag = true;
                }
                try
                {
                    gattDiscCharsByUuid.endHandle = Convert.ToUInt16(tbReadEndHandle.Text.Trim(), 16);
                }
                catch (Exception ex)
                {
                    string msg = string.Format("Invalid End Handle\nFormat: 0x0000\n\n{0}\n", ex.Message);
                    msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, msg);
                    tbReadEndHandle.Focus();
                    flag = true;
                }
                try
                {
                    switch (tbReadUUID.Text.Length)
                    {
                        case 5:
                        case 47:
                            gattDiscCharsByUuid.type = tbReadUUID.Text.Trim();
                            break;
                        default:
                            throw new ArgumentOutOfRangeException();
                    }
                }
                catch (Exception ex)
                {
                    string msg = string.Format("Invalid UUID Entry.\nFormat Is Either 00:00 or 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00\n\n{0}\n", ex.Message);
                    msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, msg);
                    tbReadUUID.Focus();
                    flag = true;
                }
                if (!flag)
                    devForm.sendCmds.SendGATT(gattDiscCharsByUuid);
                else
                    tbReadStatus.Text = "Error!!!";
            }
        }
DeviceTabsForm