【问题标题】:Get WiFi Scan Results List With Kotlin使用 Kotlin 获取 WiFi 扫描结果列表
【发布时间】:2018-08-17 08:52:59
【问题描述】:

我需要在我的 Android 应用中扫描 wifi 接入点列表。我过去使用 java 做过,但我的 kotlin 代码无法正常工作。

我的代码:

var resultList = ArrayList<ScanResult>()
lateinit var wifiManager: WifiManager

val broadcastReceiver = object : BroadcastReceiver() {
    override fun onReceive(contxt: Context?, intent: Intent?) {
        resultList = wifiManager.scanResults as ArrayList<ScanResult>
        Log.d("TESTING", "onReceive Called")
    }
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    wifiManager = this.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager

}

override fun onGridTileClicked(x: Int, y: Int) {
    startScanning()
}

fun startScanning() {
    registerReceiver(broadcastReceiver, IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))

    Handler().postDelayed({
        stopScanning()
    }, 10000)
}

fun stopScanning() {
    unregisterReceiver(broadcastReceiver)
    val axisList = ArrayList<Axis>()
    for (result in resultList) {
        axisList.add(Axis(result.BSSID, result.level))
    }
    Log.d("TESTING", axisList.toString())

}

永远不会调用 onReceive() 函数,并且我在清单中声明了 ACCESS_FINE_LOCATION 和 ACCESS_WIFI_STATE ,所以我不确定我做错了什么。我确定我遗漏了一些明显的东西,但我们将不胜感激。谢谢!

【问题讨论】:

  • 也许,在startScanning 方法中注册接收器后,您错过了wifiManager.startScan() 调用
  • @StefanGolubović 我知道这很明显!再次感谢,我的朋友。

标签: android kotlin android-wifi


【解决方案1】:

您忘记开始扫描了。在您的 startScanning 方法中添加 wifiManager.startScan() 调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-28
    • 1970-01-01
    • 1970-01-01
    • 2011-07-17
    • 2011-07-24
    相关资源
    最近更新 更多