Answer the question
In order to leave comments, you need to log in
Problem connecting to WiFi modem C#?
In general, I am doing laboratory work at the university, a program for Windows, with the search for Internet networks and further connection to them, it finds networks, connects, but when the question arises of connecting to the modem mode of the phone (a la iPhone (Alexander)) the program stops executing the script (I pointlessly click on the connect button and nothing happens). Thanks to everyone who volunteers
to help me!
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using NativeWifi;
using SimpleWifi;
namespace IvanovMRLab2
{
public partial class Form1 : Form
{
private static Wifi wifi;
List<AccessPoint> aps;
public Form1()
{
InitializeComponent();
}
private void btnRefresh_Click(object sender, EventArgs e)
{
buttonCON.Enabled = false;
listNet.Items.Clear();
comboBox1.Items.Clear();
wifi = new Wifi();
aps = wifi.GetAccessPoints();
WlanClient client = new WlanClient();
foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
{
Wlan.WlanAvailableNetwork[] wlanBssEntries = wlanIface.GetAvailableNetworkList(0);
listNet.Items.Clear();
foreach (Wlan.WlanAvailableNetwork network in wlanBssEntries)
{
ListViewItem listItemWifi = new ListViewItem();
listItemWifi.Text = System.Text.Encoding.UTF8.GetString(network.dot11Ssid.SSID).Trim((char)0);
listItemWifi.SubItems.Add(network.wlanSignalQuality.ToString() + "%");
listItemWifi.SubItems.Add(network.dot11DefaultAuthAlgorithm.ToString().Trim((char)0));
listItemWifi.SubItems.Add(network.dot11DefaultCipherAlgorithm.ToString().Trim((char)0));
listItemWifi.ImageIndex = 0;
listNet.Items.Add(listItemWifi);
comboBox1.Items.Add(listItemWifi.Text);
}
}
lbl_Status.Visible = true;
comboBox1.Visible = true;
textBox1.Visible = true;
buttonCON.Visible = true;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
buttonCON.Enabled = true;
}
private void buttonCF_Click(object sender, EventArgs e)
{
if (wifi.ConnectionStatus == WifiStatus.Connected)
{
wifi.Disconnect();
lbl_Status.Text = "Соединение разорвано.";
}
}
private bool connectToWifi(AccessPoint ap, string password)
{
AuthRequest authRequest = new AuthRequest(ap);
authRequest.Password = password;
return ap.Connect(authRequest);
}
private void buttonCON_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex >= 0)
{
foreach (AccessPoint ap in aps)
{
if (comboBox1.SelectedItem.ToString() == ap.Name)
{
if (ap.IsSecure)
{
if (textBox1.Text.Length > 7)
{
if (connectToWifi(ap, textBox1.Text))
{
lbl_Status.Text = "Подключение успешно.";
buttonCF.Visible = true;
buttonCF.Enabled = true;
}
else
lbl_Status.Text = "Не удалось подключиться.";
}
else
lbl_Status.Text = "Введите пароль ( =< 8 символов)";
}
else
{
if (connectToWifi(ap, "12345678"))
{
lbl_Status.Text = "Подключение успешно.";
buttonCF.Visible = true;
buttonCF.Enabled = true;
}
else
lbl_Status.Text = "Не удалось подключиться.";
}
}
}
}
else
lbl_Status.Text = "Выберите сеть для подключения.";
}
private void listNet_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question