How To Guide How to configure the WiFI in Android via script

Search This thread

bnsmb

Senior Member
Aug 22, 2017
229
118
Frankfurt
In the current version of Android the WiFi configuration is stored in the file

/data/misc/apexdata/com.android.wifi/WifiConfigStore.xml

e.g.

Code:
ASUS_I006D:/ # ls -lZtr /data/misc/apexdata/com.android.wifi/WifiConfigStore.xml                                        
-rw------- 1 system system u:object_r:apex_system_server_data_file:s0   4884 2023-01-29 08:55 /data/misc/apexdata/com.android.wifi/WifiConfigStore.xml
ASUS_I006D:/ #


The file is in plain ASCII xml format so it can be processed by any executable to change text files (editor, sed, etc).
But IMHO it's better to configure the WiFi manually via GUI and then use the file WifiConfigStore.xml with that configuration for configuring the WiFi via script. Therefor I use this code in the post install script to install and configure my phone (see How to install and configure the Android OS via Script):

Bash:
#
# sample post install script for the customizing of the phone
#
# This script will be copied to the phone and executed there
#
# The script is executed by the user shell; use "su - -c <command>" to execute commands as user root
#
echo ""
echo "*** Postinstall script is running ..."
echo ""
 
# ...  other customizations ....
 
#
# create the WiFi config file
#
# -rw------- 1 system system u:object_r:apex_system_server_data_file:s0 4884 2023-01-29 08:55 /data/misc/apexdata/com.android.wifi/WifiConfigStore.xml
#
WIFI_CONFIG_FILE="/data/misc/apexdata/com.android.wifi/WifiConfigStore.xml"
WIFI_CONFIG_FILE_BACKUP="${WIFI_CONFIG_FILE}.$$.bkp"
NEW_WIFI_CONFIG_FILE="/sdcard/Download/WifiConfigStore.xml"
 
if [ -r "${WIFI_CONFIG_FILE}" ] ; then
 echo "Creating a backup of the file \"${WIFI_CONFIG_FILE}\" in \"${WIFI_CONFIG_FILE_BACKUP}\" ...."
 cp "${WIFI_CONFIG_FILE}" "${WIFI_CONFIG_FILE_BACKUP}"
fi
 
echo "Creating the file \"${NEW_WIFI_CONFIG_FILE}\" ..."
cat >"${NEW_WIFI_CONFIG_FILE}" <<-\EOT
 
***  add here the contents of the file /data/misc/apexdata/com.android.wifi/WifiConfigStore.xml from the phone after manually configuring the WLAN
 
EOT
if [ $? -eq 0 ] ; then
 echo "Creating the file \"${WIFI_CONFIG_FILE}\" ..."
 su - -c cp "${NEW_WIFI_CONFIG_FILE}" "${WIFI_CONFIG_FILE}" && \
   su - -c chmod 600 "${WIFI_CONFIG_FILE}" && \
   su - -c chown system:system "${WIFI_CONFIG_FILE}" && \
   su - -c chcon -v "u:object_r:apex_system_server_data_file:s0" "${WIFI_CONFIG_FILE}"
 if [ $? -ne 0 ] ; then
   echo "Error creating the file \"${WIFI_CONFIG_FILE}\" "
 fi
else
 echo "Error creating the file \"${NEW_WIFI_CONFIG_FILE}\" "
fi
 
# enable WiFi
#
echo "Enabling WiFi ..."
 
svc wifi enable
 
# optional: Disable mobile data connections
#
echo "Disabling mobile data ..."
 
svc data disable
 
# Now reboot the phone to activate the new WiFi config ..
#
echo "Waiting 15 seconds now before rebooting - press CTRL-C to abort ...."
i=0
while [ $i -lt 15 ] ; do
 (( i = i + 1 ))
 printf "."
 sleep 1
done
printf "\n"
 
echo "Now rebooting the phone ..."
 
reboot
#

Notes:

Be aware the the file /data/misc/apexdata/com.android.wifi/WifiConfigStore.xml contains the passwords for all configured WLANs in plain text.

There should be a command to force Android to reread the file WifIConfigStore.xml but I don't know that.

Android can be forced to re-read the file WifIConfigStore.xml by killing the process system_server, e.g:

Bash:
pkill system_server

But that is more or less a warm reboot and I do not know what other side effects that restart triggers


There is an app to configure WiFi via adb command

https://github.com/steinwurf/adb-join-wifi

