How do I use Android to communicate with my Remote IO Module?

FAQs

Disclaimer: The app mentioned in this FAQ was developed for Android 4.3, is now deprecated, and is no longer available on the Google Play Store. If you wish to test this app, we can provide the APK, but please be aware that we are no longer developing it.

Brainboxes provides a JAR which allows easy integration of Brainboxes Remote IO modules into your Java code.

Requirements

Java Code Samples

Brainboxes.IO Console Application for AndroidAndroid Studio with the Brainboxes.IO API

Example Code

package com.brainboxes.edconsole2;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

    private EDDevice EDDevice;

    Button bConnect, bSend;
    EditText etIP1, etIP2, etIP3, etIP4, etPortNumber, etSendCommand;
    RelativeLayout rlIpAddress, rlPortNumber, rlSendCommand, rlConnect;
    TextView tvConsole;
    String ipAddress = "";
    int portNumber = 9500;

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

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public void InitializeUI() {
        rlIpAddress = (RelativeLayout) findViewById(R.id.rlIpAddress);
        rlPortNumber = (RelativeLayout) findViewById(R.id.rlPortNumber);
        rlConnect = (RelativeLayout) findViewById(R.id.rlConnect);
        rlSendCommand = (RelativeLayout) findViewById(R.id.rlSendCommand);
        rlSendCommand.setVisibility(View.GONE);
        bConnect = (Button) findViewById(R.id.bConnect);
        bSend = (Button) findViewById(R.id.bSend);
        etIP1 = (EditText) findViewById(R.id.etIP1);
        etIP2 = (EditText) findViewById(R.id.etIP2);
        etIP3 = (EditText) findViewById(R.id.etIP3);
        etIP4 = (EditText) findViewById(R.id.etIP4);
        etPortNumber = (EditText) findViewById(R.id.etPortNumber);
        etSendCommand = (EditText) findViewById(R.id.etSendCommand);
        etSendCommand.setAllCaps(true);
        tvConsole = (TextView) findViewById(R.id.tvConsole);
        tvConsole.append("nn");
        tvConsole.setMovementMethod(new ScrollingMovementMethod());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void ButtonOnClickListeners() {
        bConnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bConnect.getText().equals("Connect")) {
                    if(ConnectionBoxes()){
                        ipAddress = etIP1.getText() + "." + etIP2.getText() + "." + etIP3.getText() + "." + etIP4.getText();
                        portNumber = Integer.parseInt(etPortNumber.getText().toString());
                        EDDevice = new EDDeviceTCP(ipAddress, portNumber);
                        connect();
                    }
                }else{
                    disconnect();
                }
            }

        });
        bSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String command = (etSendCommand.getText().toString().toUpperCase()) + "r";
                String response;
                try {
                    response = EDDevice.SendCommand(command);
                    appendTextAndScroll("Sending:  " + command + "n");
                    appendTextAndScroll("Received: " + response + "nn");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void connect(){
        appendTextAndScroll("Connecting to " + ipAddress.toString() + "...n");
        try{
            System.out.println("Connecting...");
            EDDevice.Connect();
            System.out.println("Connected");
            appendTextAndScroll("Connected!nn");
            bConnect.setText("Disconnect");
            rlIpAddress.setVisibility(View.GONE);
            rlPortNumber.setVisibility(View.GONE);
            rlSendCommand.setVisibility(View.VISIBLE);
            bSend.setEnabled(true);
            etSendCommand.requestFocus();
        } catch (Exception e){
            System.out.println("Error Connecting: " + e.getMessage());
        }
    }

        public void disconnect(){
            EDDevice.Disconnect();
            bConnect.setText("Connect");
            appendTextAndScroll("Disconnectednn");
            rlIpAddress.setVisibility(View.VISIBLE);
            rlPortNumber.setVisibility(View.VISIBLE);
            rlSendCommand.setVisibility(View.GONE);
            bSend.setEnabled(false);
    }

    public void appendTextAndScroll(String text) {
        tvConsole.append(text);
        final int scrollAmount = tvConsole.getLayout().getLineTop(tvConsole.getLineCount()) - tvConsole.getHeight();
        if (scrollAmount [gt] 0)
            tvConsole.scrollTo(0, scrollAmount);
        else
            tvConsole.scrollTo(0, 0);
    }

    private void EditTextListeners() {
        etIP1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                if (etIP1.getText().toString().length() == 3) {
                    etIP2.requestFocus();
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
        etIP2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                if (etIP2.getText().toString().length() == 3) {
                    etIP3.requestFocus();
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
        etIP3.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                if (etIP3.getText().toString().length() == 3) {
                    etIP4.requestFocus();
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
        etIP4.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                if (etIP4.getText().toString().length() == 3) {
                    etPortNumber.requestFocus();
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
    }

    private boolean ConnectionBoxes() {
        boolean CanWeConnect = true;
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        DialogInterface.OnClickListener dialogInterface = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
            }
        };
        if (etIP1.getText().length() == 0 || etIP2.getText().length() == 0 || etIP3.getText().length() == 0 || etIP4.getText().length() == 0) {
            builder.setMessage("Please enter a valid IP address");
            builder.setTitle("Incorrect IP Address");
            builder.setPositiveButton("OK", dialogInterface);
            builder.show();
            CanWeConnect = false;
        } else if (etPortNumber.getText().length() == 0) {
            builder.setMessage("Please enter a valid port number");
            builder.setTitle("Incorrect Port Number");
            builder.setPositiveButton("OK", dialogInterface);
            builder.show();
            CanWeConnect = false;
        }
        return CanWeConnect;
    }

}

Reference

For a full list of ASCII protocol commands which can be sent to the device see: Brainboxes Remote IO ASCII protocol reference

FAQs