利用Socket与硬件通信(智能家居)

   前几天做一个智能家居APP,硬件段使用的是ESP8266WIFI模块,其实不管是WIFI模块还是蓝牙,通信都是同样一个道理,获取IP和端口来进行通信。

我是通过XCOM v2.0 发送信息,移动端接收信息后也可以发信息过去,界面如下(比较简单自己写就行)

利用Socket与硬件通信(智能家居)

  与硬件通信肯定免不了解析Json,使用Gson解析简单方便,在解析过程中遇到了一个坑爹的问题,解析出错了(;′⌒`),我在电脑输入下面字符串

{\"name\":\"John\", \"age\":20,\"grade\":{\"course\":\"English\",\"score\":100,\"level\":\"A\"}}

\"是代表 "这个符号,所以我就从XCOM v2.0发送上面字符串过来,解析失败。。。后来debug发现发送过来的信息是直接当字符串处理,不会再转义,也就\"是代表\",

所以只需发送下面格式信息即可解析

{"name":"John", "age":20,"grade":{"course":"English","score":100,"level":"A"}}    接下来是是代码,代码比较容易,连接局域网(WIFI或者热点都行)  这里的代码是基于知道硬件ip和port上的,当然你也可以通过UDP广播去获取ip和port。 Socket连接也属于网络连接,必须放在子线程中。 注意在清单文件配置网络和WIFI权限

@Override
     protected void onCreate(Bundle savedInstanceState) {
 
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
 
         mEditText = (EditText) findViewById(R.id.edittext);
         connectButton = (Button) findViewById(R.id.connectbutton);
         sendButton = (Button) findViewById(R.id.sendbutton);
         mTextView = (TextView) findViewById(R.id.retextview);
    
 
             //连接按钮 
           connectButton.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 new Thread() {
                     @Override
                     public void run() {
                         super.run();
                         try {
                             clientSocket = new Socket("192.168.23.6", 3000);
                             mReceiveThread = new ReceiveThread(clientSocket);
                             mReceiveThread.start();
                         }catch (IOException e) {
                             e.printStackTrace();
                         }
                     }
                 }.start();
                 displayToast("连接成功");
                 stop = false;
             }
         });
           //发送数据
         sendButton.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 byte[] msgBuffer = null;
 
                 String text = mEditText.getText().toString();
 
                 try {
                     msgBuffer = text.getBytes("GB2312");
                     outStrean = clientSocket.getOutputStream();
                     outStrean.write(msgBuffer);
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
                 mTextView.setText(text);
                 displayToast("发送成功");
             }
         });
 
     }
 
     private void displayToast(String s) {
         Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
     }
 
     private class ReceiveThread extends Thread {
         private InputStream inStrean = null;
         private byte[] buf11 = new byte[1024];
         private String str11 = null;
 
         public ReceiveThread(Socket s) {
             try {
                 this.inStrean = s.getInputStream();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
 
         @Override
         public void run() {
             super.run();
             while (!stop) {
                 try {
                     this.inStrean.read(buf11);
                     str11 = new String(buf11,"GB2312");
                     Gson gson = new Gson();
                     JsonReader reader = new JsonReader(new StringReader(str11));
                     reader.setLenient(true);
                     Student student = gson.fromJson(reader,Student.class);
 
              String name = student.getName();
             //    System.out.println(student.toString());
                     Message msg = new Message();
                     msg.obj = name;
                     mHandle.sendMessage(msg);
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
         }
     }
 
 
     @Override
     public void onDestroy() {
         super.onDestroy();
         if (mReceiveThread != null) {
             try {
                 clientSocket.close();
                 stop = true;
                 mReceiveThread.interrupt();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
     }
 }

相关推荐