Last active
December 31, 2017 18:10
-
-
Save Cheesebaron/5844638 to your computer and use it in GitHub Desktop.
Quick and dirty list of WiFi AP's and connect to it. Only WPA/WPA2 tested.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto" package="dk.ostebaronen.wifishizniz" android:versionCode="1" android:versionName="1.0"> | |
<uses-sdk android:targetSdkVersion="17" /> | |
<application android:label="WiFiShizniz"></application> | |
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | |
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> | |
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> | |
</manifest> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:minWidth="25px" | |
android:minHeight="25px"> | |
<ListView | |
android:minWidth="25px" | |
android:minHeight="25px" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:id="@+id/listView1" /> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content"> | |
<EditText | |
android:id="@+id/password" | |
android:inputType="textPassword" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="4dp" | |
android:layout_marginLeft="4dp" | |
android:layout_marginRight="4dp" | |
android:layout_marginBottom="16dp" | |
android:hint="Password" /> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Threading; | |
using Android.App; | |
using Android.Content; | |
using Android.Net.Wifi; | |
using Android.Runtime; | |
using Android.Widget; | |
using Android.OS; | |
namespace WifiShizniz | |
{ | |
[Activity(Label = "WifiShizniz", MainLauncher = true, Icon = "@drawable/icon")] | |
public class Activity1 : Activity | |
{ | |
private ListView _listView; | |
private ArrayAdapter<WifiEntry> _adapter; | |
private string _selectedSsid; | |
protected override void OnCreate(Bundle bundle) | |
{ | |
base.OnCreate(bundle); | |
// Set our view from the "main" layout resource | |
SetContentView(Resource.Layout.Main); | |
_listView = FindViewById<ListView>(Resource.Id.listView1); | |
RefreshWifiList(); | |
_listView.ItemClick += ListViewOnItemClick; | |
} | |
private void ListViewOnItemClick(object sender, AdapterView.ItemClickEventArgs itemClickEventArgs) | |
{ | |
var wifiItem = _adapter.GetItem(itemClickEventArgs.Position); | |
_selectedSsid = wifiItem.SSID; | |
if (wifiItem.Encryption.Contains("WPA")) | |
{ | |
OnCreateDialog(WpaDialog).Show(); | |
} | |
else if (wifiItem.Encryption.Contains("WEP")) | |
{ | |
OnCreateDialog(WepDialog).Show(); | |
} | |
else | |
{ | |
//Other, captive portal, open network, etc. | |
} | |
} | |
private void RefreshWifiList() | |
{ | |
var wifiManager = GetSystemService(WifiService).JavaCast<WifiManager>(); | |
wifiManager.StartScan(); | |
ThreadPool.QueueUserWorkItem(lol => | |
{ | |
while (true) | |
{ | |
Thread.Sleep(TimeSpan.FromSeconds(3)); | |
var wifiList = wifiManager.ScanResults; | |
if (null == _adapter) | |
{ | |
_adapter = new ArrayAdapter<WifiEntry>(this, Android.Resource.Layout.SimpleListItem1, | |
Android.Resource.Id.Text1); | |
RunOnUiThread(() => _listView.Adapter = _adapter); | |
} | |
if (_adapter.Count > 0) | |
{ | |
RunOnUiThread(() => _adapter.Clear()); | |
} | |
foreach (var wifi in wifiList) | |
{ | |
var wifi1 = wifi; | |
RunOnUiThread(() => _adapter.Add(new WifiEntry { SSID = wifi1.Ssid, Encryption = wifi1.Capabilities})); | |
} | |
RunOnUiThread(() => _adapter.NotifyDataSetChanged()); | |
} | |
}); | |
} | |
private const int WpaDialog = 0; | |
private const int WepDialog = 1; | |
protected override Dialog OnCreateDialog(int id) | |
{ | |
var customView = LayoutInflater.Inflate(Resource.Layout.wifi_dialog, null); | |
var builder = new AlertDialog.Builder(this); | |
builder.SetIcon(Android.Resource.Drawable.IcMenuPreferences); | |
builder.SetView(customView); | |
builder.SetTitle("Set WiFi password"); | |
switch(id) | |
{ | |
case WpaDialog: | |
{ | |
builder.SetPositiveButton("OK", WpaOkClicked); | |
builder.SetNegativeButton("Cancel", CancelClicked); | |
return builder.Create(); | |
} | |
case WepDialog: | |
{ | |
builder.SetPositiveButton("OK", WepOkClicked); | |
builder.SetNegativeButton("Cancel", CancelClicked); | |
return builder.Create(); | |
} | |
} | |
return base.OnCreateDialog(id); | |
} | |
private void WepOkClicked(object sender, DialogClickEventArgs e) | |
{ | |
var dialog = (AlertDialog) sender; | |
var password = (EditText) dialog.FindViewById(Resource.Id.password); | |
var conf = new WifiConfiguration(); | |
conf.Ssid = "\"" + _selectedSsid + "\""; | |
conf.WepKeys[0] = "\"" + password + "\""; | |
conf.WepTxKeyIndex = 0; | |
conf.AllowedKeyManagement.Set((int)KeyManagementType.None); | |
conf.AllowedGroupCiphers.Set((int)GroupCipherType.Wep40); | |
var wifiManager = GetSystemService(WifiService).JavaCast<WifiManager>(); | |
wifiManager.AddNetwork(conf); | |
foreach (var network in | |
wifiManager.ConfiguredNetworks.Where(network => network.Ssid.Contains(_selectedSsid))) | |
{ | |
wifiManager.Disconnect(); | |
wifiManager.EnableNetwork(network.NetworkId, true); | |
wifiManager.Reconnect(); | |
} | |
} | |
private void WpaOkClicked(object sender, DialogClickEventArgs e) | |
{ | |
var dialog = (AlertDialog)sender; | |
var password = (EditText)dialog.FindViewById(Resource.Id.password); | |
var conf = new WifiConfiguration(); | |
conf.Ssid = "\"" + _selectedSsid + "\""; | |
conf.PreSharedKey = "\"" + password + "\""; | |
var wifiManager = GetSystemService(WifiService).JavaCast<WifiManager>(); | |
wifiManager.AddNetwork(conf); | |
foreach (var network in | |
wifiManager.ConfiguredNetworks.Where(network => network.Ssid.Contains(_selectedSsid))) | |
{ | |
wifiManager.Disconnect(); | |
wifiManager.EnableNetwork(network.NetworkId, true); | |
wifiManager.Reconnect(); | |
} | |
} | |
private void CancelClicked(object sender, DialogClickEventArgs e) | |
{ | |
} | |
} | |
public class WifiEntry | |
{ | |
public string SSID { get; set; } | |
public string Encryption { get; set; } | |
public override string ToString() | |
{ | |
return SSID + " : " + Encryption; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment