AndroidNetworkTools - 實用的Android網絡工具集合

jopen 8年前發布 | 14K 次閱讀 Android開發 移動開發

Android Network Tools

AndroidNetworkTools - 實用的Android網絡工具集合,包括:

  • Ping
  • 端口掃描
  • 子網工具(找到本地網絡上的設備)
  • Wake-On-Lan網絡喚醒
  • & More :)

Usage

Add as dependency

This library is not yet released in Maven Central, until then you can add as a library module or use JitPack.io

add remote maven url

repositories {
        maven {
            url "https://jitpack.io"
        }
    }

then add a library dependency. Remember to check for latest release here

dependencies {
        compile 'com.github.stealthcopter:AndroidNetworkTools:0.1.1'
    }

Add permission

Requires internet permission (obviously...)

<uses-permission android:name="android.permission.INTERNET" />

Ping

Uses the native ping binary if avaliable on the device (some devices come without it) and falls back to a TCP request on port 7 (echo request) if not.

// Synchronously 
     PingResult pingResult = Ping.onAddress("192.168.0.1").setTimeOutMillis(1000).doPing();

     // Asynchronously
     Ping.onAddress("192.168.0.1").setTimeOutMillis(1000).setTimes(5).doPing(new Ping.PingListener() {
      @Override
      public void onResult(PingResult pingResult) {
        ...
      }
    });

Note: If we do have to fall back to using TCP port 7 (the java way) to detect devices we will find significantly less than with the native ping binary. If this is an issue you could consider adding a ping binary to your application or device so that it is always avaliable.

Port Scanning

A simple java based TCP port scanner, fast and easy to use.

// Synchronously 
    ArrayList<Integer> openPorts = PortScan.onAddress("192.168.0.1").setPort(21).doScan();

    // Asynchronously
    PortScan.onAddress("192.168.0.1").setTimeOutMillis(1000).setPortsAll().doScan(new PortScan.PortListener() {
      @Override
      public void onResult(int portNo, boolean open) {
        if (open) // Stub: found open port
      }

      @Override
      public void onFinished(ArrayList<Integer> openPorts) {
    // Stub: finished scanning
      }
    });

Note: If you want a more advanced portscanner you should consider compiling nmap into your project and using that instead.

Wake-On-Lan

Sends a Wake-on-Lan packet to the IP / MAC address

String ipAddress = "192.168.0.1";
      String macAddress = "01:23:45:67:89:ab";
      WakeOnLan.sendWakeOnLan(ipAddress, macAddress);

Misc

Other useful methods:

String ipAddress = "192.168.0.1";
      String macAddress = ARPInfo.getMacFromArpCache(ipAddress);

Building

It's a standard gradle project.

Contributing

I welcome pull requests, issues and feedback.

  • Fork it
  • Create your feature branch (git checkout -b my-new-feature)
  • Commit your changes (git commit -am 'Added some feature')
  • Push to the branch (git push origin my-new-feature)
  • Create new Pull Request

項目地址: https://github.com/stealthcopter/AndroidNetworkTools

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!