I'm making an application which needs to communicate to another app on another device. The only problem is that the IP addresses from the devices aren't allways the same. I want to client to find the server on a specific port, but how can I find devices on the network which have this port opened without me having to enter the server's ip on the client side? I've found Android's NsdManager, but that works from API level 16 and on. I'm developing with level 10.
Thanks in advance!
You can use Zero-configuration networking which can help to solve your problem.Have a look at following websites-http://www.multicastdns.org/ and http://en.wikipedia.org/wiki/Zero-configuration_networking.
You can also make use MDSND, check Bonjour implementation on Android for more info.
My apologies for not responding.
I solved this problem by broadcasting an UDP packet to all devices (255.255.255.255). Al devices listening in the specified port will respond and thereby the client will now know the server's IP address. TCP is used for further communication.
This shouldn't be too difficult.
The private ip address in question should be of the form 192.168.1.x (x being between 1 and 255)
And normally, I believe that number is assigned sequentially, so first I would check if
192.168.1.1 if not that one,
then I'd check
192.168.1.2 then
192.168.1.3 then
etc.
Broadcasting works in most cases, the link below shows how to do it from code:
[Send Broadcast UDP but not receive it on other Android devices
But there are exceptions:
Some phones doesn't recevie broadcasting package correcttly(HTC for example), i solved this problem by broadcasting from HTC phone and once other phone recevied the package, send a udp package to HTC phone(not through broadcasting)
If one of the device is act as hotspot, the broadcasting seems doesn't work at all, in this case, other devcie can try use the gateway ip(which is the hotsport device's ip)
Related
I cannot figure out how to send a UDP packet over a 4G LTE network from an Android app (no WiFi). Does anyone have any ideas or know where I can find some documentation? I need to write two android apps, one client and one server, so that I can send UDP packets with timestamps from one device to the other. This is to test 4G LTE network latency from device to device.
Please help!
Thanks!
You can use the DatagramSocket class:
http://developer.android.com/reference/java/net/DatagramSocket.html
This should work regardless of WiFi or mobile connection type, although you may find some mobile operators have some restrictions on UDP connections.
There are many existing Android Iperf apps that can be used for exactly what you are trying to do. I use Magic Iperf to do this type of testing myself.
I'am trying to make an app that receives some binary data from a other device which is not running android. The Android phone should not transfer any data to this device. So over to my questions:
Should the phone act as the server which establishes the RCOMM channel?
Is it possible to connect the phone and this device without paring, as long as I know the MAC-address of the device? If so, how?
And yes, I have read http://developer.android.com/guide/topics/connectivity/bluetooth.html so no need to only answer with that link. :)
Thanks in advance!
1)
Phone should act as a receiver.
I found that the tricky part is to use correct UUID.
If you connect two phones you should be generating those and everything work fine, but on your hardware you can't change UUID that easily.
Take a look at these reserved UUIDs.
For a Serial Port Profile's (SPP) communication use UUID 00001101-0000-1000-8000-00805F9B34FB.
2)
You can connect to a device without pairing if you already know the MAC address, you don't even need to use BlueTooth discovery.
To get device using MAC address call getRemoteDevice(String)
and then createRfcommSocketToServiceRecord(UUID) to get BlueToothSocket.
Also take a look at checkBluetoothAddress(String) to validate MAC address.
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 am working on an Android application which sends a udp broadcast message to the devices on LAN. Application works well on the device and also on the emulator running on Windows. However, I couldn't get it working on Linux and Mac Android Emulators.
Since 255.255.255.255 is not working on android, I calculate the broadcast ip by using the subnet mask. (broadcast ip in my case is 192.168.1.255 where host ip is 192.168.1.88/24). But the message is never sent. Also checked with Wireshark but seems like the udp package does not get outside of the emulator.
Is there anyone who achieved to send broadcast messages on Linux Android Emulator?
Any advice will be highly appreciated.
Could you give a sample of your code? It is important when using UDP instead of TCP to use a DatagramSocket with DatagramPackets in stead of a standard Socket or an SSLSocket (or any other kind of Socket for that matter). It seems like that might be what you're doing because it sometimes works, but it might be work trying. If that is the problem and you want any advice, this should suffice:
http://developer.android.com/reference/java/net/DatagramSocket.html
All of the documentation, examples and questions I've seen so far on TCP connections with Android have been between an Android device and a computer. As unreliable as wireless can be, is it possible to make a client-server TCP connection between Android devices over WiFi, and if so, how?
Edit: I guess I should elaborate more on my situation.
My Droid does not respond to ping or accept incoming TCP requests from anything unless I first make the Droid a client and my laptop the server. After this initial connection is established I can then ping from my laptop, or make the Droid a server and my laptop the client. What I can't do is make one Droid the server and another a client, I always get a "No route to host" error.
It feels like there is something blocking incoming connections unless the device initiates a connection to something else, and even then the only request the device will accept is with this device. How can I make my Droid a server that accepts all incoming TCP requests from any device on my local intranet?
You should be able to. Just do like you would do with computer/device connections, except run the client and server code on the devices.
One of the neat things about the Internet is that the Internet doesn't care whether you are establishing a connection between two PC's, a PC and a phone, or two phones. I would look at the Socket documentation, that should be enough to get you rolling.
It's certainly possible. The only problem is figuring out the IP address. I don't expect a phone have a fixed IP... However, if there is a fixed "login" server somewhere in the Web which IP is known to both phones then they can do a handshake via that server and after that continue peer-to-peer.
re; The only problem is figuring out the IP address. I don't expect a phone have a fixed IP.
Try this;
Use the web browser on the phone to log into this website: It should provide you the IP address.
http://whatismyipaddress.com/