I want communication between two Android phones, both the phones are connected to the same Wi-Fi network. Knowing the IP address I can directly use sockets to communicate with both the devices, but my question is, is it possible to get the IP address at the run time? Or is there some other way in which I can do this?
You can use jmDNS to achieve this. It's a really helpful library and once you detect the all devices connected to the same wifi you can get their ip and port so you can establish a connection. You can learn more about how to use it here.
Or you can use Android Wi-Fi Direct API which works only for API Level 14+. Here is more information about the API : Wifi-Direct.
Hope it is what you are looking for! : )
You can use Google Android NSD service.
Check out http://developer.android.com/training/connect-devices-wirelessly/nsd.html
Assuming that both devices are in the same local network you could use/write kind of ip scanner (Check every single ip adress whether is alive and if it is try to connect).
Related
Well, the following situation: Devices need to communicate offline. The only option is Bluetooth. Here, the issue is that Bluetooth's encryption is insecure and broken. See KNOB attack.
Therefore, I'm looking for a way to securely exchange data between two bluetooth devices (e.g.,Android and iOS). My current guess is to establish a TCP/IP connection to make use of typical SSL/TLS libraries with all its other advantages (certificate check etc.) and disable bluetooth's encryption for performance increase.
https://developer.android.com/reference/android/bluetooth/BluetoothSocket
Unfortunately, it appears that this idea is already not supported by Android. They do offer some sort of BluetoothSocket, but that is not a TCP/IP connection afaik. Does anyone know a way to enable a TCP/IP connection between two bluetooth devices? Any other idea to secure that broken bluetooth connection?
Answer is yes. Nevertheless, there is no usable API on Android (before Android 9 via reflections maybe) and it still has the hide annotation on Android 11 in its source code, besides the requirement of a special permission to be used by system apps only.
The only option is to enable Bluetooth tethering via system settings (if available). Next devices connecting over Bluetooth will get an IP address, while the smartphone acts as the router. The access of services on the smartphone is permitted.
My router shows
14:AA:BB:CC:DD:46 android-################ 192.168.3.112
AC:AA:BB:CC:FF:46 MY-LAPTOP 192.168.3.112
AC:AA:BB:CC:FF:AC NAS 192.168.3.112
Now if I use ftp://NAS/ it works, but
if I use ftp://android-################:7721/ it does not work, again ftp:/192.168.3.112:7721/ works, I don't understand what is the use of Android_ID then.
I thought since Android IP keeps varying on router we can find android by using Android_ID, but that's not the case.
Can anyone explain why is it happening?
Your Android device uses DHCP to get an IP address from the router on your network. While a router will try to use the same IP address across sessions, there's no guarantee you'll always be handed the same IP address. You generally do not want to make any assumptions about it not changing.
Assuming you have client software that's trying to talk to a service running on the Android device you need to implement some sort of network discovery mechanism. Android has a mechanism in the SDK that uses mDNS called "NSD". See https://developer.android.com/training/connect-devices-wirelessly/nsd. You could also use UDP broadcast.
"android-####" is a local hostname. Unless you have registered your Android device with a DNS server serving your local network you need to use IP addresses (and in that case you'd need to configure your router to also give your Android device the same IP address).
How to communicate with all android phones in the wifi network,(not wifi direct)..send msg files etc
If you are on a network, the most viable way will be setting up a server. The server will be the point of reference to the devices.
Do some research on REST calls and sockets.
Please, also refer to this topic: How to communicate two android devices?
I want to start a local network connection using the built-in Wi-Fi hotspot on Android devices.
Is there a way to accomplish it?
And how to communicate between two devices?
EDIT:
I want to do it programmatically. Then I can transfer my own data between devices.
I haven't done this before, so my answer is all theoretical.
I think what you need to do is declare one device the server and start its hotspot manually with a known name.
Start / stop built-in Wi-Fi / USB tethering from code? (According to this question, you can't start hotspot programatically).
Once you have the hotspot set up, you start up the server app and wait for incoming connections. The server app can show you the ip address or hostname.
On the clients, you can do a simple check and see if the hotspot name is around and automatically connect to it if you find it. See this link for connecting to a network:
Using WifiManager to connect to a network
As for your second question, how to communicate - you need to establish a connection between the devices. For this, you need the ip address of the server. The simplest thing to do would be to show the ip address on the server UI and then manually enter it on the clients and press connect. It would then establish a connection to the server on a known port and you can send messages between client and server.
For reference, I found NanoHttpd, which is a webserver for Android. It uses ServerSocket to listen for incoming connections.
Possible answers:
Before ICS and non rooted devices
There is no way you can connect to a device over wifi. There is no API to do. Bluetooth is your best option or user interaction.
Before ICS and rooted devices
I am not sure if someone hacked the code, but when I tried to do an automatic connection to a wifi spot I noticed:
The api is hidden inside the SDK.
The method that does the connection checks the thread who ask for it. If it's not the os thread, it throws an exception.
Using ICS
With ICS there is a new way to connect devices through wifi called Wi-Fi Direct.
Here is a link with some demos.
If you are going to develop your own application for each and every individual terminals(devices) then i think for you socket programming will do the trick .
Yes , android supports socket programming in the same was as java socket programming .
1st google java socket programming tutorials and then you can using the same knowledge in android .
Also do remember to include the uses internet permission in the manifest , actually thats something which ate my brains for a long time :)
You mean communication between wifi enabled devices without any server like p2p? if so its wifi direct which is supported in ICS check this out http://developer.android.com/reference/android/net/wifi/p2p/package-summary.html
Before ICS there is no standard android API for wifi direct, though Galaxy S2 has wifi direct it uses proprietary API's which 3rd party devs can't use.
i dont know programmically create wify LAN,but you can create wify LAN manually,then you can do java tcp/udp program as said by brianestey
"As for your second question, how to communicate - you need to establish a connection between the devices. For this, you need the ip address of the server. The simplest thing to do would be to show the ip address on the server UI and then manually enter it on the clients and press connect. It would then establish a connection to the server on a known port and you can send messages between client and server."
for manual connection follow the steps
take settings/wireless&networks/portable wi-fi hotspot settings from any one of the phone
create new hotspot and turn on wi-fi portable hotspot from there
connect all other device to that hotspot including your pc
you will get ip of each device programmically (includig pc,but i dont know)
I'm trying to develop an Android application which have to be able to transfer data/message between two Android mobiles, as a client server connection using their own Wifi, which having no network or routers nearby. I just tried using Tcp program to do this but it fails, its not connecting, I'm getting ConnectException and connection refused.
[Edited] Is it possible to achieve this using Android Portable Hotspot integrated with application? Is there any tutorial regarding this approach?
Any Ideas, how to do this?
Thanks.
You need to get the two devices networked to each other before you can worry about tcp usage of that network connection.
If one device is willing to perform access-point like functionality as part of a portable hotspot, and you select that as a wirelss network on the other, you may achieve a network connection between the two that can be used for custom traffic. Though there are ways the hotspot could be implemented where that would not work. Also of concern, the "client" device will now be sending all of its network traffic through the hotspot device, including not just foreground apps but anything it decides to do in the background.