真正意义上的实时的话需要类似nrf这类2.4G无线模块,直接传输。但如果实时性要求不高的话可以用esp8266,里面集成了网络的协议栈,开发起来很方便,可以直接接入网络跟手机进行tcp、udp之类的配对,但会因为网络延迟之类产生延时。最后最差的就是各种云平台,因为有上报周期限制,往往同步速度延迟达到秒级别,但胜在可以方便地运行在不同网段,否则的话就需要自己搭公网——自己搭公网服务器可以拜托上报周期限制。
最推荐就是esp8266,里面集成at指令的协议栈:
AT+CIPMUX=x 开启mux多路连接,如果要设置为服务器时,必须开启
AT+CIPSERVER=x,y 开始服务器,x为1时开启,0关闭,y为开启的端口,http协议所用的端口为80
两个指令就开启tcp协议的服务器。
安卓开发的话:
private void connect_to_host() {
if(socket==null ) {
try {
//SocketAddress socAddress = new InetSocketAddress("192.168.155.1", 6000);
//socket.connect(socAddress, 3000);//超时3秒
String address_str=edit1.getText().toString()+"."+edit2.getText().toString()+"."+edit3.getText().toString()+"."+edit4.getText().toString();
int port_value=Integer.parseInt(edit5.getText().toString());
socket=new Socket(address_str,port_value); //创建Socket实例,并绑定连接远端IP地址和端口
command_str="success connect!";
Log.e("线程反馈", "创建成功!");
out = new DataOutputStream(socket.getOutputStream());
out.writeUTF("success");
}
catch (Exception e) {
Log.e("线程反馈", "线程异常!");
e.printStackTrace();
command_str="error retry please!";
}
}
else
command_str="socket already connect!";
}
创建类似的tcp客户端。