Ping Android手机APP的客户端

时间:2012-08-27 22:45:55

标签: android connection client ping access-point

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

这是我的AsyncTask:

    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客户端?

1 个答案:

答案 0 :(得分: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;
}