Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Android.App;
- using Android.Widget;
- using Android.OS;
- using Android.Net.Wifi;
- using Android.Content;
- using System.Collections.Generic;
- using Android.Net;
- using System.Net;
- using System;
- using Java.Lang.Reflect;
- using Java.Util;
- using Java.IO;
- using Android.Util;
- namespace Korshunov05F
- {
- [Activity(Label = "Korshunov05F", MainLauncher = true)]
- public class MainActivity : Activity
- {
- Dictionary<ScanResult, string> scanResults =
- new Dictionary<ScanResult, string>();
- public static ArrayAdapter<string> adapter;
- public static List<string> mItems;
- private bool start = true;
- public static TextView textView;
- public Button button;
- public ListView listView;
- public EditText textField;
- private Reciver reciver;
- private static WifiManager wifi;
- public static List<string> WiFiNetworks;
- protected override void OnCreate(Bundle savedInstanceState)
- {
- SetContentView(Resource.Layout.activity_main);
- base.OnCreate(savedInstanceState);
- button = FindViewById<Button>(Resource.Id.button1);
- listView = FindViewById<ListView>(Resource.Id.listView1);
- textField = FindViewById<EditText>(Resource.Id.editText1);
- button.Click += Button_Click;
- mItems = new List<string>();
- listView.ItemClick += ListView_ItemClick;
- wifi = (Android.Net.Wifi.WifiManager)GetSystemService(Context.WifiService);
- reciver = new Reciver();
- reciver.reciver += Reciver_Reciver;
- System.Console.Write("Hello");
- }
- void ListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
- {
- List<ScanResult> results = new List<ScanResult>();
- Dictionary<ScanResult, string>.KeyCollection keyColl =
- scanResults.Keys;
- foreach (ScanResult r in keyColl)
- {
- if (r.Ssid != null && r.Ssid.Equals(mItems[e.Position]) && r.Capabilities.ToUpper().Contains("WPA"))
- {
- ConnectToNetworkWPA(mItems[e.Position], textField.Text);
- }
- else if (r.Ssid != null && r.Ssid.Equals(mItems[e.Position]) && r.Capabilities.ToUpper().Contains("WEP"))
- {
- ConnectToNetworkWEP(mItems[e.Position], textField.Text);
- }
- else
- {
- ConnectToNetworkOPEN(mItems[e.Position]);
- }
- }
- }
- void Reciver_Reciver(Context arg1, Intent arg2)
- {
- mItems = new List<string>();
- List<string> allInfo = new List<string>();
- IList<ScanResult> results = wifi.ScanResults;
- foreach (ScanResult result in results)
- {
- if (result.Ssid.Length >= 1)
- {
- int level = result.Level;
- if (level >= -50)
- {
- level = 100;
- }
- else
- {
- if (level <= -100)
- {
- level = 0;
- }
- else
- {
- level = 2 * (level + 100);
- }
- }
- }
- allInfo.Add(result.Ssid + " - SSID, " + level + "% " + System.Environment.NewLine);
- mItems.Add(result.Ssid);
- scanResults.Add(result, result.Ssid);
- }
- listView.Adapter = new ArrayAdapter<string>(arg1, Android.Resource.Layout.SimpleListItem1, allInfo);
- }
- public void GetWifiNetworks()
- {
- RegisterReceiver(reciver, new IntentFilter(WifiManager.ScanResultsAvailableAction));
- wifi.StartScan();
- }
- void Button_Click(object sender, EventArgs eventArgs)
- {
- if (start)
- {
- GetWifiNetworks();
- start = false;
- button.Text = "Стоп";
- }
- else
- {
- start = true;
- button.Text = "Искать";
- }
- }
- public class Reciver : BroadcastReceiver
- {
- public event Action<Context, Intent> reciver;
- public override void OnReceive(Context context, Intent intent)
- {
- this.reciver(context, intent);
- }
- }
- public Boolean ConnectToNetworkWPA(String networkSSID, String password)
- {
- try
- {
- WifiConfiguration conf = new WifiConfiguration();
- conf.Ssid = "\"" + networkSSID + "\"";
- conf.PreSharedKey = "\"" + password + "\"";
- conf.StatusField = WifiConfiguration.Status.Enabled;
- conf.AllowedGroupCiphers.NextSetBit((int)WifiConfiguration.GroupCipher.Tkip);
- conf.AllowedGroupCiphers.Set((int)WifiConfiguration.GroupCipher.Tkip);
- conf.AllowedGroupCiphers.Set((int)WifiConfiguration.GroupCipher.Ccmp);
- conf.AllowedKeyManagement.Set((int)WifiConfiguration.KeyMgmt.WpaPsk);
- conf.AllowedPairwiseCiphers.Set((int)WifiConfiguration.PairwiseCipher.Tkip);
- conf.AllowedPairwiseCiphers.Set((int)WifiConfiguration.PairwiseCipher.Ccmp);
- wifi.AddNetwork(conf);
- IList<WifiConfiguration> list = wifi.ConfiguredNetworks;
- foreach (WifiConfiguration i in list)
- {
- if (i.Ssid != null && i.Ssid.Equals("\"" + networkSSID + "\""))
- {
- wifi.Disconnect();
- wifi.EnableNetwork(i.NetworkId, true);
- wifi.Reconnect();
- break;
- }
- }
- return true;
- }
- catch (Exception ex)
- {
- alertManager();
- return false;
- }
- }
- public Boolean ConnectToNetworkWEP(String networkSSID, String password)
- {
- try
- {
- WifiConfiguration conf = new WifiConfiguration();
- conf.Ssid = "\"" + networkSSID + "\"";
- conf.WepKeys[0] = "\"" + password + "\"";
- conf.AllowedKeyManagement.Set((int)WifiConfiguration.KeyMgmt.None);
- conf.AllowedGroupCiphers.Set((int)WifiConfiguration.AuthAlgorithm.Open);
- conf.AllowedGroupCiphers.Set((int)WifiConfiguration.AuthAlgorithm.Shared);
- int networkId = wifi.AddNetwork(conf);
- if (networkId == -1)
- {
- conf.WepKeys[0] = password;
- networkId = wifi.AddNetwork(conf);
- }
- IList<WifiConfiguration> list = wifi.ConfiguredNetworks;
- foreach (WifiConfiguration i in list)
- {
- if (i.Ssid != null && i.Ssid.Equals("\"" + networkSSID + "\""))
- {
- wifi.Disconnect();
- wifi.EnableNetwork(i.NetworkId, true);
- wifi.Reconnect();
- break;
- }
- }
- return true;
- }
- catch (Exception ex)
- {
- alertManager();
- return false;
- }
- }
- public Boolean ConnectToNetworkOPEN(String networkSSID)
- {
- try
- {
- WifiConfiguration conf = new WifiConfiguration();
- conf.Ssid = "\"" + networkSSID + "\"";
- //conf.PreSharedKey = null;
- conf.AllowedKeyManagement.Set((int)WifiConfiguration.KeyMgmt.None);
- //conf.AllowedProtocols.Set((int)WifiConfiguration.Protocol.Rsn);
- //conf.AllowedProtocols.Set((int)WifiConfiguration.Protocol.Wpa);
- //conf.AllowedAuthAlgorithms.Clear();
- //conf.AllowedPairwiseCiphers.Set((int)WifiConfiguration.PairwiseCipher.Ccmp);
- //conf.AllowedPairwiseCiphers.Set((int)WifiConfiguration.PairwiseCipher.Tkip);
- //conf.AllowedGroupCiphers.Set((int)WifiConfiguration.GroupCipher.Wep40);
- //conf.AllowedGroupCiphers.Set((int)WifiConfiguration.GroupCipher.Wep104);
- //conf.AllowedGroupCiphers.Set((int)WifiConfiguration.GroupCipher.Ccmp);
- //conf.AllowedGroupCiphers.Set((int)WifiConfiguration.GroupCipher.Tkip);
- IList<WifiConfiguration> list = wifi.ConfiguredNetworks;
- foreach (WifiConfiguration i in list)
- {
- if (i.Ssid != null && i.Ssid.Equals("\"" + networkSSID + "\""))
- {
- wifi.Disconnect();
- wifi.EnableNetwork(i.NetworkId, true);
- wifi.Reconnect();
- break;
- }
- }
- return true;
- }
- catch (Exception ex)
- {
- alertManager();
- return false;
- }
- }
- public void alertManager()
- {
- AlertDialog.Builder alert = new AlertDialog.Builder(this);
- alert.SetTitle("Ошибка");
- alert.SetMessage("Ошибка подключения");
- alert.SetNegativeButton("Cancel", (senderAlert, args) => {
- Toast.MakeText(this, "Cancelled!", ToastLength.Short).Show();
- });
- Dialog dialog = alert.Create();
- dialog.Show();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement