Android/iOS set IP-address of device hotspot - android

Is there a way to "fix" or set up a static ip address of the hotspot? I need to guarantee a peripheral can connect to my server socket on that ip always.
I am trying to connect a wifi peripheral device to a server socket running on my android device. I have the peripheral connected to my android's hotspot with the ssid and ssid password.
Right now as a work around I am using ip 192.168.43.1. Sources here and here say that 192.168.43.1 will is the default. However, I am under the impression that it's possible this ip address could change.
I am also wondering about doing this for iOS but one step at a time.
Many thanks in advance!

This IP Address is hardcoded for Android as you can see in the source.
if (ifcg != null) {
/* IP/netmask: 192.168.43.1/255.255.255.0 */
ifcg.setLinkAddress(new LinkAddress(
NetworkUtils.numericToInetAddress("192.168.43.1"), 24));
ifcg.setInterfaceUp();
mNwService.setInterfaceConfig(intf, ifcg);
}
However a better implementation is for your peripheral device to get the default gateway of the local network as that will work regardless of hotspot implementation.

Related

Connect to WiFi network without DHCP and private IP address in 169.254.0.0/16

I have a device that broadcasts a WiFi AP which doesn't have a DHCP server. Computers and iPhones can connect to it and end up with an IP in 169.254.x.y. Then, mDNS such as device-name.local are resolved and allow to use the web interface of the device to configure it (notably, send it the SSID/PSK of the target WiFi network to connect to).
For some reason, my Android devices (tested on Moto G6 Android 9 and Pixel 3 XL Android 11) fail to reach the IP state. They can associate with the AP but don't get a 169.254.x.y. IP. They usually keep mobile data active, or try to switch to another working AP.
When I disable mobile data and remove all known AP, they still won't get an IP after association.
When I manually set an IP in the same subnet (obtained by a computer that managed to connect to the IP and land in 169.254.x.y.), then it works! Name resolution doesn't work, but the Android device is able to reach the other device by using its IP address.
Now this is very bad, especially because I didn't find any stable API that didn't use reflection to set a static IP address when establishing a connection to a new AP.
Is there a way to connect to the AP and be able to reach the device using whatever.local without any interaction from the user?
My code basically does these steps, with a 15-time retry on each step if it fails (and start over from #1):
connect to SSID with
WiFiForIoTPlugin.connect(ssid,
password: 'XXXX', security: NetworkSecurity.WPA, joinOnce: true, withInternet: true))
get SSID with WiFiForIoTPlugin.getSSID(), check it matches the target
get RSSI and check it's not null or == -127
try to resolve myhostname.local (which works on Linux, MacOS, iOS but not on Android)
try to communicate with the device now that its host is resolved and we have IP connectivity

How to find all the esp8266 connected to a router

Hi all I want to connect multiple esp8266 devices to my router and create a mobile app that can find those devices and send messages to them using udp.
Currently my plan is to let esp devices listen to a port and then my app would send a message on that port, esp would respond and the app will store the IP.
Is there any better way to do that?
A friend of mine told me that this approach will fail if routers's gateway is changed. Is it true?
I am just calling WiFi.begin(Ssid, Password); to connect to wifi without doing any changes with wifi.conf().
I am using arduino SDK.
Give your ESP8266 devices a static IP addresses so the mobile app will know in advance where they could be 'found':
IPAddress ip(192,168,1,xx); // desired static IP address
IPAddress gateway(192,168,1,yy); // IP address of the router
IPAddress subnet(255,255,255,0);
WiFi.begin(ssid, password);
WiFi.config(ip, gateway, subnet);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
and use dynamic DNS service (unless you have a static IP address) with port forwarding to access each of the ESPs. You can choose ports 8266, 8267, 8268, 8269, ... or any other above 1024 and then set the port forwarding in your router settings so port 8266 will be forwarded to the IP address of your first ESP and port 80 (or any other), port 8267 will be forwarded to the IP address of your second ESP and port 80 etc.
Afterwards you can access your ESPs from mobile app (or internet) using URLs looking like http://xx.xx.xx.xx:8266, http://xx.xx.xx.xx:8267, http://xx.xx.xx.xx:8268, http://xx.xx.xx.xx:8269, ... or by using dynamic DNS service with: http://myhome.something.com:8266, http://myhome.something.com:8267 etc. Or you can access them from your local network by known static IP addresses. That is - your mobile app will have to determine if it is inside local network or not. Or you can always access ESPs using proxy - in that case the URLs will not depend upon being inside or outside the local network.
Your friend may have more of the details of your solution, but IMO the gateway has nothing to do with it. If all clients and the server are on the same subnet inside the router's local network then the gateway doesn't come into play.
I would do a UDP multicast. Your mobile app would just need to send one request and then listen for replies from the ESPs for a few seconds.
One way is to check all the Mac addresses on the network. The first 6 digits is the unique code for the company that made the wifi dongle.
This is assuming you do not have any other devices on the network using the same dongle and aren't what you're trying to search for.
You can find all the Mac addresses on a network by performing an ARP request on each IP.
But all the devices would have to be on the same subnet.
If you devices are remote, then I would go with your original plan, however your friend is correct if the gateway changes this would require something a little more robust. Use a Dynamic DNS service along with a domain name. The Dynamic DNS will update your DNS records in almost real time if your gateway address changes.

