Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.25 KB | None | 0 0
  1. using Android.App;
  2. using Android.Widget;
  3. using Android.OS;
  4. using Android.Net.Wifi;
  5. using Android.Content;
  6. using System.Collections.Generic;
  7. using Android.Net;
  8. using System.Net;
  9. using System;
  10. using Java.Lang.Reflect;
  11. using Java.Util;
  12. using Java.IO;
  13. using Android.Util;
  14.  
  15. namespace Korshunov05F
  16. {
  17. [Activity(Label = "Korshunov05F", MainLauncher = true)]
  18. public class MainActivity : Activity
  19. {
  20. Dictionary<ScanResult, string> scanResults =
  21. new Dictionary<ScanResult, string>();
  22.  
  23. public static ArrayAdapter<string> adapter;
  24. public static List<string> mItems;
  25. private bool start = true;
  26. public static TextView textView;
  27. public Button button;
  28. public ListView listView;
  29. public EditText textField;
  30. private Reciver reciver;
  31. private static WifiManager wifi;
  32. public static List<string> WiFiNetworks;
  33. protected override void OnCreate(Bundle savedInstanceState)
  34. {
  35. SetContentView(Resource.Layout.activity_main);
  36. base.OnCreate(savedInstanceState);
  37.  
  38. button = FindViewById<Button>(Resource.Id.button1);
  39. listView = FindViewById<ListView>(Resource.Id.listView1);
  40. textField = FindViewById<EditText>(Resource.Id.editText1);
  41.  
  42. button.Click += Button_Click;
  43. mItems = new List<string>();
  44.  
  45. listView.ItemClick += ListView_ItemClick;
  46. wifi = (Android.Net.Wifi.WifiManager)GetSystemService(Context.WifiService);
  47. reciver = new Reciver();
  48. reciver.reciver += Reciver_Reciver;
  49. System.Console.Write("Hello");
  50.  
  51.  
  52. }
  53.  
  54. void ListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
  55. {
  56. List<ScanResult> results = new List<ScanResult>();
  57.  
  58. Dictionary<ScanResult, string>.KeyCollection keyColl =
  59. scanResults.Keys;
  60.  
  61.  
  62. foreach (ScanResult r in keyColl)
  63. {
  64. if (r.Ssid != null && r.Ssid.Equals(mItems[e.Position]) && r.Capabilities.ToUpper().Contains("WPA"))
  65. {
  66. ConnectToNetworkWPA(mItems[e.Position], textField.Text);
  67. }
  68. else if (r.Ssid != null && r.Ssid.Equals(mItems[e.Position]) && r.Capabilities.ToUpper().Contains("WEP"))
  69. {
  70. ConnectToNetworkWEP(mItems[e.Position], textField.Text);
  71. }
  72. else
  73. {
  74. ConnectToNetworkOPEN(mItems[e.Position]);
  75. }
  76.  
  77. }
  78.  
  79.  
  80.  
  81.  
  82. }
  83.  
  84.  
  85. void Reciver_Reciver(Context arg1, Intent arg2)
  86. {
  87. mItems = new List<string>();
  88. List<string> allInfo = new List<string>();
  89. IList<ScanResult> results = wifi.ScanResults;
  90. foreach (ScanResult result in results)
  91. {
  92. if (result.Ssid.Length >= 1)
  93. {
  94. int level = result.Level;
  95. if (level >= -50)
  96. {
  97. level = 100;
  98. }
  99. else
  100. {
  101. if (level <= -100)
  102. {
  103. level = 0;
  104. }
  105. else
  106. {
  107. level = 2 * (level + 100);
  108. }
  109. }
  110. }
  111. allInfo.Add(result.Ssid + " - SSID, " + level + "% " + System.Environment.NewLine);
  112. mItems.Add(result.Ssid);
  113. scanResults.Add(result, result.Ssid);
  114.  
  115. }
  116. listView.Adapter = new ArrayAdapter<string>(arg1, Android.Resource.Layout.SimpleListItem1, allInfo);
  117. }
  118.  
  119.  
  120.  
  121.  
  122. public void GetWifiNetworks()
  123. {
  124. RegisterReceiver(reciver, new IntentFilter(WifiManager.ScanResultsAvailableAction));
  125. wifi.StartScan();
  126. }
  127.  
  128. void Button_Click(object sender, EventArgs eventArgs)
  129. {
  130. if (start)
  131. {
  132. GetWifiNetworks();
  133. start = false;
  134. button.Text = "Стоп";
  135. }
  136. else
  137. {
  138. start = true;
  139. button.Text = "Искать";
  140. }
  141. }
  142.  
  143. public class Reciver : BroadcastReceiver
  144. {
  145. public event Action<Context, Intent> reciver;
  146. public override void OnReceive(Context context, Intent intent)
  147. {
  148. this.reciver(context, intent);
  149. }
  150. }
  151.  
  152. public Boolean ConnectToNetworkWPA(String networkSSID, String password)
  153. {
  154. try
  155. {
  156. WifiConfiguration conf = new WifiConfiguration();
  157. conf.Ssid = "\"" + networkSSID + "\"";
  158.  
  159. conf.PreSharedKey = "\"" + password + "\"";
  160.  
  161. conf.StatusField = WifiConfiguration.Status.Enabled;
  162. conf.AllowedGroupCiphers.NextSetBit((int)WifiConfiguration.GroupCipher.Tkip);
  163.  
  164. conf.AllowedGroupCiphers.Set((int)WifiConfiguration.GroupCipher.Tkip);
  165. conf.AllowedGroupCiphers.Set((int)WifiConfiguration.GroupCipher.Ccmp);
  166. conf.AllowedKeyManagement.Set((int)WifiConfiguration.KeyMgmt.WpaPsk);
  167. conf.AllowedPairwiseCiphers.Set((int)WifiConfiguration.PairwiseCipher.Tkip);
  168. conf.AllowedPairwiseCiphers.Set((int)WifiConfiguration.PairwiseCipher.Ccmp);
  169.  
  170.  
  171. wifi.AddNetwork(conf);
  172.  
  173.  
  174.  
  175.  
  176. IList<WifiConfiguration> list = wifi.ConfiguredNetworks;
  177. foreach (WifiConfiguration i in list)
  178. {
  179. if (i.Ssid != null && i.Ssid.Equals("\"" + networkSSID + "\""))
  180. {
  181. wifi.Disconnect();
  182. wifi.EnableNetwork(i.NetworkId, true);
  183. wifi.Reconnect();
  184.  
  185. break;
  186. }
  187. }
  188.  
  189.  
  190. return true;
  191. }
  192. catch (Exception ex)
  193. {
  194. alertManager();
  195.  
  196. return false;
  197. }
  198. }
  199. public Boolean ConnectToNetworkWEP(String networkSSID, String password)
  200. {
  201. try
  202. {
  203. WifiConfiguration conf = new WifiConfiguration();
  204. conf.Ssid = "\"" + networkSSID + "\"";
  205. conf.WepKeys[0] = "\"" + password + "\"";
  206.  
  207. conf.AllowedKeyManagement.Set((int)WifiConfiguration.KeyMgmt.None);
  208. conf.AllowedGroupCiphers.Set((int)WifiConfiguration.AuthAlgorithm.Open);
  209. conf.AllowedGroupCiphers.Set((int)WifiConfiguration.AuthAlgorithm.Shared);
  210.  
  211.  
  212. int networkId = wifi.AddNetwork(conf);
  213.  
  214. if (networkId == -1)
  215. {
  216. conf.WepKeys[0] = password;
  217. networkId = wifi.AddNetwork(conf);
  218. }
  219.  
  220. IList<WifiConfiguration> list = wifi.ConfiguredNetworks;
  221.  
  222. foreach (WifiConfiguration i in list)
  223. {
  224. if (i.Ssid != null && i.Ssid.Equals("\"" + networkSSID + "\""))
  225. {
  226. wifi.Disconnect();
  227. wifi.EnableNetwork(i.NetworkId, true);
  228. wifi.Reconnect();
  229. break;
  230. }
  231. }
  232.  
  233. return true;
  234. }
  235. catch (Exception ex)
  236. {
  237. alertManager();
  238.  
  239. return false;
  240. }
  241. }
  242. public Boolean ConnectToNetworkOPEN(String networkSSID)
  243. {
  244. try
  245. {
  246. WifiConfiguration conf = new WifiConfiguration();
  247. conf.Ssid = "\"" + networkSSID + "\"";
  248. //conf.PreSharedKey = null;
  249. conf.AllowedKeyManagement.Set((int)WifiConfiguration.KeyMgmt.None);
  250. //conf.AllowedProtocols.Set((int)WifiConfiguration.Protocol.Rsn);
  251. //conf.AllowedProtocols.Set((int)WifiConfiguration.Protocol.Wpa);
  252. //conf.AllowedAuthAlgorithms.Clear();
  253. //conf.AllowedPairwiseCiphers.Set((int)WifiConfiguration.PairwiseCipher.Ccmp);
  254. //conf.AllowedPairwiseCiphers.Set((int)WifiConfiguration.PairwiseCipher.Tkip);
  255. //conf.AllowedGroupCiphers.Set((int)WifiConfiguration.GroupCipher.Wep40);
  256. //conf.AllowedGroupCiphers.Set((int)WifiConfiguration.GroupCipher.Wep104);
  257. //conf.AllowedGroupCiphers.Set((int)WifiConfiguration.GroupCipher.Ccmp);
  258. //conf.AllowedGroupCiphers.Set((int)WifiConfiguration.GroupCipher.Tkip);
  259.  
  260.  
  261.  
  262.  
  263.  
  264. IList<WifiConfiguration> list = wifi.ConfiguredNetworks;
  265.  
  266. foreach (WifiConfiguration i in list)
  267. {
  268. if (i.Ssid != null && i.Ssid.Equals("\"" + networkSSID + "\""))
  269. {
  270. wifi.Disconnect();
  271. wifi.EnableNetwork(i.NetworkId, true);
  272. wifi.Reconnect();
  273. break;
  274. }
  275. }
  276.  
  277. return true;
  278. }
  279. catch (Exception ex)
  280. {
  281. alertManager();
  282. return false;
  283. }
  284. }
  285. public void alertManager()
  286. {
  287. AlertDialog.Builder alert = new AlertDialog.Builder(this);
  288. alert.SetTitle("Ошибка");
  289. alert.SetMessage("Ошибка подключения");
  290.  
  291. alert.SetNegativeButton("Cancel", (senderAlert, args) => {
  292. Toast.MakeText(this, "Cancelled!", ToastLength.Short).Show();
  293. });
  294. Dialog dialog = alert.Create();
  295. dialog.Show();
  296. }
  297. }
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement