3

I have a project which I need to do data communication with my android phone and Raspberry Pi 3.

Firstly, I connected raspberry pi 3 to my phone's wifi hotspot. Then with using very same code like

Sender

   1 import socket #sender
   2 
   3 UDP_IP = "127.0.0.1"
   4 UDP_PORT = 5005
   5 MESSAGE = "Hello, World!"
   6 
   7 print "UDP target IP:", UDP_IP # My phone IP
   8 print "UDP target port:", UDP_PORT
   9 print "message:", MESSAGE
  10 
  11 sock = socket.socket(socket.AF_INET, # Internet
  12                      socket.SOCK_DGRAM) # UDP
  13 sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))    

which I obtained from https://wiki.python.org/moin/UdpCommunication;

I can manage the communication. However, I need to manually write UDP_IP which corresponds to my phone's IP. Problem is that sometimes the IP changes. Therefore, I need to find the phone's IP automatically so that i won't need to change.

I did the research but couldn't find it. So I need a code which finds android phone IP(not the Raspberry Pi host IP).

  • If the Raspberry-Pi IP is known/static, maybe you could write an Android app that "pings" the RPi? – jojonas May 22 '17 at 13:03
  • Or you could use [this](https://stackoverflow.com/questions/25404485/get-ip-from-mac-address-arp-a-not-showing-device) if you have the MAC address of your phone. – jojonas May 22 '17 at 13:05
  • thank you @jojonas it worked, I also realized that hotspot ip is always 192.168.43.1, so you don't have to use your phone's ip for UDP connection. – Mehmet Özkan Demir May 26 '17 at 14:57

1 Answers1

0

I would extract the gateway IP (should be your AndroidPhone) from the network info. Here you can find an algorithm to extract network info from ifconfig in Ubuntu with phyton: Algorithm to extract network info from ifconfig (ubuntu)