android通过wifi连接接收和发送数据到硬件
实际上,我想把数据从硬件发送到安卓设备。 硬件设备连接到本地无线路由器,该路由器连接到调制解调器。 Android设备也将通过WI-FI连接到同一路由器。 你能推荐一些链接或教程吗?我可以从中了解如何在硬件设备和android设备之间建立通信,通过WI-FI发送和接收数据。请提供任何示例代码或链接android通过wifi连接接收和发送数据到硬件,android,Android,实际上,我想把数据从硬件发送到安卓设备。 硬件设备连接到本地无线路由器,该路由器连接到调制解调器。 Android设备也将通过WI-FI连接到同一路由器。 你能推荐一些链接或教程吗?我可以从中了解如何在硬件设备和android设备之间建立通信,通过WI-FI发送和接收数据。请提供任何示例代码或链接 import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class SimpleClientActivity extends Activity {
private Socket client;
private FileInputStream fileInputStream;
private BufferedInputStream bufferedInputStream;
private OutputStream outputStream;
private Button button;
private TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button1); //reference to the send button
text = (TextView) findViewById(R.id.textView1); //reference to the text view
//Button press event listener
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
File file = new File("/mnt/sdcard/input.jpg"); //create file instance, file to transfer or any data
try {
client = new Socket("10.0.2.2", 4444);// ip address and port number of ur hardware device
byte[] mybytearray = new byte[(int) file.length()]; //create a byte array to file
fileInputStream = new FileInputStream(file);
bufferedInputStream = new BufferedInputStream(fileInputStream);
bufferedInputStream.read(mybytearray, 0, mybytearray.length); //read the file
outputStream = client.getOutputStream();
outputStream.write(mybytearray, 0, mybytearray.length); //write file to the output stream byte by byte
outputStream.flush();
bufferedInputStream.close();
outputStream.close();
client.close();
text.setText("File Sent");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
//要发送消息,您还可以使用以下代码
public static String ipAddress;// ur ip
public static int portNumber;// portnumber
private Socket client;
private OutputStreamWriter printwriter;
private String message;
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
client = new Socket(ipAddress, portNumber);
printwriter = new OutputStreamWriter(client
.getOutputStream(), "ISO-8859-1");
printwriter.write("any message");
printwriter.flush();
printwriter.close();
client.close();
}
catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
你能详细介绍一下硬件设备吗?那个硬件设备是什么。它的网络接口是什么?它提供的接口是什么?这里实际上我有一个自定义硬件,它将作为一个无线接入点工作,允许无线设备(android手机)使用Wi-Fi连接到有线网络。Wi-Fi内置在硬件中。我通过android中一个名为NSD(网络服务发现)的API这是否有助于识别通过Wi-Fi连接的h/w并向该h/w发送命令。。。建议在此感谢Anil的快速响应!!!!对于在安卓系统接收数据,我需要编写客户端代码,这意味着在硬件端应该有服务器代码,可以在安卓系统端与客户端进行通信。第二个问题,wi-fi在哪里使用请详细说明一下,我真的不知道通过安卓设备和硬件通过wi-fi进行数据通信。实际上,我是有一个自定义硬件,可作为无线接入点,允许无线设备(android手机)使用Wi-Fithanks anil连接到有线网络,我需要配置/设置作为android设备接入点的硬件设备,在客户端代码开始之前,我还需要检查android设备上是否有wi-fi,然后我需要传递硬件设备ip地址和ip,如果我是Right Hi Anil请更正!!让硬件设备和android mobile之间的数据传输成为可能有一些疑问2种方式是可能的1.硬件设备将作为接入点。或2.android设备将作为接入点。我的问题是,如果我将硬件设备作为接入点,那么在安卓mobile中设置该接入点后,我的android应用程序将如何获取设置访问点的详细信息