Unable to return IP address using react-native-device-info's getIPAddress()

3.7k Views Asked by At

I need to return the IP address for the device running my React Native app (an Android smart tv app). I am making use of react-native-device-info which has allowed me to get the model, manufacturer and operating system. However I am unable to get the ip address.

This is my code

deviceInfo = DeviceInfo.getIPAddress().then(ip => {
  return ip;
});

However on the front end it appears as [object Object]. I can see in the console it is returning an object like this:

wifi:
  _40: 0
  _55: null
  _65: 0
  _72: null

I would have hoped to just return a string of the correct IP address.

I have also added the right permissions in my AndroidManifest.xml.

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

Also worth noting I am passing the information back by value: ${JSON.stringify(deviceInfo)}

Any one experienced this issue before?

2

There are 2 best solutions below

0
On

I have used below library: https://www.npmjs.com/package/react-native-network-info

And it is working fine, Below is the code:

// Get IPv4 IP (priority: WiFi first, cellular second)
NetworkInfo.getIPV4Address().then(ipv4Address => {
  console.log(ipv4Address); //result e.g 192.168.1.100
});
1
On

Since its returning a promise try putting inside a async function and try to get the result.

Ex:

const getIpAddress = async()=>{
const ip = await DeviceInfo.getIPAddress();
console.log(ip);
return ip;
}

Hope it helps. Thank you.