How to write a file inside system/etc/wifi/wpa_supplicant.conf file using android program?

别等时光非礼了梦想. 提交于 2021-01-28 22:58:15

问题


I am working on wifi related app, which takes the user giving username password and need to write(edit/append)in a file which is located in system/etc/wifi/ path.

I searched in stackoverflow and got some answers and then tried but there is no luck. This is rooted device. I did remount and su too. I can able to read the same file. I placed all the permissions in manifest too. What I missed?

try {
    String[] command = new String[]{"/system/bin/ls", "-l",
            "/system/etc/wifi/wpa_supplicant.conf" };
    Process process = Runtime.getRuntime().exec(command);
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(process.getInputStream()));
    int read;

    String output = "";
    String line;
    while ((line = reader.readLine()) != null) {
        output.concat(line + "\n");
        Log.w("myApp", "[[output]]:" + line);
    }
    reader.close();
    process.waitFor();

    String[] command2 = new String[]{"/system/etc/wifi/chmod", "777",
            "/system/etc/wifi/wpa_supplicant.conf" };

    Runtime rt = Runtime.getRuntime();
    Process p = rt.exec(new String[]{"/system/bin/sh", "-c", "echo \"12345678\" > /system/etc/wifi/wpa_supplicant.conf"});

    File path= Environment.getDataDirectory();
    FileWriter fw = new FileWriter(new File("/system/etc/wifi/wpa_supplicant"));
    fw.write('9');
    fw.close();

    Log.d(TAG, "File Saved Successfully !!!");

    //display file saved message
        /*Toast.makeText(getBaseContext(), "File saved successfully!",
        Toast.LENGTH_SHORT).show();*/

} catch (Exception e) {
    e.printStackTrace();
}

Here is my manifest permissions (outside application tag and some more permissions also there for some other purpose ).

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/> 

And below error I am getting,

01-01 06:13:53.565: W/myApp(11205): [[output]]:-rw-rw-rw- root     root           58 2000-01-01 03:56 wpa_supplicant.conf
01-01 06:13:53.580: W/System.err(11205): java.io.FileNotFoundException: /system/etc/wifi/wpa_supplicant: open failed: EROFS (Read-only file system)
01-01 06:13:53.580: W/System.err(11205):    at libcore.io.IoBridge.open(IoBridge.java:452)
01-01 06:13:53.580: W/System.err(11205):    at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
01-01 06:13:53.580: W/System.err(11205):    at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
01-01 06:13:53.581: W/System.err(11205):    at java.io.FileWriter.<init>(FileWriter.java:42)
01-01 06:13:53.581: W/System.err(11205):    at com.intel.sunnypoint.headless.HeadlessService.WriteBtn(HeadlessService.java:450)
01-01 06:13:53.581: W/System.err(11205):    at com.intel.sunnypoint.headless.HeadlessService.setupWifiAPConfig(HeadlessService.java:386)
01-01 06:13:53.581: W/System.err(11205):    at com.intel.sunnypoint.headless.HeadlessService$HttpServiceThread$WiFiPageHandler.handle(HeadlessService.java:1094)
01-01 06:13:53.581: W/System.err(11205):    at org.apache.http.protocol.HttpService.doService(HttpService.java:248)
01-01 06:13:53.581: W/System.err(11205):    at org.apache.http.protocol.HttpService.handleRequest(HttpService.java:192)
01-01 06:13:53.581: W/System.err(11205):    at com.intel.sunnypoint.headless.HeadlessService$HttpServiceThread.run(HeadlessService.java:663)
01-01 06:13:53.581: W/System.err(11205): Caused by: android.system.ErrnoException: open failed: EROFS (Read-only file system)
01-01 06:13:53.581: W/System.err(11205):    at libcore.io.Posix.open(Native Method)
01-01 06:13:53.581: W/System.err(11205):    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
01-01 06:13:53.582: W/System.err(11205):    at libcore.io.IoBridge.open(IoBridge.java:438)
01-01 06:13:53.582: W/System.err(11205):    ... 9 more

回答1:


Did you have root access? Without root access only the mechanism of flashing an update (via recovery or OTA) can modify /system. To be accepted, such an update archive must be signed with the same key as the ROM – which is something only the developer of the ROM can do.

There might be a way via a custom recovery which lets you edit all file systems directly (twrp). But without knowing what device it is, it cannot be said if a matching recovery exists. Apart from that, it's either rooting the device or not editing any file within /system.

If you have root access, you should execute commands as root! su command should be used

Runtime.getRuntime().exec("su");

or a full path to su.bin in each command.



来源:https://stackoverflow.com/questions/40591770/how-to-write-a-file-inside-system-etc-wifi-wpa-supplicant-conf-file-using-androi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!