Android Studio ping所有设备

时间:2017-10-07 18:08:32

标签: android

我创建了android应用程序来扫描并显示列表视图中连接到lan的所有设备的IP地址。 当我按下按钮启动扫描设备时,它不会显示listView中的任何设备!我在AndroidManifest中添加了对Internet的权限! 有人可以帮我解决这个问题吗?

    private Button btnScan;
    private ListView listViewIp;
    private String defaultGateway = "";
    DhcpInfo dhcp;
    WifiManager manager;
    ArrayList<String> ipList;
    ArrayAdapter<String> adapter;

    public ServersFragment()
    {
        // Required empty public constructor
    }

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
{
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_servers, container, false);

    btnScan = (Button) view.findViewById(R.id.readclient);
    listViewIp = (ListView) view.findViewById(R.id.listviewip);

    ipList = new ArrayList();
    adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, ipList);
    listViewIp.setAdapter(adapter);

    btnScan.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            new ScanIpTask().execute();
        }
    });

    manager = (WifiManager) getActivity().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    dhcp = manager.getDhcpInfo();
    defaultGateway = Formatter.formatIpAddress(dhcp.gateway);

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString("defaultGateway", defaultGateway);
    editor.commit();

    return view;
}


private class ScanIpTask extends AsyncTask<Void, String, Void>
{
    int timeout = 1000;

    @Override
    protected void onPreExecute()
    {
        ipList.clear();
        adapter.notifyDataSetInvalidated();
        Toast.makeText(getActivity(), "Scan IP...", Toast.LENGTH_LONG).show();

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
        String defaultGateway = prefs.getString("defaultGateway", "");

        defaultGateway = defaultGateway.substring(0, 9) + ".";
    }

    @Override
    protected Void doInBackground(Void... params)
    {
        for (int i = 0; i <= 255; i++)
        {
            String host = defaultGateway + i;
            System.out.println("host: " + host);
            try
            {
                InetAddress inetAddress = InetAddress.getByName(host);
                if (inetAddress.isReachable(timeout))
                {
                    publishProgress(inetAddress.toString());
                }

            } catch (UnknownHostException e)
            {
                e.printStackTrace();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }

        return null;
    }

    @Override
    protected void onProgressUpdate(String... values)
    {
        ipList.add(values[0]);
        adapter.notifyDataSetInvalidated();
        Toast.makeText(getActivity(), values[0], Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onPostExecute(Void aVoid)
    {
        Toast.makeText(getActivity(), "Done", Toast.LENGTH_LONG).show();
    }
}

0 个答案:

没有答案