If you have root access and don't mind working on CLI, a shorter version of script used for How to use Android in Wi-Fi repeater mode? can be used to create a hotspot network provided that your WiFi interface supports AP mode:
#!/system/bin/sh
set -e
#set -x
# this scripts creates a hotspot network
[ "$(id -u)" = 0 ] || { echo 'Not running as root!' >&2; exit 1; }
# check required binaries are on PATH
for bin in iw ip iptables hostapd dnsmasq
do
! which $bin >/dev/null || continue
echo "$bin not found." >&2
exit 1
done
####################
# define variables #
####################
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 (check with 'lshw' or 'ip link show')
AP_INTERFACE=${WIFI_INTERFACE}-AP
DIR=/data/local/tmp/$AP_INTERFACE
SUBNET=192.168.43
IP=${SUBNET}.1
##########################
# start / stop tethering #
##########################
STOP()
(
echo 'Cleaning up...'
# don't print error messages
exec >/dev/null 2>&1
# hope there are no other instances of same daemons
pkill -15 hostapd
pkill -15 wpa_supplicant
pkill -15 dnsmasq
# remove RPDB rule and iptables rule
ip rule del lookup main
iptables -D INPUT -i $AP_INTERFACE -p udp -m udp --dport 67 -j ACCEPT
# delete AP interface
iw $AP_INTERFACE del
rm -rf $DIR
)
if [ "$1" = stop ]
then
STOP || true
exit
elif [ "$1" != start ]
then
echo 'Usage:' >&2
printf '\t%s\n' "$(basename "$0") start|stop" >&2
exit 1
fi
################
# basic checks #
################
if ! iw phy | grep -A10 'Supported interface modes:' | grep -q '\*[ ]*AP'
then
echo 'AP mode not supported.' >&2
exit 1
fi
if ! iw dev $WIFI_INTERFACE link | grep -q '^Not connected'
then
echo 'First disconnect form Wi-Fi.' >&2
exit 1
fi
##########################
# stop running instances #
##########################
STOP || true
#####################################
# create virtual wireless interface #
#####################################
if ! iw dev $WIFI_INTERFACE interface add $AP_INTERFACE type __ap
then
echo "Couldn't create AP interface." >&2
exit 1
fi
#####################################
# configure newly created interface #
#####################################
echo 'Configuring network...'
# activate the interface and add IP
ip link set up dev $AP_INTERFACE
ip addr add ${IP}/24 broadcast ${SUBNET}.255 dev $AP_INTERFACE
# Android doesn't look up into main table by default
ip rule add lookup main
#######################
# access point daemon #
#######################
# create configuration file
mkdir -p "$DIR"
cat <<-EOF >$DIR/hostapd.conf
# network name
ssid=$SSID
# passphrase to use for protected access
wpa_passphrase=$PASSCODE
# network interface to listen on
interface=$AP_INTERFACE
# wi-fi driver
driver=nl80211
# set operation mode, 'g' for 2.4GHz band
hw_mode=g
# WLAN frequency channel to use
channel=1
# key management protocol; use pre-share key
wpa_key_mgmt=WPA-PSK
# enforce WPA2
wpa=2
EOF
echo 'Starting hostapd...'
hostapd -B $DIR/hostapd.conf
################################################
# run a dhcp server to assign IP's dynamically #
################################################
# create configuration file
cat <<-EOF >$DIR/dnsmasq.conf
# we dont want DNS server, only DHCP
port=0
# nameservers to be sent to clients
dhcp-option=6,1.1.1.1,1.0.0.1
# range of IPs to make available to wlan devices and when to renew IP
dhcp-range=$IP,${SUBNET}.254,24h
# where to save leases
dhcp-leasefile=$DIR/dnsmasq.leases
# respond to requests from a different IP broadcast subnet
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
echo 'Starting DHCP server...'
dnsmasq -C $DIR/dnsmasq.conf </dev/null
echo Done.
Now you can connect to Access Point MyAP
using key foobarfoobar
.
wpa_supplicant
can also be used in place of hostapd
for simple cases like ours, but the one shipped with Android doesn't seem to work. You need to compile binary from source, or try this one. Configuration is even simpler:
...
#######################
# access point daemon #
#######################
# create configuration file
mkdir -p $DIR/wpa_supplicant
cat <<-EOF >$DIR/wpa_supplicant.conf
ctrl_interface=$DIR/wpa_supplicant
network={
ssid="MyAP"
psk="foobarfoobar"
# force create AP network
ap_scan=2
# key management protocol; use pre-share key
key_mgmt=WPA-PSK
# enforce WPA2
proto=RSA
# set mode to AP
mode=2
# channel frequency
frequency=2412
}
EOF
echo 'Starting wpa_supplicant in AP mode...'
wpa_supplicant -B -D nl80211 -i $AP_INTERFACE -c $DIR/wpa_supplicant.conf
...
hostapd
, wpa_supplicant
and dnsmasq
all are part of AOSP. hostapd
is used for wireless tethering, wpa_supplicant
for WiFi connections and dnsmasq
as DHCP/DNS server (up to Pie).
dnsmasq
isn't necessary if you prefer static IP configuration.
- If your device doesn't contain
hostapd
binary, you can get one from some other device or compile from source, or try this one.
- For more configuration options, see
hostapd.conf
, dnsmasq.conf
and wpa_supplicant.conf
. See WiFi channels detail here.
- Also make sure both
hostapd
(runs with UID 1010
AID_WIFI
) and dnsmasq
(drops to UID 9999
AID_NOBODY
) are able to make outbound connections through firewall.
- SELinux may also cause problems with this manual setup, so set
permissive
for testing purpose or define policy rules.
- Since you don't intend to use internet, there is no need to configure NAT and packet forwarding.
PS:
In case if you have cellular connection, the only extra thing to be done is internet sharing. For extra steps see How to create a WiFi hotspot from the terminal?