将数据从一台 Android 设备发送到另一台

问题描述 投票:0回答:3

我必须将数据从一台 Android 设备发送到许多其他 Android 设备。这可能是一种单向通信,因为发送者将数据“推送”到接收者,接收者接收数据,对其进行一些修改并保存本地副本。

我浏览了网络(包括 Stack Overflow),意识到有很多解决方案:WiFi P2P、通过服务器发送数据等。理想情况下,我想做 WiFi P2P,但恐怕我的硬件做不到不支持,因此我正在考虑使用无线热点功能。

所以问题是:想象一下,将广播 WiFi 热点的设备作为“主设备”,而连接到它的设备作为“从设备”(仅从主设备接收数据)。如何从主机(一台设备)向从机(多台设备)广播数据?我是网络/套接字编程的新手,因此一个简单的解决方案和大量示例将非常有用。此外,可以安全地假设用户将手动连接到 WiFi 热点(进入设置、查找正确的 SSID、连接等),并且应用程序应该只发送数据。

android sockets wifi hotspot
3个回答
1
投票

下面的示例显示了实现您想要完成的任务的一种方法。通过尝试它,您至少会感受到它工作时的样子。

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

有多种方法可以在浏览器和 Web 服务之间建立双向通信通道。例如。 WebSocket、AJAX 等

在以下示例中,单击发送按钮时,下面的发送浏览器会发送输入的文本。

enter image description here

当接收浏览器收到通知时,它会使用计数器值和新文本字符串更新浏览器内容。每次收到更新时它都会增加计数器。

enter image description here

在下面的 send.html 和 receive.html 代码中,Accord.js 在浏览器和 Accord 云服务之间建立通信通道。发送和接收浏览器使用 ActiveML(JSON 和 XML 元语言的混合体)与 Accord 云服务进行交互。

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>

为了让浏览器与 Accord Cloud Service 通信,需要从每个浏览器登录。您可以通过单击 ac.accord.com 上的登录按钮来创建一个临时免费帐户来尝试。创建帐户后,您需要远程登录到 ac.accord.com 并在执行任何“发送”或“接收”之前执行以下操作。在 Windows 上下载并使用 PuTTY。在 linux/bsd 上使用 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";

每次从“发送”浏览器或通过 telnet CLI 接收到设置命令时,“接收”浏览器都会更新其显示。

除了使用telnet CLI模式之外,您还可以使用各种编程语言(例如C/C++、Java、Perl、Python等)与Accord Cloud Service交互。

如果此任务有预算,基于订阅的解决方案可能值得评估。订阅基于云的解决方案可能是一种具有成本效益的解决方案。 (有时它的成本可能比你花在咖啡上的钱还要少!)。披露:我为 Accord 工作。


1
投票

除了使用 wifi,您还可以尝试蓝牙或 NFC。所有这些的问题是它们都需要相当多的设置,启用这个和那个。

NFC 非常酷,设置也相对容易。可能值得尝试。

根据您发送的数据,您还可以做一些神奇的事情,例如通过短信对其进行编码,或创建二维条形码,然后另一部手机通过摄像头扫描它。


现在如果你真的想播的话,和热点无关。您只能使用UDP,并将其广播到您的子网。其他客户端应该正在侦听该端口,他们将简单地获取它。做一些谷歌搜索,看看如何使用套接字发送广播。


0
投票

热点本质上是网络设备。他们通常不知道应用程序在做什么。

为了将数据从一台设备发送到许多其他设备,您需要一台服务器来“提交”或“发送”数据,然后服务器会将数据“推送”给连接到该服务器的所有其他用户,并拥有表示有兴趣接收更新。

我已经实现了一个解决方案来实现这一点。如果您希望我在这里发布示例代码,请告诉我。然后您可以尝试并了解如何实现它。您可以使用基于浏览器的设备/telnet 会话来查看它的工作情况。

© www.soinside.com 2019 - 2024. All rights reserved.