[Android] socket connection and send button are separated

Keywords: Android socket network Java

Because of the need of the project, make an Android program, which can send instructions to control the printer, and use socket programming. Because I don't know about Android, I simply record some completed test applets

Click Connect to connect to the server

Click send to send helloWord to the server. The server uses the socket debugging tool to test

Common sense reminder
AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bm.myapplication2018_01_10">
    <!--Allow applications to change network state-->
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>

    <!--Allow application changes WIFI Connection status-->
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>

    <!--Allow applications to access relevant network information-->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <!--Allow application access WIFI Network information of network card-->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

    <!--Allow applications to fully use the network-->
    <uses-permission android:name="android.permission.INTERNET"/>

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

There are two buttons defined in MainActivity.java. The first button is used to connect and the second button is used to send. Both buttons need to be in the sub thread and can be copied directly. It is recommended to press Ctrl + F to view the socket and outputStream operations

package com.bm.myapplication2018_01_10;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    Button bt1,bt2;
    Socket socket;
    OutputStream outputStream;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initEvent();

    }

    private void initView(){
        bt1=findViewById(R.id.id_bt1);
        bt2=findViewById(R.id.id_bt2);
    }

    private void initEvent(){
        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);

    }

    private void initSocket(){

        //Connect socket
        try {
            socket=new Socket("10.3.0.87",9100);
            outputStream=socket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void initSendCommand(String command){

        //Send instruction
        byte[] command_byte=command.getBytes();
        try {
            outputStream.write(command_byte);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){

            case R.id.id_bt1:
                //Connect socket
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        initSocket();
                    }
                }).start();
                break;
            case R.id.id_bt2:
                //send data
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        initSendCommand("HelloWorld");
                    }
                }).start();
                break;

            default:
                break;

        }
    }
}

Posted by mikew2 on Tue, 05 May 2020 02:47:55 -0700