Search code examples
androidwifiandroid-wifiwifimanager

Retrieve the saved WiFi passwords from Android devices


I am developing an Android application in which I need to show the saved WiFi passwords in the mobile or tablet. Like for example, if my mobile is connected to any network that n/w password is saved in my mobile. I want to get it.


Solution

  • Your Comments helped me to some extent to find out the solution to my question. Especially @Namik Kalavadia I am talking about you Thanks for that. Finally here is the solution.

    public class MainActivity extends Activity {
    
        File file;
        public StringBuffer ab;
        public File savefile;
        public InputStream in = null;
        public String filename = "wpa_supplicant.conf";
        public File ot_path;
        Context context;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            context = getApplicationContext();
            ot_path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
            Log.d("aaa", ""+ot_path.toString());
        }
    
        public void path(View v){
            getPath();
        }
        private void getPath(){
            file = Environment.getRootDirectory();
            String ext = ".conf";
            File list[] = file.listFiles();
            ab = new StringBuffer();
            if(list!=null){
                fileNameSearch(list);
    
        }
        }
    
        public void fileNameSearch(File list[]){
            if(list!=null){
            for(int f = 0;f<list.length;f++){
                ab.append(list[f].getName()+"\n");
    
                File fi = list[f];
                String path = fi.getPath();
                if(fi.isDirectory()){
                    fileNameSearch(fi.listFiles());
                }
                else if(path.endsWith(".conf")){
                    if(path.contains(filename)){
                        try{
                        File fileForParse = copyFile(path,ot_path);
                        in = new FileInputStream(fileForParse);
                        getStringFromInputStream(in);
    
                    Log.d("aaa", "conf I got it"+path);
                        }catch(Exception e){
                            e.printStackTrace();
                        }
                    }
                }
            }
            }
            else{
                Log.d("aaa", "List is null in method");
            }
        }
        private File copyFile(String inputPath, File outputPath) {
    
            InputStream input = null;
            OutputStream out = null;
            try {
    
                if (!outputPath.exists())
                {
                    outputPath.mkdirs();
                }
                savefile = new File(outputPath,filename);
                if (!savefile.exists()) {
                    savefile.createNewFile();
                    File f = new File(inputPath);
                    Log.d("aaa",""+f.length());
                    input = new FileInputStream(inputPath);        
                    out = new FileOutputStream(savefile);
                    byte[] buffer = new byte[1024];
                    int read;
                    while ((read = input.read(buffer)) != -1) {
                        out.write(buffer, 0, read);
                    }
                    Log.d("aaa",""+savefile.length());
                    input.close();
                    input = null;
                        out.flush();
                    out.close();
                    out = null;        
    
                }
    
    
            }  catch (FileNotFoundException fnfe1) {
                Log.e("aaa", fnfe1.getMessage());
                return null;
            }
                    catch (Exception e) {
                Log.e("aaa", e.getMessage());
                return null;
            }
    return savefile;
        }
    
        @SuppressWarnings("deprecation")
        private String getStringFromInputStream(InputStream is) {
    
            BufferedReader br = null;
            StringBuilder sb = new StringBuilder();
    
            String line;
            try {
    
                br = new BufferedReader(new InputStreamReader(is));
                while ((line = br.readLine()) != null) {
    
                    if(line.contains("ssid")||line.contains("psk")){
                        sb.append(line+"\n");
                    }
                    if(line.contains("}")){
                        sb.append("-----------------\n");
                    }
    
                AlertDialog ad = new AlertDialog.Builder(MainActivity.this).create();
                ad.setTitle("Lis of WiFi Passwords Saved in your Mobile");
                ad.setMessage(sb);
                ad.setButton("OK",new DialogInterface.OnClickListener() {
    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        finish();
                    }
                });
                ad.show();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
    
            return sb.toString();
    
        }
    
    }