android通过wifi连接接收和发送数据到硬件

标签 android

实际上我想将数据从硬件发送到 android 设备。 硬件设备连接到本地无线路由器,该路由器连接到调制解调器。 Android 设备也将通过 WI-FI 连接到同一路由器。 您能否建议一些链接或教程,从中我可以了解如何在硬件设备和 Android 设备之间建立通信以通过 WI-FI 发送和接收数据。请帮助任何示例代码或链接

最佳答案

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通过wifi连接接收和发送数据到硬件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20345155/

相关文章:

android - 停止失败,带有 TarsosDSP 的 Android 应用程序

android - 使用 Picasso 在后台加载图像时显示 "Loading..."图像

android - 在 Android 中抓取帧视频到 Gif

android - 我们能否以编程方式覆盖设备的 TTS 引擎设置?

安卓doInBackground

android - Firebase 多位置规则 - 相同的值

java - 从另一个线程运行线程

java - 从 Android 图库加载位图图像时出现 FileNotFound 异常

android - 只读 mutableStateListOf

java - Android中的泄漏窗口异常