Communicate between 2 Android devices - android

I need a way to send messages between 2 android devices, 1 a phone and 1 a tablet. I'm using Java to create the app. I'm looking at servers but I can't figure out where to start. Thanks for any help :-)
P.s. Both devices would be using the same wifi connection so could I use a port to send messages

Beside the obvious solution from #dilix you could use a library like JGroups which provides automatic discovery of other devices.

The answer depends on how far away the devices are. Right next to each other (inches)? NFC. Within about 20 feet? Bluetooth. Within a hundred meters or so? Wifi Direct, which is peer to peer wireless connections. More than that? You're going to need a central server, because no carrier allows incoming TCP or UDP data- they all firewall it off. Of course all of the longer distance answers will also work for shorter distances. But different solutions here also have different security requirements.

I think you can try sockets to send\receive messages:
http://developer.android.com/reference/java/net/ServerSocket.html
http://developer.android.com/reference/java/net/Socket.html
Try to searh "android socket" and you can find some information even on the stackoverflow.

Related

Android P2P Wifi Direct / Bluetooth

I am developing currently an Android application that will communicate with other Android devices per Wifi. The Wifi will also provide the online connection. There is no server in this Wifi environment.
The idea was that the devices will discover each other with UDP multicast messages. But we noticed that UDP multicast often does not work on the devices. Sometimes it does, sometimes not.
To work around this issue we implemented a mechanism where the devices will store the local Ips online so that other devices will be able to retrieve them.
But this, of course, does only work if the Online State is available, which sometimes is not.
My question is, if there are other techniques with what the devices are able to connect per Wifi to the each other without knowing it´s Ip in the first place. Is there an easy way to forward the addresses per Bluetooth for example? Or could Wifi Direct be an option?
Every idea is welcome. Thanks for all of your help.
A zero configuration networking solution might work for you. There is a Nuget package called zeroconf that should work with Xamarin:
https://www.nuget.org/packages/Zeroconf/
Source code:
https://github.com/onovotny/Zeroconf

Data transfer between two android devices which are on different networks

I am working on one project in which two android devices which are in different network need to transfer some data. Both device will have internet connection. It will be either Wifi or GSM provider.
Consider the following cases:
If one Android device has GSM network and
another device in some other place which has Wifi network
If one Android device has GSM network and another device has GSM network
How to transfer data in such cases?
I don't want to use server in between.
Please let me know how do I solve this issue. Any clue or any links which will help me. So far I didn't find any such things. Please help me...
Short Answer: You're going to have to use a server.
Long Answer: You could use bluetooth if the location is close enough or maybe Wifi Direct although I haven't personally used the latter. Although, ultimately a server will be the best option. If you don't know a lot about backend development I could recommend parse.com as a good service that makes creating backends very easy and has fairly high usage limits before you have to pay.
Unless a device has been configured with an external IP address, which is very unlikely (impossible?) on a cellular network, there won't be a way to directly contact it from an external device. Even on WiFi, most devices will never have an externally defined address.
If you had administrative control over a directly connected and externally addressed router, you could port-forward traffic to a single device, but that's likely not the solution you're after.
A much easier approach is to use one of the services that let you define a back-end w/o a lot of setup or costs. The most popular one is probably Parse.

How to go about WLAN communication on two Android Phones

Is there a way to go about getting 2 android phones to communicate to each other locally without going through a central server? Sort of like being on WLAN? I need these 2 phones to be able to sync themselves to each other when they are nearby and running the same app.
Maybe through bluetooth perhaps? Just a suggestion, I'm not sure how to go about this, but an idea might be to have each app broadcast through a certain port on LAN whilst searching for the other's broadcast through the same port. Once the other device has been identified, then transfer the data.
The only ways to do that with WLAN environment (and not included a AP i.e infrastructure mode)
Ad-Hoc mode (Google will help you more on this)
Wi-Fi direct - that is the future of peer-to-peer with WLAN. (Again am sure Google will be more than happy to help you!)

Android - communicating between two devices

