我现在已经完成了这项工作,可以通过 WiFi 从我的 PIC 向我的 Android 应用程序发送一个字节并接收一个或多个字节。
我创建了一个新线程来等待输入流上的数据。我的主要问题是确保在启动此线程之前连接套接字,一旦我怀疑它工作正常。
当接收到数据时,它会通过处理程序发送回主代码。我的处理程序根据接收到的内容向我的应用程序输出文本,这七个字节是临时读数以及时间和日期。仅显示单个字节。
附上我的代码可能不漂亮!欢迎任何建设性的批评。
package com.example.carl.sockets;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "Main Activity - Sockets";
private Handler h;
private Socket socket;
private static final int SERVERPORT = 23;
private static final String SERVER_IP = "192.168.1.78"; // enter IP of ESP8266 here
private receiveData rd;
protected static final int SUCCESS_CONNECT = 0;
protected static final int NOW_DATA_RECEIVED = 1;
protected static final int ONE_BYTE_RECEIVED = 2;
private boolean connectedSocket = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new ClientThread()).start();
while(!connectedSocket){}
rd = new receiveData(socket);
rd.start();
h = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case SUCCESS_CONNECT:
break;
case NOW_DATA_RECEIVED:
Log.d(TAG,"Handler - In 7 bytes received");
byte[] nowData = (byte[]) msg.obj;
// process data
int tempFrac = 0;
int tempUnits = 0;
// construct time and date string, pad to 2 figures, unsigned values
String hourStr = Integer.toString(nowData[2]&0xFF);
hourStr = "00".substring(hourStr.length()) + hourStr;
String minStr = Integer.toString(nowData[3]&0xFF);
minStr = "00".substring(minStr.length()) + minStr;
String dayStr = Integer.toString(nowData[4]&0xFF);
dayStr = "00".substring(dayStr.length()) + dayStr;
String monthStr = Integer.toString(nowData[5]&0xFF);
monthStr = "00".substring(monthStr.length()) + monthStr;
String yearStr = Integer.toString(nowData[6]&0xFF);
yearStr = "00".substring(yearStr.length()) + yearStr;
String nowDataString = new String(hourStr +":" + minStr + " " + dayStr + "/" + monthStr + "/" + yearStr);
// get unsigned values
int tempMSB = nowData[0] & 0xFF;
int tempLSB = nowData[1] & 0xFF;
// add LSB and MSB
int tempVar = tempLSB + (tempMSB<<8);
// if Zero
if(tempVar == 0){
// result zero so display zero degrees
// put in text view
TextView txtData = (TextView) findViewById(R.id.textView);
txtData.setText(nowDataString + " Temp = 0.0c");
}else if(tempMSB >= 0x80){
// result negative
tempVar = (~tempVar) + 1;
tempFrac = tempVar & 0x0F;
tempFrac = tempFrac*625/1000;
tempUnits = (tempVar>>4)&0x7F;
String tempString = new String("-"+Integer.toString(tempUnits)+"."+Integer.toString(tempFrac));
// put in text view
TextView txtData = (TextView) findViewById(R.id.textView);
txtData.setText(nowDataString + " Temp = " + tempString + "c");
}else{
// result positive
tempFrac = tempVar & 0x0F;
tempFrac = tempFrac*625/1000;
tempVar = tempVar>>4;
tempUnits = tempVar & 0x7F;
String tempString = new String(Integer.toString(tempUnits)+"."+Integer.toString(tempFrac));
// put in text view
TextView txtData = (TextView) findViewById(R.id.textView);
txtData.setText(nowDataString + " Temp = " + tempString + "c");
}
break;
case ONE_BYTE_RECEIVED:
Log.d(TAG,"Handler - In 1 byte received");
byte[] recByte = (byte[]) msg.obj;
int receivedByte = (int)recByte[0];
TextView txtData = (TextView) findViewById(R.id.textView);
txtData.setText( "One Byte Received = " + Integer.toString(receivedByte));
Log.d(TAG, "In handler, one byte received = "+ Integer.toString(receivedByte));
break;
}
}
};
}
@Override
public void onPause(){
super.onPause();
try{
socket.close();
}catch(IOException e){
Log.d(TAG,"Error closing socket in on pause");
}
rd.stopRdThread();
}
public void onClick(View view) {
try {
EditText et = (EditText) findViewById(R.id.EditText01);
Integer i = Integer.parseInt(et.getText().toString());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
if (i > 0 && i < 255) {
out.write(i);
Log.d(TAG,"Byte Sending - "+ i.toString());
} else {
Toast.makeText(this, "Number out of Range !", Toast.LENGTH_SHORT).show();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
class ClientThread implements Runnable {
@Override
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
socket = new Socket(serverAddr, SERVERPORT);
if(socket.isConnected()){
Log.d(TAG, "in ClientThread - socket connected");
connectedSocket = true;
}else{
Log.d(TAG, "in ClientThread - socket not connected");
connectedSocket = false;
}
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
class receiveData extends Thread {
private Socket socketLocal;
private volatile boolean exit = false;
DataInputStream in;
byte[] fullBuffer = new byte[7];
byte[] buffer = new byte[100];
int bytes; // bytes returned from read()
int bytesCount = 0;
// constructor
receiveData(Socket socket){
this.socketLocal = socket;
}
public void run(){
try {
if (socketLocal.isConnected()) {
Log.d(TAG, "In receiveData, Socket connected");
in = new DataInputStream(socketLocal.getInputStream());
}
}catch(Exception e){
Log.d(TAG, "in receiveData - run exception - " + e.toString());
}
while(!exit){
try {
if (socketLocal.isConnected()) {
if (in.available() > 0) {
//input = in.readByte(); // Get number of bytes and message in "buffer"
bytes = in.read(buffer);
Log.d(TAG,"Bytes Read = "+Integer.toString(bytes));
System.arraycopy(buffer,0,fullBuffer,bytesCount,bytes);
bytesCount = bytesCount + bytes;
if(bytesCount >= 7){
h.obtainMessage(NOW_DATA_RECEIVED, bytesCount, -1, fullBuffer).sendToTarget(); // Send to message queue Handler
Log.d(TAG,"In handler, 7 bytes Message sent");
bytesCount = 0;
}else if(bytesCount == 1){
h.obtainMessage(ONE_BYTE_RECEIVED, bytesCount, -1, fullBuffer).sendToTarget();
Log.d(TAG, "In handler, 1 byte message sent");
bytesCount = 0;
}
}
}else{
break;
}
}catch(Exception e){
Log.d(TAG, "Read Error - " + e.toString());
}
}
}
public void stopRdThread(){
exit = true;
try {
socketLocal.close();
}catch(Exception e){
Log.d(TAG, "error closing socket - " + e.toString());
}
}
}
}
我现在唯一的问题是内存泄漏,我想?
我收到一条消息-
07-30 17:46:01.154 15417-15445/com.example.carl.sockets D/dalvikvm: GC_FOR_ALLOC 释放 512K,22% 释放 9692K/12332K,暂停 18ms,共 18ms
应用启动后,显示器显示我正在使用 9.4 MB。