Android的接收,并通过wifi连接硬件发送数据 [英] android receive and send data through wifi connection to hardware

查看:627
本文介绍了Android的接收,并通过wifi连接硬件发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其实我想从一个硬件一块数据发送到Android设备。 所述硬件装置被连接到被连接到调制解调器的本地无线路由器。 Android设备也将连接到同一台路由器通过Wi-Fi。 能否请你建议我从哪里可以得到的想法如何建立硬件设备的Andr​​oid设备之间的通信通过Wi-Fi,请您及时发送和接收数据帮助一些链接或教程任何样品code或链接

解决方案

 进口java.io.BufferedInputStream中;
进口的java.io.File;
进口java.io.FileInputStream中;
进口java.io.IOException异常;
进口java.io.OutputStream中;
进口的java.net.Socket;
进口的java.net.UnknownHostException;

进口android.app.Activity;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;
进口android.widget.TextView;

公共类SimpleClientActivity延伸活动{

 私人客户端的Socket;
 私人的FileInputStream的FileInputStream;
 私人的BufferedInputStream的BufferedInputStream;
 私人的OutputStream的OutputStream;
 私人Button按钮;
 私人TextView的文字;

 @覆盖
 公共无效的onCreate(包savedInstanceState){
  super.onCreate(savedInstanceState);
  的setContentView(R.layout.main);

  按钮=(按钮)findViewById(R.id.button1); //参照发送按钮
  文=(TextView的)findViewById(R.id.textView1); //参考文本视图

  //按钮preSS事件监听器
  button.setOnClickListener(新View.OnClickListener(){

   公共无效的onClick(视图v){


    档案文件=新的文件(到/ mnt / SD卡/ input.jpg); //创建文件的情况下,文件传输或任何数据

    尝试 {

     客户端=新的Socket(10.0.2.2,4444);乌拉圭回合的硬件设备// IP地址和端口号

     byte []的mybytearray =新的字节[(INT)file.length()]; //创建一个字节数组到文件

     的FileInputStream =新的FileInputStream(文件);
     的BufferedInputStream =新的BufferedInputStream(的FileInputStream);

     bufferedInputStream.read(mybytearray,0,mybytearray.length); //读取文件

     的OutputStream = client.getOutputStream();

     outputStream.write(mybytearray,0,mybytearray.length); //写文件以由字节输出流字节
     outputStream.flush();
     bufferedInputStream.close();
     outputStream.close();
           client.close();

           text.setText(文件已发送);


    }赶上(UnknownHostException异常E){
     e.printStackTrace();
    }赶上(IOException异常E){
     e.printStackTrace();
    }


   }
  });

 }
}
 

//发送消息u还可以使用下面code

 公共静态字符串ip地址; // UR IP
    公共静态INT端口号; //端口号

    私人客户端的Socket;

    私人OutputStreamWriter PrintWriter的;
    私人字符串信息;


新主题(新的Runnable(){

                    @覆盖
                    公共无效的run(){
                        // TODO自动生成方法存根
                        尝试 {
                            客户端=新的Socket(ip地址,端口号);
                            PrintWriter的=新OutputStreamWriter(客户端
                                    .getOutputStream(),ISO-8859-1);
                            printwriter.write(任何消息);
                            printwriter.flush();
                            printwriter.close();
                            client.close();
                        }

                        赶上(UnknownHostException异常E){
                            e.printStackTrace();
                        }赶上(IOException异常E){
                            // TODO自动生成的catch块
                            e.printStackTrace();
                        }
                    }
                })。开始();
 

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

解决方案

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

这篇关于Android的接收,并通过wifi连接硬件发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