Bash:
adb shell am start -n com.steinwurf.adbjoinwifi/.MainActivity -e ssid myssid -e password mywlan_password  -e password_type WPA

The app works but unfortunately it does not support enabling the setting to use the device MAC instead of the random MAC so it's not usable in my environment.


Another app to configure WiFi via adb command is available here:

https://github.com/pr4bh4sh/adb-wifi-setting-manager


Unfortunately that app also does not support enabling choosing the MAC address for the connection.


There is also a script to convert an old Android wpa_supplicant.conf file to the newer (post-Oreo) WifiConfigStore.xml file (not tested):

https://github.com/mnalis/android-wifi-upgrade


It's also possible to use the command wpa_cli to configure WiFi but be aware the wpa_cli only works if selinux is (temporary) disabled, example:

Code:
ASUS_I006D:/ $ su -                                                                                                                                                                             
ASUS_I006D:/ #
ASUS_I006D:/ # setenforce 0
ASUS_I006D:/ #
ASUS_I006D:/ #
ASUS_I006D:/ # wpa_cli                                                                                                                                                                         
wpa_cli v2.10-devel-11
Copyright (c) 2004-2019, Jouni Malinen <[email protected]> and contributors
 
This software may be distributed under the terms of the BSD license.
See README for more details.
 
 
Using interface 'wlan0'
 
Interactive mode
 
<3>Control interface command 'BSS RANGE=ALL MASK=0x2'
<3>Control interface command 'LIST_CREDS'
<3>Control interface command 'LIST_NETWORKS'
<3>Control interface command 'STA-FIRST'
>    
> list_networks
network id / ssid / bssid / flags
0    Zeilsheim    any    [CURRENT]
1        any    [DISABLED]
<3>Control interface command 'LIST_NETWORKS'
>
> quit
ASUS_I006D:/ #

Note: Use help to get list of known commands


Test Environment

I tested the WiFi config via script described above in these Android distributions:

OmniROM 13 (Android 13)
OmniROM 12 (Android 12)
ASUS Android 12
ASUS Android 13

This method seems not to work in Android 11
 
  • Like
Reactions: mikulik86