What is the best way for an Android app installed on two devices to communicate with each other? Can the devices connect directly without using text messaging?
You have several options, depending on your requirements and setup:
If your devices are very close to one another (up to about 10 meters), you can communicate using Bluetooth, as Derek suggested.
If your devices are somewhat further away, but within WiFi range of each other (up to about 100 meters), then they can communicate with each other using the Peer-to-Peer WiFi API, documented here (part of the Android Wireless API). This does not require a WiFi router to be present, and the devices will find each other and communicate directly. This does however require Android 4.1 or higher.
The Android Wireless API will also work if your devices are on the same local network (i.e., use the same WiFi router), even if they are not themselves within range of each other.
If none of these options are viable/guaranteed, then I agree with Derek that the easiest way would be to use ServerSocket and Socket to create a server/client interface through the Internet. Here is a sample application doing that. The main problem you might encounter is that if the server is sitting behind a NAT (such as a home internet router), you will have to configure the NAT to forward the incoming packets to your Android server.
You can connect them via bluetooth using BluetoothSockets. Android developer website has pretty good documentation on this.
http://developer.android.com/guide/topics/wireless/bluetooth.html
Or if you'd rather (and have internet on both devices), you can use regular Socket's.
http://developer.android.com/reference/java/net/ServerSocket.html for server side
http://developer.android.com/reference/java/net/Socket.html for client side
If you have a large amount of data to transfer, internet sockets have a greater data capacity and will be faster. The other advantage is that there is no such thing as "out of range". You can connect the two devices wherever internet is available, whereas with bluetooth they have to be within bluetooth range of each other
you can use PubNub. it handles all networking and you should only care about messages.
it has great API to work.
(Thanks to #Ian Jennings : Can we send data from an android device to another android device directly (p2p) without server in the middle?)
Depends on what you are doing. If you have a server, you may be able to send some message to it and have it pulled by the other device (assuming both clients have the app installed). I think this would be the most intuitive way (but it really depends on what you are communicating).
Text messaging and email might work, but you (or the user) needs to know the numbers/emails associated with a device to do that.
you should have a look at WifiDirect
Wi-Fi peer-to-peer (P2P) allows Android 4.0 (API level 14) or later
devices with the appropriate hardware to connect directly to each
other via Wi-Fi without an intermediate access point.
As was already suggested, sockets are the easiest way to accomplish this if your devices are all connected to a network.
There are things to accomplish here:
Use Network Service Discovery to find devices running your app
Connect to other instances of your app using a socket
For a complete tutorial you can check this out
ShortAnswer: Yes
Data can be sent directly.
In order of range:
1 Bluetooth
2 wifidirect
3 maybe.. GSM hardware direct?
After that, options again in range order:
4 tether or network
5 Internet
The android NSD API is meant to do the exact same thing you are trying to achieve! The example bundled with SDK is self explanatory!
please check:
Android NSD API example

Network Device Discovery

For my Android app, users need to connect to a server that will be hosted somewhere on the same LAN. There can be multiple servers hosted on the same LAN. To make it easy for the user, I was going to scan the current LAN that the Android device is connected to and then list all of the network devices that have the server running on it, rather than having the user input the IP to the computer manually.
I'm fairly new to networking, and after some searching I found out that I would have to use a multi-cast DNS search or UDP broadcast to detect the other devices. I also found a nice library called jmDNS, although I've found very few documentation and sample code on it. Could somebody point me in the right direction for what I'm trying to do to save me wasted time (mostly if I'm on the right track)? I'm assuming that I'll have to modify my server a bit to broadcast it's there? It works completely as intended if I input the IP manually into the configuration page on my app. Also, this only needs to discover Windows computers, not sure if that matters. Thanks in advance.
Well, jmDNS is a complete Java library that you could use for your setup. It can be used to braodcast your services which other clients can search for.
Bounjour service on windows is a bit tricky, although it's definitely possible. The easier way, I would say is to use jmDNS for broadcast and discovery for both your servers and clients.
You would run the jDMs or other service on your local area network as a background service or a dameon.

Categories

Resources