Android view saved wifi password

Method 1: ADB

adb shell
cat data/misc/wifi/wpa_supplicant.conf

Method 2: code

WifiManage.java

public class WifiManage {
    
    

    public static List<WifiInfo> getFileContent(File file) {
    
    
        List<WifiInfo> wifiList = new ArrayList<>();
        String content = "";
        if (!file.isDirectory()) {
    
      //检查此路径名的文件是否是一个目录(文件夹)
                try {
    
    
                    InputStream instream = new FileInputStream(file);
                    if (instream != null) {
    
    
                        InputStreamReader inputreader
                                = new InputStreamReader(instream, "UTF-8");
                        BufferedReader buffreader = new BufferedReader(inputreader);
                        String line = "";
                        //分行读取
                        while ((line = buffreader.readLine()) != null) {
    
    
                            content += line + "\n";
                        }
                        instream.close();//关闭输入流
                    }
                } catch (java.io.FileNotFoundException e) {
    
    
                    Log.d("TestFile", "The File doesn't not exist.");
                } catch (IOException e) {
    
    
                    Log.d("TestFile", e.getMessage());
                }

        }

        Pattern network = Pattern.compile("network=\\{([^\\}]+)\\}", Pattern.DOTALL);
        Matcher networkMatcher = network.matcher(content);
        while (networkMatcher.find() ) {
    
    
            String networkBlock = networkMatcher.group();
            Pattern ssid = Pattern.compile("ssid=\"([^\"]+)\"");
            Matcher ssidMatcher = ssid.matcher(networkBlock);

            if (ssidMatcher.find() ) {
    
    
                WifiInfo wifiInfo=new WifiInfo();
                wifiInfo.ssid=ssidMatcher.group(1);
                Pattern psk = Pattern.compile("psk=\"([^\"]+)\"");
                Matcher pskMatcher = psk.matcher(networkBlock);
                if (pskMatcher.find() ) {
    
    
                    wifiInfo.password=pskMatcher.group(1);
                } else {
    
    
                    wifiInfo.password="无密码";
                }
                wifiList.add(wifiInfo);
            }

        }

        return wifiList;

    }

}

WifiInfo.java

public class WifiInfo {
    
    
    public String ssid="";
    public String password="";
}

calling place

List<WifiInfo> wifiList =  WifiManage.getFileContent(new File("data/misc/wifi/wpa_supplicant.conf"));
for (WifiInfo bean : wifiList){
    
    
   Log.d("wangrui","ssid = " + bean.ssid + "----password = " + bean.password);
}

Guess you like

Origin blog.csdn.net/qq_27494201/article/details/131074163