Java 列出网络上的所有设备-Android
我试图根据本教程显示网络上的所有设备:但没有理由,我只得到路由器Ip和Mac。。。但是(奇怪的部分)。。当我用Fing(其他android应用程序)扫描时,当我用我的应用程序扫描后,我会得到所有结果 scanwifi.jar中的我的代码:Java 列出网络上的所有设备-Android,java,android,xml,android-studio,Java,Android,Xml,Android Studio,我试图根据本教程显示网络上的所有设备:但没有理由,我只得到路由器Ip和Mac。。。但是(奇怪的部分)。。当我用Fing(其他android应用程序)扫描时,当我用我的应用程序扫描后,我会得到所有结果 scanwifi.jar中的我的代码: package com.nliplace.nli.unealta; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7
package com.nliplace.nli.unealta;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
public class scanwifi extends AppCompatActivity {
Button btnRead;
TextView textResult;
ListView listViewNode;
ArrayList<Node> listNote;
ArrayAdapter<Node> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanwifi);
btnRead = (Button)findViewById(R.id.readclient);
textResult = (TextView)findViewById(R.id.result);
listViewNode = (ListView)findViewById(R.id.nodelist);
listNote = new ArrayList<>();
ArrayAdapter<Node> adapter =
new ArrayAdapter<Node>(
scanwifi.this,
android.R.layout.simple_list_item_1,
listNote);
listViewNode.setAdapter(adapter);
listViewNode.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Node node = (Node) parent.getAdapter().getItem(position);
Toast.makeText(scanwifi.this,
"MAC:\t" + node.mac + "\n" +
"IP:\t" + node.ip + "\n" +
"company:\t" + node.company + "\n" +
"country:\t" + node.country + "\n" +
"addressL1:\t" + node.addressL1 + "\n" +
"addressL2:\t" + node.addressL2 + "\n" +
"addressL3:\t" + node.addressL3 + "\n" +
"type:\t" + node.type + "\n" +
"startHex:\t" + node.startHex + "\n" +
"endHex:\t" + node.endHex + "\n" +
"startDec:\t" + node.startDec + "\n" +
"endDec:\t" + node.endDec,
Toast.LENGTH_SHORT).show();
}
});
btnRead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new TaskReadAddresses(listNote, listViewNode).execute();
}
});
}
class Node {
String ip;
String mac;
String jsonBody;
String startHex;
String endHex;
String startDec;
String endDec;
String company;
String addressL1;
String addressL2;
String addressL3;
String country;
String type;
String remark;
String queryString = "http://www.macvendorlookup.com/api/v2/";
Node(String ip, String mac){
this.ip = ip;
this.mac = mac;
queryMacVendorLookup();
}
@Override
public String toString() {
return "IP: " + ip + "\n" + "MAC: " + mac + "\n" + company + "\n" + remark;
}
private String sendQuery(String qMac) throws IOException{
String result = "";
URL searchURL = new URL(queryString + qMac);
HttpURLConnection httpURLConnection = (HttpURLConnection) searchURL.openConnection();
if(httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK){
InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader,
8192);
String line = null;
while((line = bufferedReader.readLine()) != null){
result += line;
}
bufferedReader.close();
}
return result;
}
private void ParseResult(String json){
try {
JSONArray jsonArray = new JSONArray(json);
JSONObject jsonObject = (JSONObject) jsonArray.get(0);
startHex = jsonObject.getString("startHex");
endHex = jsonObject.getString("endHex");
startDec = jsonObject.getString("startDec");
endDec = jsonObject.getString("endDec");
company = jsonObject.getString("company");
addressL1 = jsonObject.getString("addressL1");
addressL2 = jsonObject.getString("addressL2");
addressL3 = jsonObject.getString("addressL3");
country = jsonObject.getString("country");
type = jsonObject.getString("type");
remark = "OK";
} catch (JSONException e) {
e.printStackTrace();
remark = e.getMessage();
}
}
private void queryMacVendorLookup(){
try {
jsonBody = sendQuery(mac);
ParseResult(jsonBody);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class TaskReadAddresses extends AsyncTask<Void, Node, Void> {
ArrayList<Node> array;
ListView listView;
TaskReadAddresses(ArrayList<Node> array, ListView v){
listView = v;
this.array = array;
array.clear();
textResult.setText("querying...");
}
@Override
protected Void doInBackground(Void... params) {
readAddresses();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
textResult.setText("Done");
}
@Override
protected void onProgressUpdate(Node... values) {
listNote.add(values[0]);
((ArrayAdapter)(listView.getAdapter())).notifyDataSetChanged();
}
private void readAddresses() {
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] splitted = line.split(" +");
if (splitted != null && splitted.length >= 4) {
String ip = splitted[0];
String mac = splitted[3];
if (mac.matches("..:..:..:..:..:..")) {
Node thisNode = new Node(ip, mac);
publishProgress(thisNode);
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package com.nliplace.nli.unelta;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.support.v7.app.AppActivity;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.ListView;
导入android.widget.TextView;
导入android.widget.Toast;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.io.BufferedReader;
导入java.io.FileNotFoundException;
导入java.io.FileReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.net.HttpURLConnection;
导入java.net.URL;
导入java.util.ArrayList;
公共类scanwifi扩展应用程序活动{
按钮btnRead;
文本查看文本结果;
ListView listViewNode;
ArrayList listNote;
阵列适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u scanwifi);
btnRead=(按钮)findViewById(R.id.readclient);
textResult=(TextView)findViewById(R.id.result);
listViewNode=(ListView)findViewById(R.id.nodelist);
listNote=新的ArrayList();
阵列适配器=
新阵列适配器(
这个,,
android.R.layout.simple\u list\u item\u 1,
列表注释);
setAdapter(适配器);
listViewNode.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
Node Node=(节点)parent.getAdapter().getItem(位置);
Toast.makeText(scanwifi.this,
“MAC:\t”+node.MAC+“\n”+
“IP:\t”+node.IP+”\n”+
公司:\t“+node.company+”\n+
国家:\t“+node.country+”\n+
addressL1:\t“+节点。addressL1+”\n+
addressL2:\t“+节点。addressL2+”\n+
地址L3:\t“+节点。地址L3+”\n+
类型:\t“+node.type+”\n+
开始X:\t“+节点。开始X+”\n+
“endHex:\t”+node.endHex+”\n”+
startDec:\t“+节点。startDec+”\n+
“endDec:\t”+node.endDec,
吐司。长度(短)。show();
}
});
btnRead.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
新建TaskReadAddresses(listNote,listViewNode).execute();
}
});
}
类节点{
字符串ip;
字符串mac;
字符串jsonBody;
字符串startHex;
字符串结束六角;
字符串startDec;
字符串endDec;
弦乐公司;
字符串地址L1;
字符串地址L2;
字符串地址l3;
弦国;
字符串类型;
串注;
字符串查询字符串=”http://www.macvendorlookup.com/api/v2/";
节点(字符串ip、字符串mac){
this.ip=ip;
this.mac=mac;
QueryCamvendoLookup();
}
@凌驾
公共字符串toString(){
返回“IP:+IP+”\n“+”MAC:+MAC+“\n”+公司+“\n”+备注;
}
私有字符串sendQuery(字符串qMac)引发IOException{
字符串结果=”;
URL searchURL=新URL(queryString+qMac);
HttpURLConnection HttpURLConnection=(HttpURLConnection)searchURL.openConnection();
if(httpURLConnection.getResponseCode()==httpURLConnection.HTTP\u确定){
InputStreamReader InputStreamReader=新的InputStreamReader(httpURLConnection.getInputStream());
BufferedReader BufferedReader=新的BufferedReader(
输入流阅读器,
8192);
字符串行=null;
而((line=bufferedReader.readLine())!=null){
结果+=行;
}
bufferedReader.close();
}
返回结果;
}
私有void ParseResult(字符串json){
试一试{
JSONArray JSONArray=新JSONArray(json);
JSONObject JSONObject=(JSONObject)jsonArray.get(0);
startHex=jsonObject.getString(“startHex”);
endHex=jsonObject.getString(“endHex”);
startDec=jsonObject.getString(“startDec”);
endDec=jsonObject.getString(“endDec”);
company=jsonObject.getString(“公司”);
addressL1=jsonObject.getString(“addressL1”);
addressL2=jsonObject.getString(“addressL2”);
addressL3=jsonObject.getString(“addressL3”);
国家=jsonObjec
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical"
tools:context="com.nliplace.nli.unealta.scanwifi">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Aceasta activitate foloseste un API pentru a identifica dispozitive cunoscute."
android:id="@+id/textView11" />
<Button
android:id="@+id/readclient"
android:layout_width="156dp"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="Scaneaza"
android:layout_gravity="center_horizontal" />
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ListView
android:id="@+id/nodelist"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>