• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

I am looking for an Android Studio Client/Server USB Sample/Example Code

xarzu

Newbie
I am looking for an Android Studio Client/Server USB Sample/Example code. I have gotten a basic Java Based Android Studio App built successfully running through Android Studio and debugged by representing it on a physical Samsung Android device through a USB port. What I would like to do now is the following:

  1. Build a small Java example code to run separately on Ubuntu that is not necessarily build on Android Studio but some other development platform that can communicate through USB to software build on the first program mentioned here.
  2. Build an APK based on the code from the first project mentioned here and deploy it to an Android device.
  3. Test the communication between these two programs.
 
hi
Here's a simple example of how to set up a client-server communication between an Android device and a computer running Ubuntu using a USB connection:

  1. On the computer running Ubuntu, create a new Java project and add the following code to create a server that listens for incoming connections on a specific port:
Code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(8000);
            System.out.println("Server started on port 8000");
            Socket clientSocket = serverSocket.accept();
            System.out.println("Client connected");
            BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                System.out.println("Received message: " + inputLine);
                out.println("Server received message: " + inputLine);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

  1. On the Android device, create a new project in Android Studio and add the following code to create a client that connects to the server and sends a message:
Code:
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {
    private TextView mTextView;
    private EditText mEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = findViewById(R.id.text_view);
        mEditText = findViewById(R.id.edit_text);
    }

    public void sendMessage(View view) {
        String message = mEditText.getText().toString();
        new SendMessageTask().execute(message);
    }

    private class SendMessageTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... strings) {
            try {
                String message = strings[0];
                Socket socket = new Socket("192.168.42.129", 8000);
                PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                out.println(message);
                String response = in.readLine();
                return response;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
 
Back
Top Bottom