Last active
February 18, 2023 07:04
-
-
Save asmsuechan/8c5eb4b78dfd02c1f991d38ccd378de8 to your computer and use it in GitHub Desktop.
Kotlin WifiManager example
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
| package com.wifiexample.example | |
| import android.Manifest | |
| import android.content.BroadcastReceiver | |
| import android.content.Context | |
| import android.content.Intent | |
| import android.content.IntentFilter | |
| import android.content.pm.PackageManager | |
| import android.net.wifi.WifiManager | |
| import android.os.Build | |
| import android.os.Bundle | |
| import android.widget.TextView | |
| import androidx.annotation.RequiresApi | |
| import androidx.appcompat.app.AppCompatActivity | |
| class MainActivity : AppCompatActivity() { | |
| private val wifiManager: WifiManager get() = this.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager | |
| private val PERMISSIONS_REQUEST_CODE = 1 | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_main) | |
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
| if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED || | |
| checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED || | |
| checkSelfPermission(Manifest.permission.ACCESS_WIFI_STATE) != PackageManager.PERMISSION_GRANTED || | |
| checkSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED | |
| ) { | |
| requestPermissions( | |
| arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION, | |
| Manifest.permission.ACCESS_FINE_LOCATION, | |
| Manifest.permission.ACCESS_WIFI_STATE, | |
| Manifest.permission.ACCESS_NETWORK_STATE), | |
| PERMISSIONS_REQUEST_CODE); | |
| } | |
| } | |
| val intentFilter = IntentFilter() | |
| intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) | |
| this.registerReceiver(wifiScanReceiver, intentFilter) | |
| if (wifiManager.wifiState == WifiManager.WIFI_STATE_ENABLED) { | |
| val results = wifiManager.scanResults | |
| println(results) | |
| } | |
| } | |
| val wifiScanReceiver = object : BroadcastReceiver() { | |
| @RequiresApi(Build.VERSION_CODES.M) | |
| override fun onReceive(context: Context, intent: Intent) { | |
| val success = intent.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED, false) | |
| if (success) { | |
| scanSuccess() | |
| } else { | |
| scanFailure() | |
| } | |
| } | |
| } | |
| private fun scanSuccess() { | |
| val results = wifiManager.scanResults | |
| println(results) | |
| } | |
| private fun scanFailure() { | |
| val results = wifiManager.scanResults | |
| println(results) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment