0

我想 ping 我的移动 AP 的客户端。这样我想看看客户端是否真的连接到我的热点,因为/proc/net/arp仅在我关闭热点时刷新。

这是我的异步任务:

    protected Boolean doInBackground(Object... arg0) {
    // TODO Auto-generated method stub
    try {
        InetAddress ip = (InetAddress) arg0[0];
        this.context = (Context) arg0[1];
        return connected =  ip.isReachable(5000);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return false;
}

@Override
protected void onPostExecute(Boolean result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);
    android.widget.Toast.makeText(this.context, String.valueOf(this.connected), android.widget.Toast.LENGTH_SHORT).show();

}

当您的设备未植根时,是否有办法 ping 客户端?

4

1 回答 1

0

既然你有一个 IP 地址,你可以这样

public static String ping(String _ip) {

    try {
        String command = "ping -c 10 " + _ip;
        Process p = null;
        p = Runtime.getRuntime().exec(command);
        int status = p.waitFor();
        InputStream input = p.getInputStream();
        BufferedReader in = new BufferedReader(new InputStreamReader(input));
        StringBuffer buffer = new StringBuffer();
        String line = "";
        while ((line = in.readLine()) != null) {
            buffer.append(line);
            buffer.append("\n");
        }
        String bufferStr = buffer.toString();
        return bufferStr;
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return null;
}
于 2014-01-14T21:52:42.683 回答