Communication between Android and a device connected to it over Hotspot - android

I want to send data from a device to an Android Phone.The Android Phone and
device are connected through hotspot. i.e. Device is connected to Android phone through hotspot of Android.The device has capability to send data over sockets provided an IP and port. what destination IP should I provide to the device so that it can connect to my Android device on a given port. I have written the corresponding code on Android device to receive connection on a given port.

The Android phone will act as the gateway for your other device. Therefore, to find the IP of the Android phone from the other device, you have to inspect the routing information to get the IP of the gateway.
Command line
On the device connected to the access point provided by the Android phone:
$ ip route
default via 192.168.43.1 dev wlan0
...
You can validate this information by checking the IP of your Android phone:
$ adb shell ip addr
...
6: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP>
...
inet 192.168.43.1/24 brd 192.168.43.255 scope global wlan0
This should show you the IP assigned to the WiFi interface of your Android phone. It should match the gateway IP seen on your other device.
From code
If you have to obtain the gateway IP automatically, check here for some code examples.

Related

Setting static IP adress to Android device

Is there any way to set a static IP address to Android device if the device is connected to a network via ethernet cable using ethernet to USB adapter?
I tried connecting to device via ADB and then changing IP address using $ ifconfig eth0 x.x.x.x command, but I got ifconfig:ioctl 8916: Permission denied as an answer.
Nope, there is no way to set it on common devices. You need rooted device, which allows you to run ifconfig eth0, thats the only way (valid up to Android 13). By default eth0 will obtain IP using DHCP, if there is no DHCP available - it just won't work. In "your" network you may assign some particular IP for particular MAC (also ensure that MAC address of ethernet isn't randomized like for Wi-Fi starting Android 10, but it shouldn't be on Linux level)

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

Does the mobile device under test need to have an ip-address to use appium

Will the device under test need to have wifi enabled so it gets an ip address before we can use appium to connect to a port on the device or is it possible to connect to and run tests with wi-fi off and if so, what ip address would we use to talk to the socket server on the device ?
thanks in advance
Of course you can connect your device through wi-fi and run your appium tests. Below are the steps:
Your device and Machine/Laptop needs to be in the same network.
Connect your device through USB and enter following command :adb tcpip 5555
(This enables the tcpip port 5555).
You should get the output as restarting in TCP mode port: 5555
Now you can disconnect your device which is connected through the USB.
Open wi-fi setting in the device and note down the ip address of device.
In the command prompt on the machine, enter the following command to connect your device: adb connect 192.168.0.2:5555 (IP ADDRESS SHOUD BE YOURS)
You should the output as: connected to 192:268.0.2:5555
Now, Enter adb devices to check whether the device is connected or not. Your device should be listed in the output of "adb devices" command with ip address.
Use the same ip address along with port number as UDID capability. eg. 192:268.0.2:5555
Note: If you restart your device then you need to enable the tcpip port as mentioned in step 2.
I hope this helps :)

Android Wifi-Hotspot Port Forwarding

How can I make a port forwarding from a android device whose Hotspot is ON to another device who is connected to that hotspot.
I want to connect to my system server which is connected to a android Hotspot from another device which is in another network. When I use the pubilc IP of the Hotspot device I am getting access to that device only, I am not able to access to other device which is connected to it.
As how the WIFI-router provides the port forwarding in the same case I want in android when I am using Hotspot.

Programmatically find the IP address of the desktop over USB tethering

Is there a way to find the IP address of the desktop machine connected the android phone after USB tethering is turned on?
Does android assign a fixed IP address to the desktop? Or does it assign a dynamic address (dhcp)? Is there a PPP connection created between the phone and machine?
With USB tethering it seems that all android devices use the same static addresses, the android device using tcpdump is 192.168.42.129
The attached device will get a DHCP provided address in the same address range.
I listen to a port on my android app and have a udev rule to push the dhclient address back to that port on my app.
AFAIK, in usb tethering mode the phone is acting as the computer's modem, so the PC's IP would be the phone's IP.

Categories

Resources