editing a value in the file p2p_supplicant.conf which is located on /root/data/misc/wifi/p2p_supplicant.conf

前端 未结 1 1200
野性不改
野性不改 2021-01-26 11:15

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 messa

相关标签:
1条回答
  • 2021-01-26 11:39

    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
                }
    
    0 讨论(0)
提交回复
热议问题