事物的难度远远低于对事物的恐惧
完成对STM32单片机和ESP8266 WiFi模块的配置之后,接下来需要完成Android APP代码的编写以及实现。
1.添加网络权限
因为我们需要对WiFi进行操作,所以需要网络的权限,在AndroiManifest.xml文件中加入以下代码:
<uses-permission android:name="android.permission.INTERNET" />
2.建立TCP连接
首先通过文本输入控件得到IP地址和端口号,然后建立Socket连接。
case R.id.btn_turn:if (mSocket == null || !mSocket.isConnected()) {if(!TextUtils.isEmpty(mText_ip.getText()) && !TextUtils.isEmpty(mText_port.getText())){mip = mText_ip.getText().toString();mport = Integer.parseInt(mText_port.getText().toString());Log.i(TAG, "onClick: "+mport);mConnectThread = new ConnectThread(mip, mport);mConnectThread.start();}else if(TextUtils.isEmpty(mText_ip.getText())){Toast.makeText(this,"请输入IP地址",Toast.LENGTH_SHORT).show();}else if(TextUtils.isEmpty(mText_port.getText())){Toast.makeText(this,"请输入端口号",Toast.LENGTH_SHORT).show();}}if (mSocket != null && mSocket.isConnected()) {try {mSocket.close();mSocket = null;mBtn_turn.setText("连接");Toast.makeText(this, "连接已断开", Toast.LENGTH_SHORT).show();} catch (IOException e) {e.printStackTrace();}}break;
上述代码是通过按钮点击事件获得到IP地址和端口号,然后通过创建一个ConnectThread类对象并调用里面的run()方法实现TCP连接。该类的代码如下:
private class ConnectThread extends Thread {private String ip;private int port;public ConnectThread(String ip, int port) {this.ip = ip;this.port = port;}@Overridepublic void run() {try {mSocket = new Socket(ip, port);out = new PrintStream(mSocket.getOutputStream());mBufferedReader = new BufferedReader(new InputStreamReader(mSocket.getInputStream()));runOnUiThread(new Runnable() {@Overridepublic void run() {mBtn_turn.setText("断开");Toast.makeText(MainActivity.this, "连接成功", Toast.LENGTH_SHORT).show();}});} catch (IOException e) {e.printStackTrace();runOnUiThread(new Runnable() {@Overridepublic void run() {Toast.makeText(MainActivity.this, "连接失败", Toast.LENGTH_SHORT).show();}});}}}
在run()方法中不仅建立了连接,而且还获得了该连接的输出流对象 out (用于APP向单片机发送数据)和输入流对象 mBufferedReader (用于APP接收单片机发来的数据)。
3.APP发送数据
在上文中得到了输出流对象 out,利用它的print()和flush()方法完成数据的发送,代码如下:
case R.id.btn_send:final String str = mSend_text.getText().toString();if (str != null && out != null) {new Thread(new Runnable() {@Overridepublic void run() {out.print(str);out.flush();}}).start();}break;
4.APP接收数据
在这里本文使用了定时器,设置每隔一段时间就去判断数据输入流对象mBufferedReader的方法ready(),从而得知是否有数据到来了,如果有就存入到一个全局变量中,然后通过Handler对象发送消息,进而实现在UI线程中更新TextView控件,代码如下:
private class ReceiveDataTask extends TimerTask {@Overridepublic void run() {try {if (mBufferedReader != null && (mBufferedReader.ready())) {char[] readbuff = new char[30];byte[] readByte = new byte[30];mBufferedReader.read(readbuff, 0, readbuff.length);tempStrng = String.valueOf(readbuff);readByte = tempStrng.getBytes();readStrng = new String(readByte, 0, readByte.length, "GB2312");Message message = Message.obtain();message.what = 1212;mHandler.sendMessage(message);}} catch (IOException e) {e.printStackTrace();}}}private void startTimer() {Log.i(TAG, "startTimer:");if (mTimer == null) {mTimer = new Timer();}if (mReceiveDataTask == null) {mReceiveDataTask = new ReceiveDataTask();}mTimer.schedule(mReceiveDataTask, 0, 10);}private void stopTimer() {Log.i(TAG, "stopTimer: ");if (mReceiveDataTask != null) {mReceiveDataTask.cancel();mReceiveDataTask = null;}if (mTimer != null) {mTimer.cancel();mTimer = null;}}
消息处理代码如下:
private class myHandler extends Handler {@Overridepublic void handleMessage(@NonNull Message msg) {super.handleMessage(msg);switch (msg.what) {case 1212:mSend_text_dis.setText(readStrng);break;}}}
开启定时器和关闭定时器代码如下:
@Overrideprotected void onStart() {super.onStart();startTimer();}@Overrideprotected void onStop() {super.onStop();stopTimer();}
现在就可以实现了数据的互相传输。
STM32单片机和Android APP源代码免费获取方式:
下位机硬件配置可以参考这个文章:
STM32单片机通过ESP8266WiFi模块与Android APP实现数据传输(一)—下位机硬件配置