Connecting to WiFi with Static IP Address on Android

Setting static IP Address(192.168.55.155) on android device, results in its WiFi connection not working (no internet).
If its DHCP on the android device, WiFi (internet) works. The Gateway on the device is pointing to Wifi Router.
I have a few android devices on the WiFi router. I need to know the IP Addresses of these devices in advance, so having static IP address is one way.
What configuration is missing?
You cannot give it a fixed address of 192.168.55.### when the router gives you one like 192.168.0.### but only 192.168.0.### also. Try 192.168.0.155.
Its easy, use an APP called Fing this APP allows you to watch all devices connected to your network and their IPs.

Socket communication with android devices

Can i interact with a server(android device) from a client(android device) just to query a database located in the server using TCP sockets?
And Yes and No.
If your device is within wireless network and have own local IP. You can connect to it from client phone if it is in the same network just providing proper IP and Port.
If server phone is located in another wireless network, then Port have to be forwarder using router settings to the server port. Bear in mind that if you will reconnect to wireless network by phone, then IP will be automatically assigned by network infrastructure and it might be changed from previous.
If you want to connect to server phone when it is on 3G network (or similar non wifi), you wont have any specific IP, and it is not static, that way it will be almost impossible to create reliable network. It is more Peer-to-Peer model of network you are looking for.
It is much better if you use Server in the middle as Gateway, that way it will be reliable. The only thing is that you will have to host server and its processes with all traffic and so on.

Socket programming WiFi issue

I am implementing a mobile chat application, I am using socket connection for implementing p2p connectivity. My chat is working fine with android devices. My issue is I can connect a device in 3g network or with in the same WiFi network but the connection is not working when a device form outside WiFi try to connect a device in the WiFi network. I know it's because of the local IP of the device assigned by the WiFi. How can I root and connect a device in the WiFi?
Sounds more like a router settings issue, than an app issue (meaning that users of your app would need to do this for their own networks as well).
Your wifi router needs to know how to forward communication to your device. So say that your app is connecting on port 1234, then you need to tell your router to forward communication from the outside on port 1234 to the internal IP of your phone in your wifi network.
The phone that is outside of the network should target your network's external IP and port 1234.
Sajan, your issue is not only an IP mapping issue, but also a NAT traversal issue. It is not always possible to punch holes in NATs. And when it is, it must be done with some sophisticated techniques including a super peer located outside of your wifi/lan which will read the translated address from your inside peer.
Unless you are using UDP and the NAT is friendly, it is not possible to send TCP communication directly to the inside peer. In most case, you'll have to check what is possible with the NAT, and if there is something possible, you'll need to perform mapping prediction and tell both peer to attempt com on predicted IP addresses.
That's valid if you don't want you users to have to configure their NATs. And even in that case, such configuration will not always be enough (if there are cascades of NATs, or proxies for example).

Categories

Resources