1

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?

G. Cox
  • 97
  • 1
  • 2
  • 7
  • Use react-native-network-info:https://github.com/pusherman/react-native-network-info – Kabir Aug 19 '19 at 10:26
  • you could try using a brief sleep to allow the task of getting the IP be completed before referencing its return. That way depending on how many times its called the timer thread would guarantee its return. And also catching exceptions might help. – OlaB Aug 19 '19 at 11:37

2 Answers2

1

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
});
Nishant Upadhyay
  • 639
  • 7
  • 20
0

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.