android wifi的基本思路

来源:互联网 发布:手机网络主播怎么加入 编辑:程序博客网 时间:2024/05/16 19:39

举个例子:
比如,采集,摄像头的照片的信息(这里所说的照片,不是指手机摄像头拍摄的信息,而是说,开发板上面的摄像头,所拍摄的照片),到手机上面来.
既然数据之间有了交互,所以就要建立连接,这里采用wifi连接的方式.
就是说,我们开发板上面,集成一块wifi的板子,这块板子会连接到家庭的wifi中,

所以,我们andorid工程师的任务,就是,连接上这块wifi板子.拿到放回来的数据.
简单来说就是使用tcp/ip的连接.
示例代码:

    Socket sock = null;    OutputStream out = null;    InputStream in = null;    //wifi板上面的ip和端口    sock = new Socket("192.168.199.18", 8888);    out = sock.getOutputStream();    in = sock.getInputStream();    out.write("GP".getBytes());    out.flush();    byte[] cbuf = new byte[4];    in.read(cbuf);    int len = byteArray2int(cbuf);    System.out.println("len="+len);    final byte[] buffer = new byte[len];    baos = new ByteArrayOutputStream();    while (len > 0) {        int size = in.read(buffer);        baos.write(buffer, 0, size);        len -= size;    }        baos.close();
//这个方法,对数据的处理,和c工程师约好的    public static int byteArray2int(byte[] b) {        byte[] a = new byte[4];        int i = a.length - 1, j = b.length - 1;        for (; i >= 0; i--, j--) {// 从b的尾部(即int值的低位)开始copy数据            if (j >= 0)                a[i] = b[j];            else                a[i] = 0;// 如果b.length不足4,则将高位补0        }        int v0 = (a[0] & 0xff) << 24;// &0xff将byte值无差异转成int,避免Java自动类型提升后,会保留高位的符号位        int v1 = (a[1] & 0xff) << 16;        int v2 = (a[2] & 0xff) << 8;        int v3 = (a[3] & 0xff);        return v0 + v1 + v2 + v3;    }
0 0
原创粉丝点击