expo-network
provides useful information about the device's network such as its IP address, MAC address, and airplane mode status.Android Device | Android Emulator | iOS Device | iOS Simulator | Web |
---|---|---|---|---|
→
expo install expo-network
If you're installing this in a bare React Native app, you should also follow these additional installation instructions.
ACCESS_NETWORK_STATE
and ACCESS_WIFI_STATE
are added automatically.import * as Network from 'expo-network';
Gets the device's current IPv4 address. Returns 0.0.0.0
if the IP address could not be retrieved.
On web, this method uses the third-party ipify service
to get the
public IP address of the current device.
await Network.getIpAddressAsync(); // "92.168.32.44"
Promise<string>
A Promise
that fulfils with a string
of the current IP address of the device's main
network interface. Can only be IPv4 address.
Name | Type | Description |
---|---|---|
interfaceName | null | string | A string representing interface name (eth0 , wlan0 ) or null (default),
meaning the method should fetch the MAC address of the first available interface. |
Deprecated. This method is deprecated and will be removed in a future SDK version.
Gets the specified network interface's MAC address.
Beginning with iOS 7 and Android 11, non-system applications can no longer access the device's
MAC address. In SDK 41 and above, this method will always resolve to a predefined value that isn't useful.
If you need to identify the device, use the getIosIdForVendorAsync()
method / androidId
property of the expo-application
unimodule instead.
Promise<string>
A Promise
that fulfils with the value '02:00:00:00:00:00'
.
Gets the device's current network connection state.
On web, navigator.connection.type
is not available on browsers. So if there is an active
network connection, the field type
returns NetworkStateType.UNKNOWN
. Otherwise, it returns
NetworkStateType.NONE
.
await Network.getNetworkStateAsync(); // { // type: NetworkStateType.CELLULAR, // isConnected: true, // isInternetReachable: true, // }
A Promise
that fulfils with a NetworkState
object.
Tells if the device is in airplane mode.
await Network.isAirplaneModeEnabledAsync(); // false
Promise<boolean>
Returns a Promise
that fulfils with a boolean
value for whether the device is in
airplane mode or not.
Name | Type | Description |
---|---|---|
isConnected (optional) | boolean | If there is an active network connection. Note that this does not mean that internet is reachable.
This field is false if the type is either Network.NetworkStateType.NONE or Network.NetworkStateType.UNKNOWN ,
true otherwise. |
isInternetReachable (optional) | boolean | If the internet is reachable with the currently active network connection. On Android, this
depends on NetInfo.isConnected() (API level < 29) or ConnectivityManager.getActiveNetwork()
(API level >= 29). On iOS, this value will always be the same as isConnected . |
type (optional) | NetworkStateType | A NetworkStateType enum value that represents the current network
connection type. |
An enum of the different types of devices supported by Expo.
BLUETOOTH
- Active network connection over Bluetooth.NetworkStateType.BLUETOOTH = "BLUETOOTH"
CELLULAR
- Active network connection over mobile data or DUN-specific
mobile connection when setting an upstream connection for tethering.NetworkStateType.CELLULAR = "CELLULAR"
ETHERNET
- Active network connection over Ethernet.NetworkStateType.ETHERNET = "ETHERNET"
NONE
- No active network connection detected.NetworkStateType.NONE = "NONE"
OTHER
- Active network connection over other network connection types.NetworkStateType.OTHER = "OTHER"
UNKNOWN
- The connection type could not be determined.NetworkStateType.UNKNOWN = "UNKNOWN"
VPN
- Active network connection over VPN.NetworkStateType.VPN = "VPN"
WIFI
- Active network connection over WiFi.NetworkStateType.WIFI = "WIFI"
WIMAX
- Active network connection over Wimax.NetworkStateType.WIMAX = "WIMAX"
Code | Description |
---|---|
ERR_NETWORK_IP_ADDRESS | On Android, there may be an unknown Wi-Fi host when trying to access WifiManager in getIpAddressAsync . On iOS, no network interfaces could be retrieved. |
ERR_NETWORK_UNDEFINED_INTERFACE | An undefined interfaceName was passed as an argument in getMacAddressAsync . |
ERR_NETWORK_SOCKET_EXCEPTION | An error was encountered in creating or accessing the socket in getMacAddressAsync . |
ERR_NETWORK_INVALID_PERMISSION_INTERNET | There are invalid permissions for android.permission.ACCESS_WIFI_STATE in getMacAddressAsync . |
ERR_NETWORK_NO_ACCESS_NETWORKINFO | Unable to access network information |