How can two machines detect each other when their ips keep changing? - android

Let me describe what I want to achieve and where I am stuck. It is okay to suggest an entirely different alternative.
I have an Android phone with NFC feature.
I have a macbook pro.
A wifi network.
Whenever I tap my phone on a certain NFC tag I want my Macbook to run a certain script.
So I did the following
An android application that is invoked whenever the device senses a particular NFC tag.
My Macbook runs a NodeJS HTTP server
Android app sends an HTTP request to NodeJS, NodeJS then invokes whatever script I wanted to run.
This setup works perfectly at my home where I can Set the IP address of my Macbook to whatever I want. But i want the same setup to work transparently when I am in starbucks (or workplace) and both my Android and Macbook are on the same Wifi network. (I dont want to enter the IP addresses manually).
When I was halfway through I realized that I could have used bluetooth feature on the Macbook but then I will probably have to write a sophisticated program on macbook to listen to the Android device and accept commands.

On Android, use Network Service Discovery. If you're using an earlier target, jmDNS is your friend. I have only used this to allow android devices to find each other, but this should be compatible with bonjour service on your Mac.

Solution #1
Use a network service discovery technology. I would perhaps suggest Zero Config which Apple implements as "Bonjour"
Solution #2
Have a third device with a known IP address for bookkeeping the other devices' IP (essentially what DNS is)
Solution #3
Setup your WiFi network to have static IPs : allocating predefined IPs to computer with specific MAC addresses (that way clients can still have "DHCP" but the router would always allocate the same local IP)

I'd look into dynamic dns. You can use that to automatically update dns names for a computer that changes IP addresses.

Related

How to make an Android application that will connect and read data from an alarm system (local network)?

