I want to connect multiple devices to a single bluetooth server. My application currently makes the server discoverable, and then, the client application lists the discovered devices and tries to connect to one of them. The problem is, I don't know which of the discovered devices is the server.
The way I am dealing with the problem envolves a try-catch mechanism, where I try to connect to each device and, upon connection, there is a brief message swapping between client and server, where the client asks if the connected device is a server for my application.
The main problem here is that the socket connection and message swapping takes too long, if there are multiple discoverable devices nearby I am forced to probe each one of them asking "are you a server for this app?". Is there any alternative to this system?
Note: There is a little "catch" in this application, there may be multiple servers, so I really need a fast way to go around my device list checking which ones I can connect to.
Related
just want to know is there any methods to allow 2 different apps to communicate. Both of the apps are installed on different devices as well. I had gone throught quite a lot of research, but seem that INTENT, BROADCAST RECEIVER failed to meet my scenario. INTENT, BROADCAST RECEIVER can be work if both of the apps installed on the same device.
Here i can say you can use to make your things work :-
Sockets are typically used to accomplish this between Android devices (or between any peer devices).
When two devices desire to interact, you configure one or both of them to "listen" for connections on a socket and accept a connection from the other when that happens (or you can have a dedicated client and server and the client always initiates the connections).
You can exchange messages after the link has been made.
Android client server socket applications come in a variety of forms, but one that I found handy was:
Example of Android Server/Client using Socket on the client side (and its companion server side blog article - link included in the client blog)
It should be noted that you might need to add your own "protocol" on top of this. For instance, if you are sending a file that is unknown in length without a special "end" character, you might want to add a byte (or several bytes to represent an int, long, etc.) at the beginning to indicate the length of the transmission so the receiving side can tell when it has received everything (or that it has not received everything in case of an error).
connecting via networks (such as most 3G/4G) that forbid inbound connections
Even though there is nothing theoretically blocking sockets from functioning in these situations, many mobile operators will not permit inbound socket connections in practise. You would also need to determine the mobile's public IP address, which is doable but adds complexity. Whether your solution will only ever operate on a single operator's network, you can test it out to see if it works; but, if it doesn't, you could discover that using a server in the "middle" is preferable and easier: Devices A and B establish connections with servers Device A "discovers" device B after requesting the addresses of connected devices from the server. Device A sends device B a message.
Actually, it indicates that the messages should be forwarded to device B while sending them to the server. Device B is informed by the server that a message is available for it (using some sort of message notification like Google Cloud Messaging for example, or simply by the devices polling regularly to see if they have any messages). Device B accesses the server and downloads the messages. The aforementioned will function on pretty much any network that permits internet connectivity. It does have the drawback of having a server, but for the majority of mobile networks, it is probably a necessary approach.
You make one app a server using ServerSocket.
You make the other app a client using a Socket.
With both devices in the same network the client can connect to the server knowing its local ip.
After connection established they can communicate.
I am working on an app that uses peer-to-peer communication between Android devices. It receives and delivers JSON data and the data never goes through the server ever. The server is only used to keep track of user IDs, login times, IP changes, and other stuff. This app will use sockets for communication.
Requirements
JSON data goes directly to an Android device from another Android device without going through server.
The android devices can be connected to any kind network like 3G or Wi-Fi (here I assume I have to use NAT).
Server is used to keep track of IP changes of the Android devices as they may be connected to any kind of network.
Challenges
The main problems I am facing are:
I don't know how to connect two devices if they are behind a Wi-Fi network.
What approach I can use if I am going to use P2P for delivering JSON data.
Update
For this I have to use hole punching. The idea I got is that no one can connect to a device from the outside. It must start communication from inside the device. So if A request server S for address of B, then server gives the address of B to A and vice versa. Now what? What occurs next?
Any suggestion would be greatly appreciated.
I'm trying to figure out what the solution is to having multiple android devices on one network share data.
Assuming all of them are running the same app, and a user "registers", I'd like to figure out (without any user input) what other devices on the network are available to be talked to and then to send them this registration data, so that when the user goes to the next device, their info is already there.
I'm able to ping the 255 connected devices in the x.x.x.0-255 range to see which respond, but am not clear on how I can write an app resident server (using the term loosely) that I could then send the data to.
I can't be the first person to need to solve this problem, but am unable to find anything useful on this front.
One caveat is that this is for devices that have no 3G (or other means) of getting to the internet, and the wifi network they're on won't have access to the internet either, so the solution has to be 100% internal network and can't include any additional devices (like a box running apache that all the device can use as a server).
TIA
but am not clear on how I can write an app resident server (using the term loosely) that I could then send the data to.
Create a Service that has code that opens a ServerSocket and listens on incoming requests, such as an HTTP server.
On the whole, this is dangerous, but for constrained circumstances -- such as your "100% internal network" -- it should be safe.
I have two Android devices. One is acting as a server and the other as a client. The client connects to the server and requests a file - this is done in one thread on the client and one thread on the server so that both can continue doing what they want.
The client then attempts to connect to the server again to request another file. Right now I am getting a java.io.IOException: Device or resource busy when attempting to connect (socket.connect()). Is it because Bluetooth (on Android) only allows one channel between two devices? (if it were another device it would work but if it is the same it doesn't ?) Note that both attempts are made with the same service name and UUID.
Even if the error is specific to my code, I would like to know if this is the case or not.
System: android 2.2.1 communicating with the bluecove bluetooth library.
Definitely not with the same UUID(universally UNIQUE Identifier).
Reference for that was taken from here
Maybe with more than one. You can connect multiple devices in the Server/Client style, you can try to set one of the devices as a server and start several clients on the other. My first guess would be to start several client threads, but you might have to find a way to change the MAC address for each of them.
Here you can find another discussion about how to change your mac address, but only works in rooted devices. I can't find anything else for non rooted ones. No idea on how to do this programmatically but it might give you a start.
Here there is a discussion about connecting several clients at the same time in a server. I got there from this question. ( I think this might be your closest shot)
Here you have a discussion about peer-to-peer networks.
AFAIK, multiple connectivity is not possible in case of Bluetooth Connection. Bluetooth is Connectivity API is by default Synchronized so only one connection at a time is possible. So you can not perform multiple connections.
However it can be possible in another way like making one connection , performing 2 seconds operation on it and then creating another connection and performing 2 seconds operations like in normal multitasking operating system happens.
I am currently working on the creation of an application for mobile devices with Android which should be capable of sending and receiving messages through local means like wifi and bluetooth. After making a research about it I found out that Android doesn't have a native wifi ad hoc functionality and that the only way to achieve that is through rooting your device and patching the wpa_suplicant and some other similiar methods which are not generic for every device.
Synopsis of what I am trying to achieve:
An application for Android that scans for devices nearby and tries to connect to them automatically. If the other device also got this application isntalled then it automatically accepts any connection request it receives and when the connection is established it sends back a message which ensures that it has this app installed so that both can start exchanging messages automatically and the users don't have to allow/deny any of the sending/receiving in real time.
My questions are:
Is there any way of sending messages through wifi between 2 android
devices without the use of internet?
Since Ad Hoc is not supported, is there any alternative?
In case the answer to 2 is no , then is it possible to make one of
the devices to be an access point so the other can connect to it and
vice versa.
As for bluetooth, is it possible to skip or automate the allow/deny
pop up of an incoming connection and the pairing procedure?
I'll be glad if you could give me an answer to those and/or any suggestions.
You'll be able to communicate between two devices over wifi without a router/access point using Wifi Direct (P2P) in Android 4.0.
Otherwise, I don't believe you can achieve this over Wifi (bluetooth would be the next best alternative). Since 2.2 you can set your phone up to be a Wifi Access Point (for tethering your device's network connection), but during this time, the Wifi hardware is unavailable for normal use. If you had 3 devices it could work (1 AP, 2 connecting to it).
Is this for a constant connection, or a one-off small delivery? You could use NFC if you want to transmit a URL for example (Android Beam integrates this type of sharing in Android 4.0, but could implement it in 2.3).