Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect when the device gets a new IP?

Tags:

android

ip

dhcp

For multicast purposes, I'm looking for a simple way to detect when the IP of an Android device changes. How can I go about doing so?

More specifically, I'm looking to detect:

  • When the device connects to a new Wifi network and it gets an IP from the DHCP
  • When the device for some reason needs to renew an IP
like image 242
yydl Avatar asked Dec 23 '11 04:12

yydl


People also ask

Does IP change with device?

When a device is assigned a static IP address, the address does not change. Most devices use dynamic IP addresses, which are assigned by the network when they connect and change over time.

How do I scan all IP addresses on my network?

To see all of the devices connected to your network, type arp -a in a Command Prompt window. This will show you the allocated IP addresses and the MAC addresses of all connected devices.

How is IP address assigned to the new device?

Your IP address is assigned to your device by your ISP. Your internet activity goes through the ISP, and they route it back to you, using your IP address. Since they are giving you access to the internet, it is their role to assign an IP address to your device. However, your IP address can change.


1 Answers

You can do this with the ConnectivityManager:

You can use this to query the current connection state:

ConnectivityManager connMananger = (ConnectivityManager) 
            context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo netInfo = connMananger.getActiveNetworkInfo();

The current IP address of network interfaces can be acquired with NetworkInterface.getNetworkInterfaces()

And you can receive automatic notification of when the connection state changes via the CONNECTIVITY_ACTION broadcast

like image 86
Thomas Dignan Avatar answered Sep 30 '22 23:09

Thomas Dignan