0

I am trying to develop a function for editing a value in the root file p2p_supplicant.conf which is located on /root/data/misc/wifi/p2p_supplicant.conf

The toast message always showing "File Not Found" my code is:

   private static final String FILE_PATH = "/root/data/misc/wifi/p2p_supplicant.conf";
   private static final String MARKER_LINE = "p2p_oper_channel=";
   private static final String TEXT_TO_ADD = "11";

public void changeConfig() {

     String message = String.format("Entering Config Class");
     Toast.makeText(getApplicationContext(), message,Toast.LENGTH_LONG).show();

          List<String> fileLines = new ArrayList<String>();
          Scanner scanner = null;
          try {
             scanner = new Scanner(new File(FILE_PATH));
             while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                fileLines.add(line);
                if (line.trim().equalsIgnoreCase(MARKER_LINE)) {
                   fileLines.add(TEXT_TO_ADD);

                     String message2 = String.format("File Written");
                     Toast.makeText(getApplicationContext(), message2,Toast.LENGTH_LONG).show();
                }
             }

          } catch (FileNotFoundException e) {
             e.printStackTrace();
             String message1 = String.format("File Not found");
             Toast.makeText(getApplicationContext(), message1,Toast.LENGTH_LONG).show();
          } finally {
             if (scanner != null) {
                scanner.close();
             }
          }

          PrintWriter pw = null;
          try {
             pw = new PrintWriter(new File(FILE_PATH));
             for (String line : fileLines) {
                pw.println(line);
             }
          } catch (FileNotFoundException e) {
             e.printStackTrace();
          } finally {
             if (pw != null) {
                pw.close();
             }
          }
        }

But the code did not find the location/path of the file. Please suggest. N.B. My phone is rooted.

  • I have a solution but it is not very neat. Basically, everytime you want to change the channel you re-write the whole wpa_supplicant file with the new channel. – Ziad Halabi Apr 22 '15 at 11:14
  • Yes, I just want to do so with this application. If possible, please provide your solution, i can try with it... – Nayeem Morshed Tushar Apr 22 '15 at 11:26

1 Answers1

0

Basically, you are reading the file, changing the channel then rewriting the whole file. I am looking for a solution where you read the file first, edit it then write it back. Once I resolve that issue I will edit my answer here. Note before testing it, make a backup file for your wpa_supplicant.conf file in case errors happen.

Step 1: reading the file = check this solution: Executing a Process in Android to read a file

Step 2: looping over the String containing the text of the file and changing the operating channel and saving the the whole text in String text

Step 3: writing text into the the file

    Process p; 
            try { 
               // Preform su to get root privileges
               p = Runtime.getRuntime().exec("su"); 

               // Attempt to write a file to a root-only 
               DataOutputStream os = new DataOutputStream(p.getOutputStream()); 
               os.writeBytes("echo \""+text+"\" > /data/misc/wifi/p2p_supplicant.conf\n");
               // Close the terminal
               os.writeBytes("exit\n"); 
               os.flush(); 
               try { 
                  p.waitFor(); 
                       if (p.exitValue() != 255) { 
                          // TODO Code to run on success
                       } 
                       else { 
                           // TODO Code to run on unsuccessful
                       } 
               } catch (InterruptedException e) { 
                  // TODO Code to run in interrupted exception
               } 
            } catch (IOException e) { 
               // TODO Code to run in input/output exception
            }
Community
  • 1
  • 1
Ziad Halabi
  • 964
  • 11
  • 31
  • This Command can successfully write the value to the file. But it replaced the whole lines in the file. Is it possible to use any Linux command to read the specific line? Please suggest.. – Nayeem Morshed Tushar Apr 28 '15 at 10:14
  • I tried to do that using Linux commands to edit one line, but it didn't work. That's why I adopted this solution. If editing a line works with you, let me knw – Ziad Halabi Apr 28 '15 at 10:17
  • I have posted my code of my function, it is still not working. Please check if possible... – Nayeem Morshed Tushar Apr 28 '15 at 13:21
  • Please refer to this solution, this one works for me: http://stackoverflow.com/questions/30774066/how-to-change-a-value-of-a-file-using-java-io-function. At first I have copy the content of the file to external storage using `"su", "/bin/sh", "cat /data/misc/wifi/p2p_supplicant.conf > /sdcard/config.temp"`, then apply this approach and sent the modified content back to original location again using `cat`... quit stupid approach ahh !!! – Nayeem Morshed Tushar Jun 12 '15 at 09:11