前言
之前做了一个STM32+ESP8266实现远程控制LED的小试验,里面用到了网络调试助手的TCP客户端,看了一些教程,尝试自己写一个TCP客户端调试助手,记录如下,仅供参考
一、界面布局
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp"
android:background="@drawable/bac3"
>
<EditText
android:id="@+id/et_1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="16sp"
android:textColor="#FFFFFF"
android:hint="IP地址"
android:textColorHint="#FFFFFF"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:background="@drawable/ipadress"
android:layout_margin="10dp"
/>
<EditText
android:id="@+id/et_2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/et_1"
android:textSize="16sp"
android:textColor="#FFFFFF"
android:hint="端口号"
android:textColorHint="#FFFFFF"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:background="@drawable/ipadress"
android:layout_margin="10dp"
/>
<Button
android:id="@+id/connect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/et_2"
android:background="@drawable/anjian"
android:layout_marginTop="40dp"
android:text="连接"
android:textSize="24dp"
android:textColor="#A59493"
/>
<TextView
android:id="@+id/send_receive"
android:layout_width="match_parent"
android:layout_height="350sp"
android:layout_marginTop="10dp"
android:layout_below="@id/connect"
android:singleLine="false"
android:scrollbars="vertical"
android:fadeScrollbars="false"
android:padding="5dp"
android:textColor="#FFFFFF"
/>
<Button
android:id="@+id/send"
android:layout_width="80dp"
android:layout_height="50dp"
android:text="发送"
android:textColor="#A59493"
android:textSize="20dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="@drawable/anjian"
android:layout_marginLeft="5dp"
/>
<EditText
android:id="@+id/content"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_toLeftOf="@id/send"
android:layout_alignParentBottom="true"
android:background="@drawable/ipadress"
android:padding="10dp"
android:textColor="#FFFFFF"
/>
</RelativeLayout>
有一些要注意的地方是:
1、 因为我改了背景,所以首先要把自己的背景图放到res/drawable中,然后在bulid.gradle中加入下面的代码
aaptOptions.cruncherEnabled = false
aaptOptions.useNewCruncher = false
如图:
2、 把软件下到手机上发现APP顶部有一块蓝色部分,搜了一下怎么去掉,方法是在res/values/styles.xml中把DarkActionBar改成NoActionBar
如图:
3、 还有一个地方是,为了让我们接收发送的消息可以滚动,在下面的那个TextView中需要加这几个属性
二、MainActivity
代码如下:
public class MainActivity extends AppCompatActivity {
private Button bt1;
private EditText et1;
private EditText et2;
private Button bt2;
private EditText et3;
private TextView receive;
private String ip;
private int port;
boolean connected = false;
public Socket socket = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.et_1);
et2 = (EditText) findViewById(R.id.et_2);
et3 = (EditText) findViewById(R.id.content);
bt1 = (Button) findViewById(R.id.connect);
bt2 = (Button) findViewById(R.id.send);
receive = (TextView) findViewById(R.id.send_receive);
receive.setMovementMethod(ScrollingMovementMethod.getInstance()); //滚动显示
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(connected == false){
ConnectThread ct = new ConnectThread();
ct.start();
return;
}
else{
bt1.setText("连接");
connected = false;
try{
socket.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
});
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("发送事件","开始发送");
if(bt1.getText().toString().equals("断开连接"))
{
sendComd(et3.getText().toString());
receive.append("Send : "+et3.getText().toString()+"\n");
int offset=receive.getLineCount()*receive.getLineHeight(); //滚动显示
if(offset>receive.getHeight()){
receive.scrollTo(0,offset-receive.getHeight());
}
}
et3.setText("");
}
});
}
class ConnectThread extends Thread{
public void run() {
try{
//从编辑框获得地址和端口
InetAddress ipAddress = InetAddress.getByName(et1.getText().toString());
int port = Integer.valueOf(et2.getText().toString());
socket = new Socket(ipAddress,port);
connected = true;
runOnUiThread(new Runnable() {
@Override
public void run() {
bt1.setText("断开连接");
}
});
try{
OutputStream outputStream = socket.getOutputStream();
outputStream.write("connect".getBytes());//发送connect数据给服务端令其做好准备
}catch (IOException e){
e.printStackTrace();
}
ReceiveThread rt = new ReceiveThread();//启动接收线程
rt.start();
Looper.prepare();
Toast.makeText(MainActivity.this,"连接成功",Toast.LENGTH_SHORT).show();
Looper.loop();
}catch (UnknownHostException e){
Looper.prepare();
Toast.makeText(MainActivity.this,"连接失败",Toast.LENGTH_SHORT).show();
Looper.loop();
e.printStackTrace();
}catch (IOException e){
Looper.prepare();
Toast.makeText(MainActivity.this,"连接失败",Toast.LENGTH_SHORT).show();
Looper.loop();
e.printStackTrace();
}
}
}
public void sendComd(final String cmd)
{
new Thread()
{
public void run()
{
try {
OutputStream os = socket.getOutputStream();
os.write(cmd.getBytes());
} catch (Exception e) {
Log.v("发送事件","发送异常");
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
class ReceiveThread extends Thread{
public void run(){
if(socket != null){
try{
while(true){
final byte[] buffer = new byte[1024];
InputStream inputStream = socket.getInputStream();
final int len = inputStream.read(buffer);
if(len == -1){
socket.close();
socket=null;
connected = false;
runOnUiThread(new Runnable() {
@Override
public void run() {
bt1.setText("connect");
}
});
return;
}
final String viewdata = new String(buffer,"UTF-8");
runOnUiThread(new Runnable() {
@Override
public void run() {
receive.append("Receive : "+viewdata+"\n");
int offset=receive.getLineCount()*receive.getLineHeight(); //滚动显示
if(offset>receive.getHeight()){
receive.scrollTo(0,offset-receive.getHeight());
}
}
});
}
}catch (IOException e){
e.printStackTrace();
}
}
}
}
}
注意: 因为我们用到了网络,所以我们要在 AndroidManifest.xml 中加入以下代码
<uses-permission android:name="android.permission.INTERNET" />
如图:
三、改APP图标、名字
这个其实很简单,也是在 AndroidManifest.xml 中更改,把图标放到相应的文件中,这里我是放在mipmap里
如图:
四、工程文件
最后把工程文件分享出来,需要的自取
链接:https://pan.baidu.com/s/13Ib1N6TLxxbkCHfAwM2cbg
提取码:gs8x
好的,本次记录就到这里了