6

I want to connect and transfer data between a PC and an Android device both of which are on the same local WiFi network.

I cannot use the local IP as such in the code to actually get the connection going because it does not remain constant every time. I know I can set the local IP to be constant but I am looking for a more general solution to the problem.

Having a central server is also not what I'm looking for, because I want to transfer data offline.

I am not an expert on networks as you might have already guessed, so if I am missing something out, let me know. Also is there some API in android that could do this?

1 Answer 1

11

You should look at broadcasting. This is technique of sending your packets to all devices (IP's) in a subnet. It would look something like that:

  • Android 192.168.0.101: Send message packet to broadcast address 192.168.0.254
  • PC 192.168.0.110: Reply to 192.168.0.101's broadcast message
  • Android now knows PC's IP address and can communicate directly

It's really simple. You can find JAVA sample in this SO question: https://stackoverflow.com/questions/12999425/simple-udp-broadcast-client-and-server-on-different-machines

6
  • +1 but is this the most efficient way to go about this?
    – shyam
    Commented Dec 5, 2014 at 9:18
  • 2
    it is the ONLY way to go about it.
    – Mawg
    Commented Dec 5, 2014 at 15:33
  • @Mawg I'm gonna take your word for it then.
    – shyam
    Commented Dec 6, 2014 at 4:40
  • Think about it. If you want to sent a letter (how quaint) to that nice grrrl who smiled at you in the pub lat night, your going to have to walk around town pushing a note throw every letter box asking her to get in touch
    – Mawg
    Commented Dec 6, 2014 at 9:24
  • Well, its more similar to asking a postman of giving letters to all the girls in town. One more alternative is phonebook, or in your case - some kind of external site/api (tracker in bitorrent terms).
    – Dissimilis
    Commented Dec 6, 2014 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.