如何使InetAddress.reachable()执行更快?

时间:2019-06-21 06:43:54

标签: android kotlin inet

在我的应用中,我试图在此获取已连接设备的wifi列表,我可以成功获取所有设备,但是问题是,当我从自己的设备中删除val reachable = address.isReachable(1000)时,整个过程要花很多时间才能在此处执行循环可以立即执行,但是我需要这一行代码来检查连接,所以我的问题是如何增强我的代码以更快地获得所需的结果!

任何建议将不胜感激!

这是我的代码:

for (i in 0..254)
                    {
                        val testIp = prefix + (i).toString()
                        val address = InetAddress.getByName(testIp)
                        val reachable = address.isReachable(1000)
                        val hostName = address.hostName.toString()
                        if (reachable) {
                            Log.i(TAG, "Host: " + (hostName) + "(" + (testIp) + ") is Connected!")
                        }

                    }

这是我获取连接设备列表的完整代码:

fun sniffnetwork(){
        val mContextRef:WeakReference<Context>
        mContextRef = WeakReference<Context>(this)
        Observable.fromCallable {
            Log.d(TAG, "Let's sniff the network")
            try
            {
                val context = mContextRef.get()
                if (context != null)
                {
                    val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
                    val activeNetwork = cm.getActiveNetworkInfo()
                    val wm = context.getApplicationContext().getSystemService(Context.WIFI_SERVICE) as WifiManager
                    val connectionInfo = wm.getConnectionInfo()
                    val ipAddress = connectionInfo.getIpAddress()
                    val ipString = Formatter.formatIpAddress(ipAddress)
                    Log.d(TAG, "activeNetwork: " + activeNetwork.toString())
                    wifiname = activeNetwork.extraInfo
                    wifiname= wifiname.replace("^\"|\"$".toRegex(), "")
                    tvwifiname.text = wifiname + " "+ "connected "
                    Log.d(TAG, "ipString: " + (ipString).toString())
                    val prefix = ipString.substring(0, ipString.lastIndexOf(".") + 1)
                    Log.d(TAG, "prefix: " + prefix)
                    for (i in 0..254)
                    {
                        val testIp = prefix + (i).toString()
                        val address = InetAddress.getByName(testIp)
                        val reachable = address.isReachable(200)
                        val hostName = address.hostName.toString()
                        if (reachable) {
                            Log.i(TAG, "Host: " + (hostName) + "(" + (testIp) + ") is Connected!")
                        }

                    }
                }
                else
                {
                    Log.i("error:","Context is null")
                }
            }
            catch (t:Throwable) {
                Log.e(TAG, "Well that's not good.", t)
            }

        }.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe{

                }
    }

0 个答案:

没有答案