I need to make an Android app for the problem mentioned in the title. This system already runs on Windows and is connected to the computer via an UTP cable. Now, I need to connect to the local network that I already did by connecting it to the router (I'm not sure if this is enough, but using the Fing app I detected the device in the local network), and build an Android app.
Just to mention I have tried NSD (link below) but I do not think the alarm system supports it.
https://developer.android.com/training/connect-devices-wirelessly/nsd.html
I had a problem defining the type of service. I tried to find all the devices using the code below, but unsuccessfully.
private static final String SERVICE_TYPE = "_services._dns-sd._udp";
Using this type of service I could not find anything, i.e. I did not manage to trigger the method
public void onServiceFound (NsdServiceInfo service)
My question is, should I continue with this approach (is it possible at all)? Otherwise, I need some guidance on how to connect to the device and read data(string type).
For now, I only have the IP address I received using the Fing app.
I found a partial solution for my problem. This is how to connect device and check are data is transferring (in case the someone have similar problem).
You need to do next:
connect device direct to yours computer with Ethernet cable
used WireShark app to find ip address, port and protocol (utp
protocol in my case)
after get device data, disconect device and reconnect directly with yours modem (no need for aditional device like ethernet switch, or to do the port forwarding)
you can use some udp client app on android phone to check if data
are receiving (I used TCP Sender / Receiver). Just set ip address
and port.
My problem was because I used wrong UDP client app. That's the reason it didn't working from the beginning.

Android: Client-Server using TCP, and discovery of PC/Mac on LAN (WiFi)

I want to create app in client-server model. So I written desktop server and mobile client library. Now I encounter some problem with automatically discover PC/Mac computer in LAN network from Android device.
Possible solutions:
get android device ip address, get subnet mask, use org.apache.commons.net.util.SubnetUtils class to getInfo().getAllAddresses() and scan them all using InetAddress.isReachable(timout=1000ms) This solution works but if we have optimistic subnet mask 255.255.255.0 it must scan 254 addresses and it takes 254 seconds => > 4min. Ok I could use threads, maybe addition of 10 threads and splitting this task to check about 26 ip addresses could make it below <45 seconds. But there also can be subnet mask 255.255.0.0 (less probable but...).
Android don't have arp -a command line tool, but it has something like /proc/net/arp file? I am reading this file and can get some ip addresses in LAN very fast, I can reject IP addresses that don't have real MAC but 00:00:00:00:00:00. I could also find then vendor of this device also using some api, local database?
The problem is that I have read this file has some drawbacks like contains non existent devices (after their disconnection)? and not all devices actually connected? Is it right? So Can I relay on it or not?
Both above solutions enable to find only IP address, what about port number of my server app. Ok I could use fixed port number (but it can be used by some other service and then there will be problem). Maybe trying consecutive addresses from xxxx port number like, xxxx+1, xxxx+2,.... etc. to the moment I find some on which I can correct. So the connection could take reasonable amount of time I suppose.
I have read about NSD or DNS-SD:
https://developer.android.com/training/connect-devices-wirelessly/nsd.html
This seems to enable register some service (In my case my server-side app) and enable it to be discovered in local network by other devices? Is this useable in the case of PC/MacOS server app registering for discovery, and Android App finding this service using NSD?
When I have IP address how can I get name of the device:
i.e. name that is displayed here "Play Internet 4G..." which is my wifi router.
or the name of my computer that is displayed here
I think using NSD (Network Service Discovery) implementing DNS-SD (Service Discovery) and on MacOS (also Windows, iOS) Bonjour
http://developer.apple.com/bonjour
Will be the best way to implement discovery of devices (services offered by client-server local network software) in LAN. You can get also descriptive names like name of the computer or server app running on that computer to show the end user. There is dns-sd command line tool in Terminal on macOS you can test registration of services in network and their later discovery. It works perfectly well and fast. Example screenshot of my Android app discovering service registered in macOS command line tool.
Watch more:
https://developer.apple.com/videos/play/wwdc2011/211/

Accessing WiFi LAN (no gateway) and mobile network simultaneously

I'm aware it's been asked before (with limited or no solution) but I haven't seen any recent updates and I think my scenario is unique so I'll start a new thread.
I have a Raspberry Pi and it's functioning as a hot spot among other things. By design, DHCP isn't handing out a default gateway. My use case is one or more devices interacting with the Pi as a server.
One of the connected devices will be a mobile device(edit: running a custom app of my design which is part of the total solution), and that device will maintain its cellular connection, not for tethering or routing but so that the application can access internal and external resources simultaneously.
Currently:
IOS: This works exactly as I expect on IOS - it senses that the WiFi network doesn't have a default gateway and sends external traffic over cell.
Android: This doesn't work at all. Android stubbornly sees WiFi connected and disables cellular, even with no default gateway.
Windows Phone: Kind of a hybrid. Both networks stay up but my scenario has a DNS entry in public DNS, and a public site links to a resource on the Pi. If I link to the resource directly using the WiFi IP address of the Pi, it works fine (although this causes other issues related to security). If I link via a FQDN, I'm actually not sure what's going on. It doesn't resolve from the web browser although every networking utility I've installed on the phone for troubleshooting correctly resolves the FQDN to the Pi address via DNS.
Hope this makes sense. The question: How can I enable this functionality across the board in 2015? :) For my purposes, the Pi as a private nonroutable hotspot works better than WiFi direct or Bluetooth. And remember I'm not asking the phone to ROUTE; I need my custom application to be able to simultaneously connect to the Pi and to the public Internet. I want all the phones to do what IOS does in this regard.
My current scenario is the Pi hosting a web page (among other things) and that's my preference, but can this even be solved natively?
You say that this is not a tethering or routing situation, but if you think about it, it really is both.
It is routing because the traffic from the phone needs to be routed to one of the two interfaces. There may not be routing through the phone, but it's still a routing problem. Fortunately, it seems that the routing part is working on all three platforms.
It is also an unusual form of tethering. The only differences are that traffic does not originate from the Pi, but from the phone, and that the Pi rather than the phone acts as the access point.
Thinking of it in terms of tethering is important because it shows that the Android CAN have WiFi and cellular active at the same time (at least in hotspot mode, but probably also in client mode). In this respect, my answer isn't complete - I can't tell you HOW to accomplish that.
As for the Windows phone problem you mentioned: in order to troubleshoot that, first identify where the DNS server is located that knows about the Pi's FQDN. Does a public DNS server know about it, even though it is obviously on a private network?
There are two possible explanations for your observation:
There is no public DNS server that even knows this FQDN. In that case, IOS is probably using something like mDNS to locate the Raspberry Pi.
There is a public DNS server that knows this FQDN. In that case, maybe Windows Phone checks the DNS reply for martians and rejects it, thinking that it is an attack.
Either way, what you'd really need is to have two separate DNS infrastructures. One way to do this is to include a small DNS server (dnsmasq maybe) with your app, and configure a forwarder for the zone with the Pi's FQDN to forward to the Pi's IP address (which would have to be hardcoded, obviously). All other requests should go to the normal DNS servers.
Another way might be to not use DNS at all, but use the hosts file instead.
For Android phone you can use application named "WiFi & Mobile Data Switch" (https://play.google.com/store/apps/details?id=com.totemsoft.wifimd). For me it solves the problem of simultaneous connecting to internet (via mobile data) and to local network (via WiFi, internet is inaccessible via WiFi). This applications works ok on Samsung Galaxy A8. I hope, my comment helps to somebody.

WiFi-Direct communication between Mac/Windows and Android

Is it possible to let an Android (>4.0) device establish a WiFi-Direct connection with a Mac OS or Windows device?
According to the Wifi-Direct Docs the protocol allows:
Android 4.0 (API level 14) or later devices with the appropriate hardware to connect directly to each other via Wi-Fi
It doesn't look like Google provides this functionality via the API. Although WiFi-Direct isn't a proprietary protocol and could be implemented for any platform. There are a few posts around the web of people trying to get Wifi-Direct to work under windows:
Broadcast message from Desktop PC to Android Device using WIFI connectivity
https://superuser.com/questions/417888/how-do-i-connect-a-laptop-running-windows-7-to-an-android-phone-using-wifi-direct
http://www.intel.com/content/www/us/en/wireless-products/my-wifi-technology.html
http://androidforums.com/android-lounge/552970-howto-wifi-direct-use-your-laptop-desktop-softap-android-reverse-tethering.html
All you need is to use java.net.Socket. Depends on what do you mean by establish direct connection. TCP connection is established when on destination device (windows or mac) you open listening socket by ServerSocket. On other device you can use Socket with destination address. Of course, windows or mac box will propably have to open that port in firewall.
I guess what you actually need to know is how to get name of target computer or how to discover computers on local network. Unfortunately, it does not depend on android version I think. You should check http://en.wikipedia.org/wiki/Zero_configuration_networking about some hints. I fear there might be problem in need to open privileged port on android, as normally you have to be root for that. And that would require rooted android.
Of course you should specify what do you intend with that direct connection and what software do you need. If both ends have your application, you can just open any not allocated and unpriviledged port on android. Use UDP to broadcast periodically name of android device (propably ask user to enter name of his device). Use multicast to send requests, and receive replies the same way. Multicast sending and receiving would require your application on android and on windows and on mac also. Now you can read from multicast application what type of machine it is, and what local port it left open for your direct connection. From source of udp message you know where to connect.
You may use some framework to speed things up. All this require some knowledge about networking, but I don't know simpler and well working solution. Unless you provide details about what is your direct connection to do.

TCP connection from phone to debugging host

I'm developing client/server software for Android.
While connected to the phone via USB debugging, I'd like to access the webserver I'm running on my developement PC - using the USB connection.
Is that possible and if so, how ?
I stumbled upon the answer after a night sleep.
Enabling USB tethering on my phone (N1, 2.2.1) gives it and the host computer an IP address. The phone can communicate with my web server running on my developement machine!
(Chris, the rumors are right)
Just make sure your web server is listening on that IP address. For apache, use the line
Listen 80
or
Listen x.x.x.x:80
(x.x.x.x being address of your android tunnel)
The easiest way to do it is via wireless. If you don't have wireless, I'd say bite the bullet and get it, it's going to be a necessity for testing anyway :)
Typically the USB only supports connections from the development machine to the phone (via adb port forwards) and not the other way around. Of course once a connection is created data can move bidirectionally. One could use this to build a tunneling proxy web proxy (one connection in from the development machine to a daemon running on the phone, outbound connections from the phone then tunnel through this)
I have heard rumors though that what you want to do - often called reverse tethering - may be supported in some more recent devices.
Kevin's wifi suggestion may be the simplest with a real device.
On the other hand, unlike typical phones, the android emulator can directly access the development machine's loopback interface at an alias address given in the documentation.

Categories

Resources