Get Wifi Interface name on Android

前端 未结 6 936
无人及你
无人及你 2021-01-06 18:24

I am currently developing a sort of wifi sniffer. To achieve that I use a tcpdump binary compiled for arm. But it\'s assume that I know the name of the Wifi Interface.

6条回答
  •  情话喂你
    2021-01-06 18:57

    All the answers related to this question are not straight-forward. But you can achieve this by reflection. The code mentioned below will work even when there is no wifi connection.

        String interfaceName=null;
            try {
                Method m = Class.forName("android.os.SystemProperties").getMethod("get", String.class);
                interFace = (String) m.invoke(null, "wifi.interface");
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            System.out.println("Wifi Interface Name: " + interfaceName);
    

    It is working on all the api levels (even when your targetsdk will be 30 inspite of some limitations added in reflection).

提交回复
热议问题