3

Is it possible to create a Wi-Fi hotspot from an Android device using its Wi-Fi connection rather than Cellular data?

Without the help of any third-party application (Like Netshare), by changing some code in setting application?

Note:
I'm having AOSP 7.0 so there is no question of the root device.

  • 1
    @IrfanLatif Even with root I think not possible on most devices. A wifi chip that is in AP mode can't be in client mode at the same time. – confetti Aug 31 '18 at 11:02
  • Related: Android as WiFi bridge with AP – Izzy Aug 31 '18 at 15:28
4

WI-FI REPEATER MODE ON ANDROID

I don't know of any straight forward tool other than Android apps (which use Wi-Fi Direct) that will let use wi-fi and hotspot at the same time. Neither it's a standard feature introduced in custom ROM's so far (AFAIK). However you can do this manually if you are comfortable with commandline usage. But it's NOT possible without root.

WHAT'S NEEDED

  • Rooted device
  • Device must support nl80211 driver

    All newer devices with Qualcomm MSM chipset support this AFAIK e.g. Xiaomi's Redmi Note 4 (mido) built with MSM8953.

    Kernel must also support this driver. To confirm:

    ~# zcat /proc/config.gz | grep CONFIG_CFG80211
    CONFIG_CFG80211=y
    
    ~# lshw | grep wireless=
           configuration: broadcast=yes driver=wcnss_wlan multicast=yes wireless=Qcom:802.11n
    
  • Device must support managed mode and AP mode at the same time.

    ~# iw phy | grep -iA2 'valid interface combinations'
            valid interface combinations:
             * #{ managed } <= 3, #{ IBSS, AP } <= 1, #{ P2P-client, P2P-GO } <= 1,
               total <= 3, #channels <= 1
    

    This means that your wireless chip supports creating at maximum 3 interfaces, one of which can be in AP mode, with others being in managed or P2P mode, and at maximum 1 channel is supported. If channels value is 2, you can operate both interfaces on different channels. However using same channel is recommended to avoid interference.

    Standard wi-fi interface on Android devices (usually wlan0) is always run in managed mode. We are going to create a virtual wireless interface to be run in AP mode.

  • Linux tools: iw, ip, iptables, hostapd, dnsmasq etc.

    You can also use wpa_supplicant in place of hostapd with slightly different configuration.

    Usually these binaries come bundled with Android. However there are modifications by Google and vendors to make these tools fit for Android needs. So they may not behave as standard Linux tools and in rare cases you might need to compile them from source.

    Wireless tools should be newer enough to have support for driver nl80211.

  • Terminal emulator (Termux is a good one)

STEPS:

For ease of use, I have summarized all steps in a shell script with brief explanation. You can put this on your $PATH e.g. /system/bin/android_ap and execute it directly: ~# android_ap start. A Custom Quick Settings Tile can also be created for ease of use.

#!/system/bin/sh
set -e
#set -x

[ "$(id -u)" != 0 ] && echo 'Not running as root!' && exit

SSID=MyAP                           # set this to your desired string (avoid spaces and non-ascii characters)
PASSCODE=foobarfoobar               # set this to your desired string (8 to 63 characters)
WIFI_INTERFACE=wlan0                # set this according to your device (lshw | grep -A10 Wireless | grep 'logical name')
SUBNET=192.168.42                   # must be different than WIFI_INTERFACE
AP_INTERFACE=${WIFI_INTERFACE}-AP
IP=${SUBNET}.1
DIR=/data/local/tmp/$AP_INTERFACE

USAGE()
{
    echo 'Usage:'
    printf '\t%s\n' "$(basename "$0") start|stop"
    exit
}

STOP() {
    # hope there are no other instances of same daemons
    pkill -15 hostapd dnsmasq
    # remove iptables rules
    iptables -D INPUT -i $AP_INTERFACE -p udp -m udp --dport 67 -j ACCEPT
    iptables -t nat -D POSTROUTING -s ${SUBNET}.0/24 ! -o $AP_INTERFACE -j MASQUERADE
    iptables -D FORWARD -i $AP_INTERFACE -s ${IP}/24 -j ACCEPT
    iptables -D FORWARD -i $WIFI_INTERFACE -d ${SUBNET}.0/24 -j ACCEPT
    # delete AP interface
    ip link show | grep "${AP_INTERFACE}:" && iw $AP_INTERFACE del
    rm -rf $DIR
} >/dev/null 2>&1

CHECKS()
{
    for binary in iw ip iptables hostapd dnsmasq; do
        which $binary >/dev/null && continue
        exit
    done

    # this check is necessary if need to use single channel
    if iw dev $WIFI_INTERFACE link | grep -q '^Not connected'
    then
        echo 'First connect to Wi-Fi for internet sharing.'
        exit
    fi

    if ! iw phy | grep -iqE '{.*managed.*AP.*}' && ! iw phy | grep -iqE '{.*AP.*managed.*}'
    then
        echo 'AP mode not supported.'
        exit
    fi
}

CREATE_AP()
{
    if ! iw dev $WIFI_INTERFACE interface add $AP_INTERFACE type __ap
    then
        echo "Couldn't create AP."  # :(
        exit
    fi
}

