android通过wifi连接接收和发送数据到硬件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20345155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android receive and send data through wifi connection to hardware
提问by Neelam Singh
Actually I want to send data from a hardware piece to an android device. The hardware device is connected to local wireless router which is connected to modem. Android device will also connected to same router through WI-FI. Can you please suggest some links or tutorial from where i can get idea how to establish communication between hardware device an the android device to send and receive data through WI-FI .Please Help any sample code or links
实际上,我想将数据从硬件发送到 android 设备。硬件设备连接到本地无线路由器,该路由器连接到调制解调器。Android 设备也将通过 WI-FI 连接到同一路由器。您能否建议一些链接或教程,我可以从中了解如何在硬件设备和 android 设备之间建立通信以通过 WI-FI 发送和接收数据。请帮助任何示例代码或链接
回答by Looking Forward
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();
}
}
});
}
}
// to send message u can also use below code
// 发送消息你也可以使用下面的代码
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();