通过WiFi连接在Android设备和硬件之间传输数据

8

实际上,我想将硬件设备中的数据发送到 Android 设备。

硬件设备连接到本地无线路由器,该路由器连接到调制解调器。

Android 设备也将通过 WI-FI 连接到同一路由器。

请您推荐一些链接或教程,以便我可以了解如何在硬件设备和 Android 设备之间建立通信,通过 WI-FI 发送和接收数据。请帮忙提供任何示例代码或链接。


1
请您详细说明一下这个硬件设备,它是什么样的硬件设备?它有哪些网络接口?它提供哪些接口? - Aman Gautam
这里实际上我有一台自定义硬件,它将作为一个无线接入点工作,允许无线设备(安卓手机)使用Wi-Fi连接到有线网络。而且Wi-Fi已经内置在硬件中。 - Neelam Singh
我在Android中使用了一个名为NSD(网络服务发现)的API,通过它是否有助于识别通过Wi-Fi连接的硬件并向其发送命令...请就此提供建议。 - Venkatraman
1个回答

7
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();

感谢Anil的快速回复!!!为了在Android上接收数据,我需要编写客户端代码,这意味着在硬件侧应该有服务器代码,它将与Android侧的客户端通信。第二个问题是Wi-Fi的用途在哪里? - Neelam Singh
请详细说明一下,我对通过 Android 设备和硬件进行 Wi-Fi 数据通信并不了解。 - Neelam Singh
这里实际上我有一台自定义硬件,它将作为一个无线接入点工作,允许无线设备(安卓手机)使用Wi-Fi连接到有线网络。 - Neelam Singh
谢谢Anil,我理解的是我需要在Android设备上配置/设置充当访问点的硬件设备,并且在客户端代码启动之前,我还需要检查Android设备上是否开启了Wi-Fi,然后我需要传递硬件设备的IP地址和IP。如果我理解有误,请纠正我。 - Neelam Singh
你好 Anil!我有些疑问,关于如何在硬件设备和安卓手机之间实现数据传输。有两种方式:1. 硬件设备作为接入点;2. 安卓设备作为接入点。我的问题是,如果我将硬件设备设置为接入点,在安卓手机中设置完接入点后,我的安卓应用程序如何获取已设置的接入点的详细信息? - Neelam Singh
@AnilBhatiya 你会回来看这个死帖的机会有多大?在新线程中运行消息发送的意义是什么,但不运行文件发送呢? - Chemistpp

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接