Top Liked Posts

  • There are no posts matching your filters.
  • 1
    In the current version of Android the WiFi configuration is stored in the file

    /data/misc/apexdata/com.android.wifi/WifiConfigStore.xml

    e.g.

    Code:
    ASUS_I006D:/ # ls -lZtr /data/misc/apexdata/com.android.wifi/WifiConfigStore.xml                                        
    -rw------- 1 system system u:object_r:apex_system_server_data_file:s0   4884 2023-01-29 08:55 /data/misc/apexdata/com.android.wifi/WifiConfigStore.xml
    ASUS_I006D:/ #


    The file is in plain ASCII xml format so it can be processed by any executable to change text files (editor, sed, etc).
    But IMHO it's better to configure the WiFi manually via GUI and then use the file WifiConfigStore.xml with that configuration for configuring the WiFi via script. Therefor I use this code in the post install script to install and configure my phone (see How to install and configure the Android OS via Script):

    Bash:
    #
    # sample post install script for the customizing of the phone
    #
    # This script will be copied to the phone and executed there
    #
    # The script is executed by the user shell; use "su - -c <command>" to execute commands as user root
    #
    echo ""
    echo "*** Postinstall script is running ..."
    echo ""
     
    # ...  other customizations ....
     
    #
    # create the WiFi config file
    #
    # -rw------- 1 system system u:object_r:apex_system_server_data_file:s0 4884 2023-01-29 08:55 /data/misc/apexdata/com.android.wifi/WifiConfigStore.xml
    #
    WIFI_CONFIG_FILE="/data/misc/apexdata/com.android.wifi/WifiConfigStore.xml"
    WIFI_CONFIG_FILE_BACKUP="${WIFI_CONFIG_FILE}.$$.bkp"
    NEW_WIFI_CONFIG_FILE="/sdcard/Download/WifiConfigStore.xml"
     
    if [ -r "${WIFI_CONFIG_FILE}" ] ; then
     echo "Creating a backup of the file \"${WIFI_CONFIG_FILE}\" in \"${WIFI_CONFIG_FILE_BACKUP}\" ...."
     cp "${WIFI_CONFIG_FILE}" "${WIFI_CONFIG_FILE_BACKUP}"
    fi
     
    echo "Creating the file \"${NEW_WIFI_CONFIG_FILE}\" ..."
    cat >"${NEW_WIFI_CONFIG_FILE}" <<-\EOT
     
    ***  add here the contents of the file /data/misc/apexdata/com.android.wifi/WifiConfigStore.xml from the phone after manually configuring the WLAN
     
    EOT
    if [ $? -eq 0 ] ; then
     echo "Creating the file \"${WIFI_CONFIG_FILE}\" ..."
     su - -c cp "${NEW_WIFI_CONFIG_FILE}" "${WIFI_CONFIG_FILE}" && \
       su - -c chmod 600 "${WIFI_CONFIG_FILE}" && \
       su - -c chown system:system "${WIFI_CONFIG_FILE}" && \
       su - -c chcon -v "u:object_r:apex_system_server_data_file:s0" "${WIFI_CONFIG_FILE}"
     if [ $? -ne 0 ] ; then
       echo "Error creating the file \"${WIFI_CONFIG_FILE}\" "
     fi
    else
     echo "Error creating the file \"${NEW_WIFI_CONFIG_FILE}\" "
    fi
     
    # enable WiFi
    #
    echo "Enabling WiFi ..."
     
    svc wifi enable
     
    # optional: Disable mobile data connections
    #
    echo "Disabling mobile data ..."
     
    svc data disable
     
    # Now reboot the phone to activate the new WiFi config ..
    #
    echo "Waiting 15 seconds now before rebooting - press CTRL-C to abort ...."
    i=0
    while [ $i -lt 15 ] ; do
     (( i = i + 1 ))
     printf "."
     sleep 1
    done
    printf "\n"
     
    echo "Now rebooting the phone ..."
     
    reboot
    #

    Notes:

    Be aware the the file /data/misc/apexdata/com.android.wifi/WifiConfigStore.xml contains the passwords for all configured WLANs in plain text.

    There should be a command to force Android to reread the file WifIConfigStore.xml but I don't know that.

    Android can be forced to re-read the file WifIConfigStore.xml by killing the process system_server, e.g:

    Bash:
    pkill system_server

    But that is more or less a warm reboot and I do not know what other side effects that restart triggers


    There is an app to configure WiFi via adb command

    https://github.com/steinwurf/adb-join-wifi

    Bash:
    adb shell am start -n com.steinwurf.adbjoinwifi/.MainActivity -e ssid myssid -e password mywlan_password  -e password_type WPA

    The app works but unfortunately it does not support enabling the setting to use the device MAC instead of the random MAC so it's not usable in my environment.


    Another app to configure WiFi via adb command is available here:

    https://github.com/pr4bh4sh/adb-wifi-setting-manager


    Unfortunately that app also does not support enabling choosing the MAC address for the connection.


    There is also a script to convert an old Android wpa_supplicant.conf file to the newer (post-Oreo) WifiConfigStore.xml file (not tested):

    https://github.com/mnalis/android-wifi-upgrade


    It's also possible to use the command wpa_cli to configure WiFi but be aware the wpa_cli only works if selinux is (temporary) disabled, example:

    Code:
    ASUS_I006D:/ $ su -                                                                                                                                                                             
    ASUS_I006D:/ #
    ASUS_I006D:/ # setenforce 0
    ASUS_I006D:/ #
    ASUS_I006D:/ #
    ASUS_I006D:/ # wpa_cli                                                                                                                                                                         
    wpa_cli v2.10-devel-11
    Copyright (c) 2004-2019, Jouni Malinen <[email protected]> and contributors
     
    This software may be distributed under the terms of the BSD license.
    See README for more details.
     
     
    Using interface 'wlan0'
     
    Interactive mode
     
    <3>Control interface command 'BSS RANGE=ALL MASK=0x2'
    <3>Control interface command 'LIST_CREDS'
    <3>Control interface command 'LIST_NETWORKS'
    <3>Control interface command 'STA-FIRST'
    >    
    > list_networks
    network id / ssid / bssid / flags
    0    Zeilsheim    any    [CURRENT]
    1        any    [DISABLED]
    <3>Control interface command 'LIST_NETWORKS'
    >
    > quit
    ASUS_I006D:/ #

    Note: Use help to get list of known commands


    Test Environment

    I tested the WiFi config via script described above in these Android distributions:

    OmniROM 13 (Android 13)
    OmniROM 12 (Android 12)
    ASUS Android 12
    ASUS Android 13

    This method seems not to work in Android 11