FIND_CHANNEL()
{
    # find what channel wi-fi is using
    CHANNEL="$(iw $WIFI_INTERFACE scan | grep -C5 "$(iw $WIFI_INTERFACE link | grep SSID | cut -d: -f2-)" | grep -i channel | tail -c3)"
    if [ -z "$CHANNEL" ]
    then
        echo  "Couldn't find channel info. Are you are connected to Wi-Fi?"
        STOP
        exit
    fi

    # if more than 1 channels are supported, use any frequency
    [ ! -z "$CHANNEL" ] || CHANNEL=11
}

ADD_IP_ROUTE()
{
    # activate the interface and add IP
    ip link set up dev $AP_INTERFACE
    ip addr add ${IP}/24 broadcast ${SUBNET}.255 dev $AP_INTERFACE

    # routing table 97 needs to be put necessarily on Android
    # because in main table, route for $WIFI_INTERFACE takes priority (ip route show)
    # and all traffic goes there ignoring $AP_INTERFACE
    ip route add ${SUBNET}.0/24 dev $AP_INTERFACE table 97
}

HOSTAPD_CONFIG()
{
    mkdir -p "$DIR"
    cat <<-EOF >$DIR/hostapd.conf
        # network name
        ssid=$SSID
        # network interface to listen on
        interface=$AP_INTERFACE
        # wi-fi driver
        driver=nl80211
        # WLAN channel to use
        channel=$CHANNEL
        # ser operation mode, what frequency to use
        hw_mode=g
        # enforce Wireless Protected Access (WPA)
        wpa=2
        # passphrase to use for protected access
        wpa_passphrase=$PASSCODE
        # WPA protocol
        wpa_key_mgmt=WPA-PSK
    EOF

    # you can tune other parameters such as mtu, beacon_int, ieee80211n, wowlan_triggers (if supported)
    # for better performace and options such as *_pairwise for better security
}

INTERNET_SHARE()
{
    # allow IP forwarding
    echo 1 >/proc/sys/net/ipv4/ip_forward
    # route and allow forwrding through firewall
    iptables -t nat -I POSTROUTING -s ${SUBNET}.0/24 ! -o $AP_INTERFACE -j MASQUERADE
    iptables -I FORWARD -i $AP_INTERFACE -s ${IP}/24 -j ACCEPT
    iptables -I FORWARD -i $WIFI_INTERFACE -d ${SUBNET}.0/24 -j ACCEPT
}

DHCP_SERVER()
{
    # configuration
    cat <<-EOF >$DIR/dnsmasq.conf
        # we dont want DNS server, only DHCP
        port=0
        # only listen on AP interface
        interface=$AP_INTERFACE
        listen-address=$IP

        #bind-interfaces

        # range of IPs to make available to wlan devices andwhen to renew IP
        dhcp-range=$IP,${SUBNET}.254,24h
        # where to save leases
        dhcp-leasefile=$DIR/dnsmasq.leases
        # set default gateway
        dhcp-option-force=option:router,$IP
        # add OpenDNS servers for DNS lookup to announce
        dhcp-option-force=option:dns-server,208.67.220.220,208.67.222.222

        #dhcp-option-force=option:mtu,1500

        # respond to a client who is requesting from a different IP broadcast subnet
        # or requesting an out of range / occupied IP
        # or requesting an IP from expired lease of previous sessions
        # or obtained from some other server which is offline now
        dhcp-authoritative
        # don't look for any hosts file and resolv file
        no-hosts
        no-resolv
    EOF

    # open listening port
    iptables -I INPUT -i $AP_INTERFACE -p udp -m udp --dport 67 -j ACCEPT

    # start dhcp server
    dnsmasq -C $DIR/dnsmasq.conf
}

if [ "$1" = stop ]
then
    STOP || true
    exit
fi

[ "$1" = start ] || USAGE

# basic check
CHECKS
# stop running instances
STOP || true
# create virtual wireless interface
CREATE_AP
# find channed already used ny wi-fi
FIND_CHANNEL
# configre newly created interface
ADD_IP_ROUTE
# configure acces point daemon
HOSTAPD_CONFIG
# start hostapd
hostapd -B $DIR/hostapd.conf
# share internet from Wi-Fi to AP
INTERNET_SHARE
# run a dhcp server to assign IP's dynamically
# otherwise assign a static IP to connected device in subnet range (2 to 254)
DHCP_SERVER

echo Done.

SOURCES:

0

You can not simply do it without root. But you can use USB and BlueTooth tethering using a Wi-Fi network. I would suggest using BlueTooth tethering if you want to share internet on other phones because it is a mess to do all that to use Wi-Fi and hotspot at same time.

  • 1
    I am using Xiaomi Redmi Note 6 Pro. It can turn both WiFi and HotSpot at same time, without root. – user277945 Oct 29 '18 at 9:11
0

There is an app called VPN Hopspot - Tethering/Wi-Fi repeater on Google Play, it's open sourced on GitHub at here. Should work on most recent android devices powered by snapdragon SoC (root required).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.