赞
踩
**
分为操作分析和代码分析。
1.代码分析:
代码1获取连接热点的IP地址,然后写入:(这个代码之后添加的,用户可自行复制粘贴)
WifiManager wifiManager = (WifiManager) getApplicationContext(). getSystemService(Context.WIFI_SERVICE);//定义服务器
DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();//获取服务器信息
String IP = Formatter.formatIpAddress(dhcpInfo.gateway);//获取服务器IP
edtIP = (EditText) this.findViewById(R.id.id_edt_inputIP);
edtIP.setText(IP+"");//
代码2.建立连接
//建立连接按钮的线程 private void connectThread() { if (!isConnected) { new Thread(new Runnable() { public void run() { Looper.prepare(); Log.i(tag, "---->> connect/close server!"); connectServer(edtIP.getText().toString(), edtPort.getText()//获取输入框的IP和端口号 .toString()); isConnected=true; } }).start(); } else { try { if (mSocket != null) { mSocket.close(); mSocket = null; Log.i(tag, "--->>重新连server."); } } catch (IOException e) { e.printStackTrace(); } btnConn.setText("断开连接"); edtIP.setEnabled(true); edtPort.setEnabled(true); isConnected = false; } } //与服务器连接 private void connectServer(String ip, String port) { try { Log.e(tag, "--->>start connect server !" + ip + "," + port); mSocket = new Socket(ip, Integer.parseInt(port)); Log.e(tag, "--->>end connect server!"); OutputStream outputStream = mSocket.getOutputStream(); printWriter = new PrintWriter(new BufferedWriter( new OutputStreamWriter(outputStream, Charset.forName("gb2312")))); in = mSocket.getInputStream(); myHandler.sendEmptyMessage(2); showInfo("连接服务器成功"); } catch (Exception e) { isConnected = false; showInfo("连接服务器失败"); Log.e(tag, "exception:" + e.toString()); } }
代码3.十六进制与ACSII的切换
case R.id.buttonfasong: if (sendon.getText().equals("16进制发送")) { sendon.setText("HEX发送"); } else { sendon.setText("16进制发送"); } break; case R.id.buttonjieshou: if (receiveon.getText().equals("16进制接收")) { receiveon.setText("HEX接收"); } else { receiveon.setText("16进制接收"); } break;
代码4.十六进制接收于发送代码(难点,也是开发中最重要的,这里主要参考蓝牙十六进制而改的)
private String getHexString() { String s = edtSend.getText().toString(); StringBuilder sb = new StringBuilder(); for ( i = 0; i < s.length(); i++) { char c = s.charAt(i); if (('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')) { sb.append(c); } } if ((sb.length() % 2) != 0) { sb.deleteCharAt(sb.length()); } return sb.toString(); } private byte[] stringToBytes(String s) { byte[] buf = new byte[s.length() / 2]; for (int i = 0; i < buf.length; i++) { try { buf[i] = (byte) Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16); } catch (NumberFormatException e) { e.printStackTrace(); } } return buf; } public String bytesToString(byte[] bytes) { try { final char[] hexArray = "0123456789ABCDEF".toCharArray(); char[] hexChars = new char[bytes.length * 2]; StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { int v = bytes[i] & 0xFF; hexChars[i * 2] = hexArray[v >>> 4]; hexChars[i * 2 + 1] = hexArray[v & 0x0F]; sb.append(hexChars[i * 2]); sb.append(hexChars[i * 2 + 1]); sb.append(' '); } return sb.toString(); }catch (Exception e) { Log.e(tag, "--->> send failure!" + e.toString()); } return null; } public String asciiToString(byte[] bytes) { char[] buf = new char[bytes.length]; StringBuilder sb = new StringBuilder(); for (int i = 0; i < buf.length; i++) { buf[i] = (char) bytes[i]; sb.append(buf[i]); } return sb.toString(); }
剩下的都是Button textview 的声明了。
操作分析如下:
1.PC端测试
1.PC连接手机的热点:
2.PC上输入cmd指令:
2.接着输入ipconfig 查看网络信息
3.找WiFi的网络 192.168.43.161
4.点击窗口调试工具
5,配置参数
5.试验如下
2.ESP8266 测试
1.AT+RST (复位)
2.AT+CWMODE=2 (服务器模式)
3.AT+CWSAP=“traffic”,“12345678”,11,0(“名字和密码”)
4.AT+CWMODE=2(传输模式)
5.AT+CIPMUX=1
6.AT+CIPSERVER=1,9999 (9999为端口号)
7.AT+CIFSR (查看ip)
8.AT+CIPSEND=1,6(0表示客户端的地址,也可是其他数,主要看客户端的数目;6表6个字节)
手机连接这个热点,使用代码1,获取IP地址,再填入端口号就能使用了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。