您的位置:首頁>正文

利用Socket與硬體通信

前幾天做一個智慧家居APP,硬體段使用的是ESP8266WIFI模組, 其實不管是WIFI模組還是藍牙, 通信都是同樣一個道理, 獲取IP和埠來進行通信。

我是通過XCOM v2.0 發送資訊, 移動端接收資訊後也可以發資訊過去, 介面如下(比較簡單自己寫就行)

與硬體通信肯定免不了解析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許可權

1 @Override 2 protected void onCreate(Bundle savedInstanceState) { 3 4 super.onCreate(savedInstanceState); 5 setContentView(R.layout.activity_main); 6 7 mEditText = (EditText) findViewById(R.id.edittext); 8 connectButton = (Button) findViewById(R.id.connectbutton); 9 sendButton = (Button) findViewById(R.id.sendbutton); 10 mTextView = (TextView) findViewById(R.id.retextview); 11 12 13 //連接按鈕 14 connectButton.setOnClickListener(new View.OnClickListener { 15 @Override 16 public void onClick(View view) { 17 new Thread { 18 @Override 19 public void run { 20 super.run; 21 try { 22 clientSocket = new Socket("192.168.23.6", 3000); 23 mReceiveThread = new ReceiveThread(clientSocket); 24 mReceiveThread.start; 25 }catch (IOException e) { 26 e.printStackTrace; 27 } 28 } 29 }.start; 30 displayToast("連接成功"); 31 stop = false; 32 } 33 }); 34 //發送資料 35 sendButton.setOnClickListener(new View.OnClickListener { 36 @Override 37 public void onClick(View view) { 38 byte msgBuffer = null; 39 40 String text = mEditText.getText.toString; 41 42 try { 43 msgBuffer = text.getBytes("GB2312"); 44 outStrean = clientSocket.getOutputStream; 45 outStrean.write(msgBuffer); 46 } catch (IOException e) { 47 e.printStackTrace; 48 } 49 mTextView.setText(text); 50 displayToast("發送成功"); 51 } 52 }); 53 54 } 55 56 private void displayToast(String s) { 57 Toast.makeText(this, s, Toast.LENGTH_SHORT).show; 58 } 59 60 private class ReceiveThread extends Thread { 61 private InputStream inStrean = null; 62 private byte buf11 = new byte[1024]; 63 private String str11 = null; 64 65 public ReceiveThread(Socket s) { 66 try { 67 this.inStrean = s.getInputStream; 68 } catch (IOException e) { 69 e.printStackTrace; 70 } 71 } 72 73 @Override 74 public void run { 75 super.run; 76 while (!stop) { 77 try { 78 this.inStrean.read(buf11); 79 str11 = new String(buf11,"GB2312"); 80 Gson gson = new Gson; 81 JsonReader reader = new JsonReader(new StringReader(str11)); 82 reader.setLenient(true); 83 Student student = gson.fromJson(reader,Student.class); 84 85 String name = student.getName; 86 // System.out.println(student.toString); 87 Message msg = new Message; 88 msg.obj = name; 89 mHandle.sendMessage(msg); 90 } catch (IOException e) { 91 e.printStackTrace; 92 } 93 } 94 } 95 } 96 97 98 @Override 99 public void onDestroy { 100 super.onDestroy; 101 if (mReceiveThread != null) { 102 try { 103 clientSocket.close; 104 stop = true; 105 mReceiveThread.interrupt; 106 } catch (IOException e) { 107 e.printStackTrace; 108 } 109 } 110 } 111 }
同類文章
Next Article
喜欢就按个赞吧!!!
点击关闭提示