Skip to content

Commit

Permalink
Added connection function using the WPS button on the device.
Browse files Browse the repository at this point in the history
  • Loading branch information
LowSkillDeveloper committed Jun 22, 2024
1 parent dc9aad6 commit 28ebfa6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import android.location.LocationManager
import android.net.Uri
import android.net.wifi.WifiConfiguration
import android.net.wifi.WifiManager
import android.net.wifi.WpsInfo
import android.os.Build
import android.os.Bundle
import android.os.Handler
Expand Down Expand Up @@ -672,6 +673,31 @@ class MyActivity : AppCompatActivity() {
}
wpsGenStart(essdWps, bssdWps)
}
6 -> run {
if (!scanResult.capabilities!!.contains("WPS")) {
val dialogClickListener = DialogInterface.OnClickListener { dialog, which ->
when (which) {
DialogInterface.BUTTON_POSITIVE -> scanResult.bssid?.let { connectUsingWPSButton(it) }
DialogInterface.BUTTON_NEGATIVE -> {
}
}
dialog.dismiss()
}
val builder = AlertDialog.Builder(this@MyActivity)
builder.setTitle(getString(R.string.dialog_are_you_sure))
.setMessage(String.format(getString(R.string.dialog_wps_disabled), scanResult.essid))
.setPositiveButton(getString(R.string.dialog_yes), dialogClickListener)
.setNegativeButton(getString(R.string.dialog_no), dialogClickListener).show()
return@run
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
scanResult.bssid?.let { connectUsingWPSButton(it) }
} else {
val toast = Toast.makeText(applicationContext,
getString(R.string.dialog_message_unsupported_android), Toast.LENGTH_LONG)
toast.show()
}
}
}
if (needToast) {
val toast = Toast.makeText(applicationContext,
Expand All @@ -683,6 +709,29 @@ class MyActivity : AppCompatActivity() {
dialogBuilder.show()
}


private fun connectUsingWPSButton(bssid: String) {
val wpsConfig = WpsInfo()
wpsConfig.setup = WpsInfo.PBC
wpsConfig.BSSID = bssid

val wpsCallback = object : WifiManager.WpsCallback() {
override fun onStarted(pin: String?) {
Toast.makeText(applicationContext, "WPS started.", Toast.LENGTH_SHORT).show()
}

override fun onSucceeded() {
Toast.makeText(applicationContext, "WPS succeeded", Toast.LENGTH_LONG).show()
}

override fun onFailed(reason: Int) {
Toast.makeText(applicationContext, "WPS failed. Reason: $reason", Toast.LENGTH_LONG).show()
}
}

wifiMgr.startWps(wpsConfig, wpsCallback)
}

private fun wpsGenStart(essdWps: String, bssdWps: String) {
val wpsActivityIntent = Intent(this@MyActivity, WPSActivity::class.java)
wpsActivityIntent.putExtra("variable", essdWps)
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<item>Скопировать пароль</item>
<item>Сохранить wifi в настройках телефона</item>
<item>Сгенерировать пин-код WPS</item>
<item>Подключиться с помощью кнопки WPS на устройстве</item>
</string-array>

<string-array name="menu_wps_pin">
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
<item>Copy network key</item>
<item>Store network profile</item>
<item>Generate WPS PIN</item>
<item>Connect using the WPS button on device</item>
</string-array>

<string name="status_searching">Searching in 3WiFi…</string>
Expand Down

0 comments on commit 28ebfa6

Please sign in to comment.