Jackson148
Jackson148

Reputation: 115

How to activate button before it is pressed?

I am making an app that connects to Amazon's AWS services. I have everything connecting properly, but I need to push a button before it connects. Is there a way to avoid this step and have it connect automatically to AWS?

Right now, the user has to press a button saying they want to connect, then press another saying they want to subscribe to a topic to receive updates. Being as the sole purpose of this app is to connect to AWS, I would like to remove the button presses as it simply wastes time.

This is the tutorial that I followed to set up the connection, in case it provides better information: https://www.linkedin.com/pulse/android-app-aws-iot-core-guide-felipe-ramos-da-silva

Otherwise, here is my code:

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

        //Sets up layout information
        txtSubscribe = (EditText) findViewById(R.id.txtSubscribe);
        tvClientId = (TextView) findViewById(R.id.tvClientId);
        tvStatus = (TextView) findViewById(R.id.tvStatus);

        tvSteamTemp = (TextView) findViewById(R.id.tvSteamTemp);
        tvWaterTemp = (TextView) findViewById(R.id.tvWaterTemp);
        tvWaterFlow = (TextView) findViewById(R.id.tvWaterFlow);
        tvDieselFlow = (TextView) findViewById(R.id.tvDieselFlow);
        tvManualResetLevel = (TextView) findViewById(R.id.tvManualResetLevel);
        tvWaterFeederLevel = (TextView) findViewById(R.id.tvWaterFeederLevel);
        tvAutoResetPressure = (TextView) findViewById(R.id.tvAutoResetPressure);
        tvManualResetPressure = (TextView) findViewById(R.id.tvManualResetPressure);
        tvTempLimit = (TextView) findViewById(R.id.tvTempLimit);

        btnConnect = (Button) findViewById(R.id.btnConnect);
        btnConnect.setOnClickListener(connectClick);
        btnConnect.setEnabled(false);

        btnSubscribe = (Button) findViewById(R.id.btnSubscribe);
        btnSubscribe.setOnClickListener(subscribeClick);

        btnDisconnect = (Button) findViewById(R.id.btnDisconnect);
        btnDisconnect.setOnClickListener(disconnectClick);


        /* MQTT client IDs are required to be unique per AWS IoT account.
         * This UUID is "practically unique" but does not _guarantee_
         * uniqueness.
         */

        clientId = UUID.randomUUID().toString();
        tvClientId.setText(clientId);

        // Initialize the AWS Cognito credentials provider
        // Sends info to AWS so it knows to what it needs to connect
        credentialsProvider = new CognitoCachingCredentialsProvider(
                getApplicationContext(), // context
                COGNITO_POOL_ID, // Identity Pool ID
                MY_REGION // Region
        );

        // MQTT Client
        /* Sets up the app side of being able to read and understand
         * information sent from AWS
         */
        mqttManager = new AWSIotMqttManager(clientId, CUSTOMER_SPECIFIC_ENDPOINT);

        // The following block uses a Cognito credentials provider for authentication with AWS IoT.
        new Thread(new Runnable() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        btnConnect.setEnabled(true);
                    }
                });
            }
        }).start();
    }

This is what happens when the connect button is clicked:

    View.OnClickListener connectClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Log.d(LOG_TAG, "clientId = " + clientId);

            try {
                mqttManager.connect(credentialsProvider, new AWSIotMqttClientStatusCallback() {
                    @Override
                    public void onStatusChanged(final AWSIotMqttClientStatus status, final Throwable throwable) {
                        Log.d(LOG_TAG, "Status = " + status);

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (status == AWSIotMqttClientStatus.Connecting) {
                                    tvStatus.setText("Connecting...");

                                } else if (status == AWSIotMqttClientStatus.Connected) {
                                    tvStatus.setText("Connected");

                                } else if (status == AWSIotMqttClientStatus.Reconnecting) {
                                    if (throwable != null) {
                                        Log.e(LOG_TAG, "Connection error.", throwable);
                                    }
                                    tvStatus.setText("Reconnecting");
                                } else if (status == AWSIotMqttClientStatus.ConnectionLost) {
                                    if (throwable != null) {
                                        Log.e(LOG_TAG, "Connection error.", throwable);
                                        throwable.printStackTrace();
                                    }
                                    tvStatus.setText("Disconnected");
                                } else {
                                    tvStatus.setText("Disconnected");

                                }
                            }
                        });
                    }
                });
            } catch (final Exception e) {
                Log.e(LOG_TAG, "Connection error.", e);
                tvStatus.setText("Error! " + e.getMessage());
            }
        }
    };

I am hoping to be able to completely remove the "connect" button, or, at the very least, have it be there as a "reconnect" button. This way when the app starts, it will already be connecting instead of waiting for user input.

Upvotes: 0

Views: 77

Answers (2)

Romadro
Romadro

Reputation: 626

add call btnConnect.performClick() after btnConnect.setEnabled(true);

I have no idea why you have to create new Thread in acitivity onCreate method and then use runOnUiHandle to run it on UI thread. onCreate method runs on UI thread by default

Upvotes: 1

Sid
Sid

Reputation: 573

Create a function with the contents of connectClick and call it in onCreate. Since you are not using the argument v in connectClick, this function does not need to have any arguments.

Upvotes: 0

Related Questions