├── .gitignore ├── README.md ├── arpinfodemo.iml ├── build.gradle ├── proguard-rules.pro └── src ├── androidTest └── java │ └── com │ └── example │ └── arpinfodemo │ └── ExampleInstrumentedTest.java ├── main ├── AndroidManifest.xml ├── java │ └── com │ │ └── example │ │ └── arpinfodemo │ │ ├── MainActivity.java │ │ └── UDPThread.java └── res │ ├── layout │ └── activity_main.xml │ ├── menu │ └── activity_main.xml │ ├── mipmap-hdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-mdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ └── values │ ├── colors.xml │ ├── strings.xml │ └── styles.xml └── test └── java └── com └── example └── arpinfodemo └── ExampleUnitTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ARPDemo 2 | reade arp info : ip mac 3 | # test -------------------------------------------------------------------------------- /arpinfodemo.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.3" 6 | 7 | defaultConfig { 8 | applicationId "com.example.readarpinfodemo" 9 | minSdkVersion 15 10 | targetSdkVersion 25 11 | versionCode 1 12 | versionName "1.0" 13 | 14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 15 | 16 | } 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | } 24 | 25 | dependencies { 26 | compile fileTree(dir: 'libs', include: ['*.jar']) 27 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 28 | exclude group: 'com.android.support', module: 'support-annotations' 29 | }) 30 | compile 'com.android.support:appcompat-v7:25.3.1' 31 | compile 'com.android.support.constraint:constraint-layout:1.0.2' 32 | testCompile 'junit:junit:4.12' 33 | } 34 | -------------------------------------------------------------------------------- /proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\Android\SDK/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /src/androidTest/java/com/example/arpinfodemo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.example.arpinfodemo; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.example.readarpinfodemo", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/main/java/com/example/arpinfodemo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.arpinfodemo; 2 | 3 | import android.content.Context; 4 | import android.net.wifi.WifiInfo; 5 | import android.net.wifi.WifiManager; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.os.Bundle; 8 | import android.support.v7.widget.ListViewCompat; 9 | import android.util.Log; 10 | import android.view.View; 11 | import android.widget.ListView; 12 | import android.widget.SimpleAdapter; 13 | import android.widget.TextView; 14 | 15 | import java.io.BufferedReader; 16 | import java.io.FileReader; 17 | import java.net.InetAddress; 18 | import java.net.UnknownHostException; 19 | import java.util.ArrayList; 20 | import java.util.HashMap; 21 | import java.util.Hashtable; 22 | import java.util.Locale; 23 | import java.util.Map; 24 | 25 | public class MainActivity extends AppCompatActivity implements View.OnClickListener{ 26 | private String WIP ; 27 | private String myWifiName ; 28 | private String myIp; 29 | private String myMac; 30 | private TextView connectWifiInfo; 31 | private ListView arpLv; 32 | private ArrayList> arpList = new ArrayList>(); 33 | 34 | @Override 35 | protected void onCreate(Bundle savedInstanceState) { 36 | super.onCreate(savedInstanceState); 37 | setContentView(R.layout.activity_main); 38 | 39 | connectWifiInfo = (TextView) findViewById(R.id.connectWifiInfo); 40 | arpLv = (ListView) findViewById(R.id.arplist); 41 | 42 | getNetworkInfo(); 43 | readArp(); 44 | } 45 | 46 | @Override 47 | public void onClick(View v) { 48 | if(v.getId()==R.id.btnRefresh){ 49 | getNetworkInfo(); 50 | readArp(); 51 | } 52 | } 53 | 54 | private void readArp() { 55 | try { 56 | BufferedReader br = new BufferedReader( 57 | new FileReader("/proc/net/arp")); 58 | String line = ""; 59 | String ip = ""; 60 | String flag = ""; 61 | String mac = ""; 62 | 63 | while ((line = br.readLine()) != null) { 64 | try { 65 | line = line.trim(); 66 | if (line.length() < 63) continue; 67 | if (line.toUpperCase(Locale.US).contains("IP")) continue; 68 | ip = line.substring(0, 17).trim(); 69 | flag = line.substring(29, 32).trim(); 70 | mac = line.substring(41, 63).trim(); 71 | if (mac.contains("00:00:00:00:00:00")) continue; 72 | Log.e("scanner", "readArp: mac= "+mac+" ; ip= "+ip+" ;flag= "+flag); 73 | String arp = "ip: "+ip+" | "+"mac: "+mac+" | "+"flag: "+flag; 74 | Map map = new HashMap(); 75 | map.put("arp",arp); 76 | arpList.add(map); 77 | } catch (Exception e) { 78 | continue; 79 | } 80 | } 81 | br.close(); 82 | 83 | SimpleAdapter sad = new SimpleAdapter(this,arpList,R.layout.list_item, 84 | new String[]{"arp"},new int[]{R.id.arpitem}); 85 | arpLv.setAdapter(sad); 86 | 87 | 88 | } catch(Exception e) { 89 | } 90 | 91 | } 92 | 93 | private void getNetworkInfo() { 94 | try { 95 | WifiManager wm = null; 96 | try { 97 | wm = (WifiManager)this.getApplicationContext().getSystemService(Context.WIFI_SERVICE); 98 | } catch (Exception e) { 99 | wm = null; 100 | } 101 | if (wm != null && wm.isWifiEnabled()) { 102 | WifiInfo wifi = wm.getConnectionInfo(); 103 | if (wifi.getRssi() != -200) { 104 | myIp = getWifiIPAddress(wifi.getIpAddress()); 105 | } 106 | myWifiName = wifi.getSSID(); //获取被连接网络的名称 107 | myMac = wifi.getBSSID(); //获取被连接网络的mac地址 108 | String str = "WIFI: "+myWifiName+"\n"+"WiFiIP: "+myIp+"\n"+"MAC: "+myMac; 109 | connectWifiInfo.setText(str); 110 | discover(myIp);// 发送arp请求 111 | } 112 | } catch (Exception e) { 113 | e.getMessage(); 114 | } 115 | } 116 | 117 | 118 | 119 | private void discover(String ip) { 120 | String newip = ""; 121 | if (!ip.equals("")) { 122 | String ipseg = ip.substring(0, ip.lastIndexOf(".")+1); 123 | for (int i=2; i<255; i++) { 124 | newip = ipseg+String.valueOf(i); 125 | if (newip.equals(ip)) continue; 126 | Thread ut = new UDPThread(newip); 127 | ut.start(); 128 | } 129 | } 130 | } 131 | 132 | private String getWifiIPAddress(int ipaddr) { 133 | String ip = ""; 134 | if (ipaddr == 0) return ip; 135 | byte[] addressBytes = {(byte)(0xff & ipaddr), (byte)(0xff & (ipaddr >> 8)), 136 | (byte)(0xff & (ipaddr >> 16)), (byte)(0xff & (ipaddr >> 24))}; 137 | try { 138 | ip = InetAddress.getByAddress(addressBytes).toString(); 139 | if (ip.length() > 1) { 140 | ip = ip.substring(1, ip.length()); 141 | } else { 142 | ip = ""; 143 | } 144 | } catch (UnknownHostException e) { 145 | ip = ""; 146 | } catch (Exception e) { 147 | ip = ""; 148 | } 149 | return ip; 150 | } 151 | 152 | 153 | } 154 | -------------------------------------------------------------------------------- /src/main/java/com/example/arpinfodemo/UDPThread.java: -------------------------------------------------------------------------------- 1 | package com.example.arpinfodemo; 2 | 3 | import java.io.IOException; 4 | import java.net.DatagramPacket; 5 | import java.net.DatagramSocket; 6 | import java.net.InetAddress; 7 | import java.net.SocketException; 8 | import java.net.UnknownHostException; 9 | 10 | public class UDPThread extends Thread { 11 | private String target_ip = ""; 12 | 13 | public static final byte[] NBREQ = { (byte) 0x82, (byte) 0x28, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1, 14 | (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x20, (byte) 0x43, (byte) 0x4B, 15 | (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, 16 | (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, 17 | (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, 18 | (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x0, (byte) 0x0, (byte) 0x21, (byte) 0x0, (byte) 0x1 }; 19 | 20 | public static final short NBUDPP = 137; 21 | 22 | public UDPThread(String target_ip) { 23 | this.target_ip = target_ip; 24 | } 25 | 26 | @Override 27 | public synchronized void run() { 28 | if (target_ip == null || target_ip.equals("")) return; 29 | DatagramSocket socket = null; 30 | InetAddress address = null; 31 | DatagramPacket packet = null; //单播 32 | try { 33 | address = InetAddress.getByName(target_ip); 34 | packet = new DatagramPacket(NBREQ, NBREQ.length, address, NBUDPP); 35 | socket = new DatagramSocket(); 36 | socket.setSoTimeout(200); 37 | socket.send(packet); 38 | socket.close(); 39 | } catch (SocketException se) { 40 | } catch (UnknownHostException e) { 41 | } catch (IOException e) { 42 | } finally { 43 | if (socket != null) { 44 | socket.close(); 45 | } 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 16 | 17 | 24 | 31 |