Question

I have to send data from one Android devices to many other Android devices. This will likely be a one way communication, as the sender will "push" data to the receiver, and the receiver receives the data, performs some modifications on it and saves a local copy.

I looked around the web (stackoverflow included) and realized that there are a number of solutions out there: wifi P2P, sending data via a server etc. Ideally I would like to do the wifi P2P but I'm afraid my hardware does not support it, thus, I'm thinking about using the wireless hot spot feature instead.

So here's the question: imagine having the device that is broadcasting the wifi hotspot as a "master" and the devices connected to it being a "slave" (which just receives data from the master). How do I broadcast data from the master (one device) to the slaves (many devices)? I'm new to network/socket programming, so a simple solution and lots of examples would be very useful. Also, it is safe to assume that the user will connect to the wifi hotspot manually (going into settings, finding the right SSID, connecting etc.), and that the app should just send data.

Thank you very much for your time!

Was it helpful?

Solution

The example below shows one way of doing of what you want to accomplish. By experimenting with it you will at least get a feel for what it is like when it is working.

+---------+    +---------+    +---------+
| Receive |    | Receive |    |  Send   |
| Browser |    | Browser |    | Browser |
+----+----+    +----+----+    +----+----+
     |              |              |
     |              |              |
     +-------+------+--------------+        +---------+
             |                              | telnet  |
             |   +--------------------------+  CLI    |
             |   |                          | session |
             |   |                          +---------+
          +--+---+--+
          | Accord  |    +------------------------+
          | Cloud   +----+ C/Java/Perl/Python etc |
          | Service |    | Program Language APIs  |
          +---------+    +------------------------+

There are several ways to establish a bi-directional communication channel between a browser and a web service. E.g. WebSocket, AJAX, etc.

In the following example the Send Browser below sends the entered text when the send button is clicked.

enter image description here

When the Receive Browser receives a notification, it updates the browser content with a counter value and the new text string. It increments the counter each time it receives an update.

enter image description here

In send.html and receive.html code below, Accord.js establishes a communication channel between a browser and the Accord Cloud Service. Send and Receive browsers interact with the Accord Cloud Service using ActiveML, a hybrid of JSON and XML meta languages.

prompt> cat send.html
<html>
<head>
<title>Accord Software, Inc.</title>
<link rel="icon" href="/favicon.gif"/>
</head>
<body>
<script type="text/javascript" src="http://ac.accord.com/src/Accord.js"></script>
<script type="text/javascript">
var rpc; 

function run() {
    if (typeof AccordAmlHttpRpc != 'function' ||
                    typeof checkSessionId != 'function') {
        setTimeout(function(){run();}, 100);
        return;
    }

    rpc = new AccordAmlHttpRpc();
}

/*
 * Send the text string when 'Click to Send' button is acted upon.
 * This ActiveML command will update the string value and any
 * sessions that have outstanding 'wait for an update' will unblock
 * and receive the update notification.
 */

function sendMessage() {
    var elem = document.getElementById("SendMsg");

    rpc.call('aml set string Demo.Msg = "' + elem.value + '";');
}

run();
</script>
<br>
Enter text: 
<input id="SendMsg" type="text" value="" maxlength="50" />
<button onclick="sendMessage()">Click to Send</button>
</body>
</html>

prompt> cat recv.html
<html>
<head>
<title>Accord Software, Inc.</title>
<link rel="icon" href="/favicon.gif"/>
</head>
<body>
<div id="Page"></div>
<script type="text/javascript" src="http://ac.accord.com/src/Accord.js"></script>
<script type="text/javascript"> 
var rpc; 
var div = document.getElementById('Page');

/*
 * Display the string and increment counter.
 */

var count = 0;

function DisplayMsg(s) {
    div.innerHTML = count + ': ' + s;
    count++;
}

/*
 * Event is received as 'ActiveML set string Demo.Msg = "hello, world";' 
 */

function RecvMsg(s) {
    var eq = s.indexOf(' = ');

    /* 
     * Remove quotes and semico at the end.
     */

    s = s.substring(eq+4, s.length-2);

    DisplayMsg(s);
}

/*
 * DisplayString() is called initially to display the current value
 * followed by RecvMsg() for each subsequent update.
 */

function run() {
    if (typeof AccordAmlHttpRpc != 'function' ||
                    typeof checkSessionId != 'function') {
        setTimeout(function(){run();}, 100);
        return;
    }

    rpc = new AccordAmlHttpRpc();

    /*
     * Communication with the back-end service by using
     * ActiveML.
     */

    rpc.call('aml print string Demo.Msg;', DisplayMsg, RecvMsg);
    rpc.call('aml wait for an update to print string Demo.Msg;', 0, 0);
}

run();
</script>
</body>
</html>

In order for a browser to communicate with Accord Cloud Service, one needs to login from each browser. You can create a temporary free account by clicking on the login button at ac.accord.com to try things out. After creating the account, you need to telnet to ac.accord.com and do the following before doing any 'sends' or 'receives'. On windows download and use PuTTY. On linux/bsd use telnet.

prompt> telnet ac.accord.com
Connected to ac.accord.com.
Escape character is '^]'.

Accord ActiveML - Version 1.0.0.0
Copyright (c) 2001-2013, Accord Software, Inc. All rights reserved.

ActiveML Uid: <email>
Password: <password>

Welcome !

aml> create aobject Demo;
aml> create string Demo.Msg;
aml> set string Demo.Msg = "hello, world";

Every time a set command is received from either a 'send' browser or via telnet CLI, the 'receive' browsers will update their display.

Apart from using a telnet CLI mode, you can also interact with Accord Cloud Service using various programming languages such as C/C++, Java, Perl, Python, etc.

If there is a budget for this task, a subscription based solution may be worth evaluating. Subscribing to a Cloud based solution can be a cost effective solution. (Sometimes it can cost less than what you spend on coffee!). Disclosure: I work for Accord.

OTHER TIPS

Instead of doing wifi, you can also try bluetooth, or NFC. The problem with all these is they all requires quite a bit of setup, enable this and that.

NFC is pretty cool and setup is relatively easier. may worth trying.

Depends on the data you are sending, you can also do something magic, like encoding them through SMS, or create a 2D bar code and the other phone scan it through camera.


now if you really want to broadcast, it has nothing to do with the hotspot. you can only use UDP, and broadcast it to your subnet. the others client should be listening on the port and they will simply get it. do some google search and see how to send broadcast using sockets.

Hot spots are essentially networking devices. They generally are not aware of what applications are doing.

In order to send data from one device to many other devices you will need a server to which you 'submit' or 'send' data and then the server would 'push' data out to all the other users connected to the server and have expressed an interest in receiving the updates.

I have implemented a solution that does exactly this. If you want me to post sample code here, let me know. Then you can play with and learn what's involved in making it happen. You can use browser based devices/telnet sessions to see it working.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top