📄 frmconnectprofile.cs
字号:
#region Using directives
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Threading;
#endregion
namespace UI
{
/// <summary>
/// Summary description for frmBluetooth.
/// </summary>
public class frmConnectProfile : System.Windows.Forms.Form
{
private Form parentForm;
private MenuItem menuItemGoToLogin;
private MenuItem menuItemCancel;
private Label lblDeviceInfo;
private ComboBox comboBoxProfile;
private TextBox txtDeviceInfo;
/// <summary>
/// Main menu for the form.
/// </summary>
private System.Windows.Forms.MainMenu mainMenu1;
BTHWrapper.BTHConnector _connector;
Profile.ProfileList _profileList;
Profile.Profile _profile;
public frmConnectProfile(BTHWrapper.BTHConnector inConn, Profile.ProfileList lstProfile)
{
_profile = new Profile.Profile();
_connector = inConn;
_profileList = lstProfile;
InitializeComponent();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
this.parentForm = parentForm;
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItemGoToLogin = new System.Windows.Forms.MenuItem();
this.menuItemCancel = new System.Windows.Forms.MenuItem();
this.lblDeviceInfo = new System.Windows.Forms.Label();
this.comboBoxProfile = new System.Windows.Forms.ComboBox();
this.txtDeviceInfo = new System.Windows.Forms.TextBox();
//
// mainMenu1
//
this.mainMenu1.MenuItems.Add(this.menuItemGoToLogin);
this.mainMenu1.MenuItems.Add(this.menuItemCancel);
//
// menuItemGoToLogin
//
this.menuItemGoToLogin.Text = "Login";
this.menuItemGoToLogin.Click += new System.EventHandler(this.menuItemGoToLogin_Click);
//
// menuItemCancel
//
this.menuItemCancel.Text = "Cancel";
this.menuItemCancel.Click += new System.EventHandler(this.menuItemCancel_Click);
//
// lblDeviceInfo
//
this.lblDeviceInfo.Location = new System.Drawing.Point(16, 88);
this.lblDeviceInfo.Size = new System.Drawing.Size(193, 22);
this.lblDeviceInfo.Text = "Device Info:";
//
// comboBoxProfile
//
this.comboBoxProfile.BackColor = System.Drawing.SystemColors.Menu;
this.comboBoxProfile.Location = new System.Drawing.Point(16, 30);
this.comboBoxProfile.Size = new System.Drawing.Size(207, 33);
this.comboBoxProfile.SelectedIndexChanged += new System.EventHandler(this.comboBoxProfile_SelectedIndexChanged);
//
// txtDeviceInfo
//
this.txtDeviceInfo.Enabled = false;
this.txtDeviceInfo.Font = new System.Drawing.Font("Nina", 9F, System.Drawing.FontStyle.Regular);
this.txtDeviceInfo.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
this.txtDeviceInfo.Location = new System.Drawing.Point(16, 125);
this.txtDeviceInfo.Multiline = true;
this.txtDeviceInfo.ReadOnly = true;
this.txtDeviceInfo.Size = new System.Drawing.Size(207, 118);
//
// frmConnectProfile
//
this.ClientSize = new System.Drawing.Size(240, 266);
this.Controls.Add(this.lblDeviceInfo);
this.Controls.Add(this.comboBoxProfile);
this.Controls.Add(this.txtDeviceInfo);
this.Menu = this.mainMenu1;
this.Text = "Choose a Profile";
this.Load += new System.EventHandler(this.frmConnect_Load);
}
#endregion
private void menuItemCancel_Click(object sender, EventArgs e)
{
this.Dispose();
}
private void menuItemGoToLogin_Click(object sender, EventArgs e)
{
string strACK="";
string tmp = "";
Profile.Profile p = new Profile.Profile(); ;
if (comboBoxProfile.SelectedItem != null)
{
if (_connector != null)
{
_connector.Dispose();
_connector = new BTHWrapper.BTHConnector();
}
_connector.InitialiseBluetooth();
_connector.InitialiseSocket();
p = (Profile.Profile)comboBoxProfile.SelectedItem;
System.Threading.Timer tm = new System.Threading.Timer(new System.Threading.TimerCallback(ResetConnection), 3, 10000, System.Threading.Timeout.Infinite);
_connector.Connect(p.Address);
tm.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
if (!_connector.IsDisconnected())
{
//do
//{
_connector.SendLoginMessage("1100" + p.Login);
Thread.Sleep(500);
_connector.SendLoginMessage("1101" +p.Password);
Thread.Sleep(500);
_connector.SendLoginMessage("1110");
Thread.Sleep(500);
strACK = _connector.Recieve();
if (strACK.Substring(0, 4).Equals("true"))
{
_connector.SendLoginMessage("1111");
//Global.ShowCaution("Login success\nWaiting..", "Login success");
_connector.TerminateSocket();
_connector.Dispose();
}
else
{
Global.ShowCaution("Login and password error", "Invaild Login");
_connector.TerminateSocket();
_connector.Dispose();
return;
}
//strACK = _connector.Recieve();
//} while (!strACK.Substring(0, 2).Equals("OK"));
_connector = new BTHWrapper.BTHConnector();
_connector.InitialiseBluetooth();
_connector.InitialiseSocket();
//connet to server application
do
{
_connector.Connect(p.Address);
} while (_connector.IsDisconnected());
frmRemoteControl frmRC = new frmRemoteControl(_connector);
frmRC.Show();
this.Dispose();
}
}
else
{
Global.ShowCaution("You have to select a profile to connect.", "Profile error");
}
/*
frmRemoteControl frmRC = new frmRemoteControl(_connector);
frmRC.Show();
this.Dispose();*/
}
private void ResetConnection(object b)
{
Global.ShowCaution("Cannot connect to device.", "Connecting error");
_connector.Dispose();
//_connector = new BTHWrapper.BTHConnector();
//_connector.InitialiseSocket();
}
private void frmConnect_Load(object sender, EventArgs e)
{
_profileList.LoadFile(Global.PROFILE_PATH);
comboBoxProfile.DataSource = _profileList.List;
comboBoxProfile.DisplayMember = "ProfileName";
}
private void comboBoxProfile_SelectedIndexChanged(object sender, EventArgs e)
{
Profile.Profile p = new Profile.Profile();
p = (Profile.Profile)comboBoxProfile.SelectedItem;
txtDeviceInfo.Text = "Device name: "+ p.DeviceName + "\r\n";
txtDeviceInfo.Text += "Address: " + p.Address + "\r\n";
txtDeviceInfo.Text += "Login: " + p.Login + "\r\n";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -