当我的设备处于网络共享模式时,获取未连接的设备

时间:2015-04-01 19:55:46

标签: android android-wifi tethering

我正在使用以下代码激活网络共享模式:

private void setWifiTetheringEnabled(boolean enable) {
    WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);

    Method[] methods = wifiManager.getClass().getDeclaredMethods(); 
    for (Method method : methods) {
        Log.e("teste", method.getName());
        if (method.getName().equals("setWifiApEnabled")) {
            try {
                method.invoke(wifiManager, null, enable);
            } catch (Exception ex) {
            }
            break;
        }
    }
}

我正在使用此代码连接设备(并阅读信息)

   br = new BufferedReader(new FileReader("/proc/net/arp"));

但是在这个文件中(/ proc / net / arp)只有连接的设备,我想获得一些关于刚以网络共享模式扫描我的手机的设备的信息(Mac地址)。

执行ntcfg我的回复:

 04-10 10:32:27.908: E/test(18908): dummy0   DOWN                                   0.0.0.0/0   0x00000082 xx:xx:b6:69:37:4e
04-10 10:32:27.908: E/test(18908): wlan0    UP                                192.168.43.1/24  0x00001043 xx:xx:11:f9:a9:45
04-10 10:32:27.908: E/test(18908): rmnet_usb0 UP                              100.107.189.66/30  0x00000041 00:00:00:00:00:00
04-10 10:32:27.908: E/test(18908): rmnet_usb1 DOWN                                   0.0.0.0/0   0x00000000 00:00:00:00:00:00
04-10 10:32:27.908: E/test(18908): rmnet_usb2 DOWN                                   0.0.0.0/0   0x00000000 00:00:00:00:00:00
04-10 10:32:27.908: E/test(18908): rmnet_usb3 DOWN                                   0.0.0.0/0   0x00000000 00:00:00:00:00:00
04-10 10:32:27.908: E/test(18908): lo       UP                                   127.0.0.1/8   0x00000049 00:00:00:00:00:00
04-10 10:32:27.908: E/test(18908): sit0     DOWN                                   0.0.0.0/0   0x00000080 00:00:00:00:00:00
04-10 10:32:27.908: E/test(18908): rmnet_smux0 DOWN                                   0.0.0.0/0   0x00001002 xx:6f:06:64:e2:61
04-10 10:32:27.908: E/test(18908): rmnet0   DOWN                                   0.0.0.0/0   0x00001002 xx:xx:a4:fe:a4:e5
04-10 10:32:27.908: E/test(18908): rmnet1   DOWN                                   0.0.0.0/0   0x00001002 xx:x:68:34:70:29
04-10 10:32:27.908: E/test(18908): rmnet2   DOWN                                   0.0.0.0/0   0x00001002 xx:xx:ac:a5:b3:a1
04-10 10:32:27.908: E/test(18908): rmnet3   DOWN                                   0.0.0.0/0   0x00001002 xx:xx:1b:af:02:fe
04-10 10:32:27.908: E/test(18908): rmnet4   DOWN                                   0.0.0.0/0   0x00001002 xx:xx:32:79:22:e7
04-10 10:32:27.908: E/test(18908): rmnet5   DOWN                                   0.0.0.0/0   0x00001002 xx:xx:9b:4c:93:c5
04-10 10:32:27.908: E/test(18908): rmnet6   DOWN                                   0.0.0.0/0   0x00001002 xx:xx:1a:1e:86:e8
04-10 10:32:27.908: E/test(18908): rmnet7   DOWN                                   0.0.0.0/0   0x00001002 xx:xx:46:f0:c5:48
04-10 10:32:27.908: E/test(18908): rev_rmnet2 DOWN                                   0.0.0.0/0   0x00001002 xx:xx:98:22:6f:fb
04-10 10:32:27.908: E/test(18908): rev_rmnet3 DOWN                                   0.0.0.0/0   0x00001002 xx:xx:8d:bf:3e:50
04-10 10:32:27.908: E/test(18908): rev_rmnet4 DOWN                                   0.0.0.0/0   0x00001002 xx:xx:f4:c0:81:ae
04-10 10:32:27.908: E/test(18908): rev_rmnet5 DOWN                                   0.0.0.0/0   0x00001002 xx:xx:29:94:d5:e5
04-10 10:32:27.908: E/test(18908): rev_rmnet6 DOWN                                   0.0.0.0/0   0x00001002 xx:xx:41:39:44:db
04-10 10:32:27.908: E/test(18908): rev_rmnet7 DOWN                                   0.0.0.0/0   0x00001002 xx:xx:4b:0b:13:18
04-10 10:32:27.908: E/test(18908): rev_rmnet8 DOWN                                   0.0.0.0/0   0x00001002 xx:xx:64:4f:b4:f6
04-10 10:32:27.908: E/test(18908): rev_rmnet0 DOWN                                   0.0.0.0/0   0x00001002 xx:xx:f4:d2:ae:66
04-10 10:32:27.908: E/test(18908): rev_rmnet1 DOWN                                   0.0.0.0/0   0x00001002 xx:xx:8d:f0:64:2a

1 个答案:

答案 0 :(得分:-1)

致电exec("netcfg");


/**
 * Execute a command in a shell
 * 
 * @param command
 *            command to execute
 * @return the return of the command
 */
public String exec(String command) {
    String retour = "";
    try {
        Runtime runtime = Runtime.getRuntime();

        Process p = runtime.exec(command);

        java.io.BufferedReader standardIn = new java.io.BufferedReader(
                new java.io.InputStreamReader(p.getInputStream()));
        java.io.BufferedReader errorIn = new java.io.BufferedReader(
                new java.io.InputStreamReader(p.getErrorStream()));
        String line = "";
        while ((line = standardIn.readLine()) != null) {
            retour += line + "\n";
        }
        while ((line = errorIn.readLine()) != null) {
            retour += line + "\n";
        }
    } catch (java.io.IOException e) {
        e.printStackTrace();
    }

    return retour;
}

它将在shell&中执行“netcfg”命令。产生如下的输出

lo       UP           127.0.0.1/8   0x00000049  00:00:00:00:00:00

sit0     DOWN         0.0.0.0/0     0x00000080  00:00:00:00:00:00

eth0     UP           0.0.0.0/0     0x00001003  78:c5:e5:a2:b2:d0

wlan0    DOWN         0.0.0.0/0     0x00001002  64:70:02:20:79:31

包括已连接的network interfaces , status , IP obtained & MAC address

最后一个字段是MAC地址。您必须从此单个字符串中解析MAC地址。