Track application id or package name using DatagramPacket in android

49 Views Asked by At

I open a socket and track network calls in the Android application. Currently, I can view the hostname and IP address of the coming network traffic using the DatagramPacket class. Is it possible to get the application id or package name using it? Or is it possible to get the application ID or package name from IPPacket in DNSResolver?

1

There are 1 best solutions below

0
On

After a long search and reading documentation of Android related to networks, I found the solution to get the application ID that the OS assigns. Through this ID it's possible to find the network packet's source. It's not possible to get the package name through DatagramPacket or any network calls.

The ID is an integer before comparing the ID it's important to get the application ID which was assigned by the OS using PackageManager.

val local = InetSocketAddress(sourceHostAddress, sourcePort)
val remote = InetSocketAddress(destinationHostAddress, destinationPort)
val connectivityManager = instance.getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager;
val uId = connectivityManager.getConnectionOwnerUid(protocol, local, remote);

Protocol TCP = 6 UDP = 17