Android 将数据从一个安卓设备发送到另一个安卓设备
我必须将数据从一个安卓设备发送到许多其他安卓设备。这可能是单向通信,因为发送方将“推送”数据到接收方,接收方接收数据,对其执行一些修改并保存本地副本 我环顾了一下网络(包括stackoverflow),发现有很多解决方案:wifi P2P、通过服务器发送数据等。理想情况下,我希望使用wifi P2P,但我担心我的硬件不支持它,因此,我考虑改用无线热点功能 所以这里有一个问题:想象一下,将正在广播wifi热点的设备作为“主设备”,将连接到它的设备作为“从设备”(仅从主设备接收数据)。如何将数据从主设备(一个设备)广播到从设备(多个设备)?我是网络/套接字编程新手,因此一个简单的解决方案和大量的示例将非常有用。此外,可以安全地假设用户将手动连接到wifi热点(进入设置、找到正确的SSID、连接等),并且应用程序应该只发送数据Android 将数据从一个安卓设备发送到另一个安卓设备,android,sockets,wireless,personal-hotspot,Android,Sockets,Wireless,Personal Hotspot,我必须将数据从一个安卓设备发送到许多其他安卓设备。这可能是单向通信,因为发送方将“推送”数据到接收方,接收方接收数据,对其执行一些修改并保存本地副本 我环顾了一下网络(包括stackoverflow),发现有很多解决方案:wifi P2P、通过服务器发送数据等。理想情况下,我希望使用wifi P2P,但我担心我的硬件不支持它,因此,我考虑改用无线热点功能 所以这里有一个问题:想象一下,将正在广播wifi热点的设备作为“主设备”,将连接到它的设备作为“从设备”(仅从主设备接收数据)。如何将数据从主
非常感谢您抽出时间 热点本质上是网络设备。他们通常不知道应用程序在做什么 为了将数据从一个设备发送到多个其他设备,您需要一个服务器,您可以向该服务器“提交”或“发送”数据,然后服务器将数据“推送”到连接到该服务器的所有其他用户,并表示有兴趣接收更新
我已经实现了一个解决方案,可以做到这一点。如果你想让我在这里发布示例代码,请告诉我。然后,你可以玩游戏,学习如何让它发生。您可以使用基于浏览器的设备/telnet会话查看它的工作情况。您也可以尝试蓝牙或NFC,而不是使用wifi。所有这些的问题是它们都需要相当多的设置,启用这个和那个 NFC非常酷,安装也相对容易。也许值得一试 根据你发送的数据,你也可以做一些神奇的事情,比如通过短信对它们进行编码,或者创建一个二维条形码,然后另一部手机通过摄像头扫描
现在,如果你真的想广播,它与热点无关。您只能使用UDP,并将其广播到您的子网。其他客户端应该在端口上侦听,他们只需简单地获取它。做一些谷歌搜索,看看如何使用套接字发送广播。下面的示例展示了一种实现目标的方法。通过实验,你至少会感觉到它工作时的样子
+---------+ +---------+ +---------+
| Receive | | Receive | | Send |
| Browser | | Browser | | Browser |
+----+----+ +----+----+ +----+----+
| | |
| | |
+-------+------+--------------+ +---------+
| | telnet |
| +--------------------------+ CLI |
| | | session |
| | +---------+
+--+---+--+
| Accord | +------------------------+
| Cloud +----+ C/Java/Perl/Python etc |
| Service | | Program Language APIs |
+---------+ +------------------------+
有几种方法可以在浏览器和web服务之间建立双向通信通道。例如WebSocket、AJAX等
在以下示例中,单击“发送”按钮时,下面的“发送浏览器”将发送输入的文本
接收浏览器收到通知时,会使用计数器值和新文本字符串更新浏览器内容。每次收到更新时,它都会递增计数器
在下面的send.html和receive.html代码中,Accord.js在浏览器和Accord云服务之间建立了通信通道。发送和接收浏览器使用ActiveML与Accord云服务交互,ActiveML是JSON和XML元语言的混合体
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>
prompt>cat send.html
雅阁软件公司。
var-rpc;
函数运行(){
if(HttpPC的类型!=“函数”||
checkSessionId的类型!=“函数”){
setTimeout(函数(){run();},100);
返回;
}
rpc=new-amlhttprpc();
}
/*
*按“单击发送”按钮时发送文本字符串。
*此ActiveML命令将更新字符串值和任何
*具有未完成“等待更新”的会话将取消阻止
*并接收更新通知。
*/
函数sendMessage(){
var elem=document.getElementById(“SendMsg”);
调用('aml set string Demo.Msg=“”+elem.value+”;”);
}
run();
输入文本:
点击发送
提示符>cat recv.html
雅阁软件公司。
var-rpc;
var div=document.getElementById('Page');
/*
*显示字符串和增量计数器。
*/
var计数=0;
函数DisplayMsg(s){
div.innerHTML=count+':'+s;
计数++;
}
/*
*事件作为“ActiveML set string Demo.Msg=“hello,world”;”接收
*/
函数RecvMsg(s){
var eq=s.indexOf('=');
/*
*删除结尾处的引号和分号。
*/
s=s.子串(等式+4,s.长度-2);
显示消息;
}
/*
*最初调用DisplayString()以显示当前值
*然后是每个后续更新的RecvMsg()。
*/
函数运行(){
if(HttpPC的类型!=“函数”||
checkSessionId的类型!=“函数”){
setTimeout(函数(){run();},100);
返回;
}
rpc=new-amlhttprpc();
/*
*通过使用与后端服务的通信
*ActiveML。
*/
call('aml print string Demo.Msg;',DisplayMsg,RecvMsg);
call('aml等待打印字符串Demo.Msg;的更新',0,0);
}
run();
为了让浏览器与Accord云服务通信,需要从每个浏览器登录。您可以通过点击ac.accord.com上的登录按钮来创建一个临时免费帐户,并进行尝试。创建帐户后,您需要telnet到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";
prompt>telnet ac.accord.com
已连接到ac.accord.com。
转义字符为“^]”。
Accord ActiveML-版本1.0.0.0
版权(c)2001-2013,雅阁软件有限公司。保留所有权利。
ActiveML Uid:
密码:
欢迎
aml>创建AOObject演示;
aml>创建字符串Demo.Msg;
反洗钱>反洗钱