Skip to content

Commit 11e35d0

Browse files
Fixed open network connections
1 parent 9086d38 commit 11e35d0

File tree

5 files changed

+33
-5
lines changed

5 files changed

+33
-5
lines changed

app/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ android {
1111
applicationId = "com.cgprograms.dnslock"
1212
minSdk = 29
1313
targetSdk = 34
14-
versionCode = 3
15-
versionName = "3.0.0"
14+
versionCode = 4
15+
versionName = "3.0.1"
1616

1717
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
1818
vectorDrawables {
20 Bytes
Binary file not shown.
15 Bytes
Binary file not shown.

app/release/output-metadata.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
"type": "SINGLE",
1212
"filters": [],
1313
"attributes": [],
14-
"versionCode": 3,
15-
"versionName": "3.0.0",
14+
"versionCode": 4,
15+
"versionName": "3.0.1",
1616
"outputFile": "app-release.apk"
1717
}
1818
],

app/src/main/java/com/cgprograms/dnslock/MainActivity.kt

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,35 @@ class MainActivity : Activity() {
7070
wifiManager.enableNetwork(netId, true)
7171
}
7272

73-
// Toggle Wi-Fi on/off
73+
addButton.setOnClickListener {
74+
val selectedSSID = ssid.text.toString()
75+
val scanResult = wifiManager.scanResults.find { it.SSID == selectedSSID }
76+
77+
if (scanResult != null) {
78+
val isOpenNetwork = getSecurityType(scanResult) == "Open"
79+
80+
val wifiConfig = WifiConfiguration().apply {
81+
SSID = "\"$selectedSSID\""
82+
if (isOpenNetwork) {
83+
allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE)
84+
} else {
85+
preSharedKey = "\"${password.text}\""
86+
}
87+
}
88+
89+
val netId = wifiManager.addNetwork(wifiConfig)
90+
if (netId != -1) {
91+
wifiManager.enableNetwork(netId, true)
92+
Toast.makeText(this, "Connected to $selectedSSID", Toast.LENGTH_SHORT).show()
93+
} else {
94+
Toast.makeText(this, "Failed to connect to $selectedSSID", Toast.LENGTH_SHORT).show()
95+
}
96+
} else {
97+
Toast.makeText(this, "SSID not found in scan results", Toast.LENGTH_SHORT).show()
98+
}
99+
}
100+
101+
74102
toggleWifiButton.setOnClickListener {
75103
val status = !wifiManager.isWifiEnabled
76104
wifiManager.isWifiEnabled = status

0 commit comments

Comments
 (0)