How to login on Android ... Keep It Simple


#1

I write apps for a living, and the documentation has lots missing. I am just trying to write a simple app that logs in and lists devices. The documentation implies something that should never happen in Android dev, a network call on the main thread. Well it doesn’t work, and it is not because it is on their main thread. The firmware has lots of hello world like examples. But the Android is really missing lots of example code. here is my super simple code. Why doesn’t it work???

public class MainActivity extends AppCompatActivity {
    private TextView login_Button;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ParticleCloudSDK.init(this);
        login_Button = (TextView)findViewById(R.id.login_button);
        login_Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });
        login();
    }

    private void login () {
        Boolean loginSuccessful = false;
        try {
            ParticleCloudSDK.getCloud().logIn("xxxxx@gmail.com", "3xxxxx");
            loginSuccessful = true;
        } catch (ParticleCloudException e) {
            e.printStackTrace();
        }
        if (loginSuccessful == true){
            Log.d("debug", "login successful");
        } else {
            Log.d("debug", "login failed");
        }
    }
}

#2

@rw86347 In your login example code is running on main thread though.

I agree that more examples would help, but for now here is example with login.

Also it is documented that most method calls require background thread to work here.


#3

This is more like what I was expecting however this code looks to be out of date…

09-18 16:33:22.458 4019-4019/com.example.rodgerwilson.particleblinker W/ParticleCloud: ParticleCloud.get() is deprecated and will be removed before the 1.0 release. Use ParticleCloudSDK.getCloud() instead!
09-18 16:33:22.480 4019-4019/com.example.rodgerwilson.particleblinker E/Async: Error calling API: Unknown error communicating with server.
  io.particle.android.sdk.cloud.ParticleCloudException
    at io.particle.android.sdk.cloud.ParticleCloud.logIn(ParticleCloud.java:173)
    at com.example.rodgerwilson.particleblinker.MainActivity$2.callApi(MainActivity.java:74)
    at com.example.rodgerwilson.particleblinker.MainActivity$2.callApi(MainActivity.java:72)
    at io.particle.android.sdk.utils.Async$AsyncApiWorker.doInBackground(Async.java:105)
    at io.particle.android.sdk.utils.Async$AsyncApiWorker.doInBackground(Async.java:73)
    at android.os.AsyncTask$2.call(AsyncTask.java:305)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
    at java.lang.Thread.run(Thread.java:761)
09-18 16:33:22.484 4019-4019/com.example.rodgerwilson.particleblinker W/System.err: io.particle.android.sdk.cloud.ParticleCloudException
09-18 16:33:22.484 4019-4019/com.example.rodgerwilson.particleblinker W/System.err:     at io.particle.android.sdk.cloud.ParticleCloud.logIn(ParticleCloud.java:173)
09-18 16:33:22.484 4019-4019/com.example.rodgerwilson.particleblinker W/System.err:     at com.example.rodgerwilson.particleblinker.MainActivity$2.callApi(MainActivity.java:74)
09-18 16:33:22.484 4019-4019/com.example.rodgerwilson.particleblinker W/System.err:     at com.example.rodgerwilson.particleblinker.MainActivity$2.callApi(MainActivity.java:72)
09-18 16:33:22.484 4019-4019/com.example.rodgerwilson.particleblinker W/System.err:     at io.particle.android.sdk.utils.Async$AsyncApiWorker.doInBackground(Async.java:105)
09-18 16:33:22.484 4019-4019/com.example.rodgerwilson.particleblinker W/System.err:     at io.particle.android.sdk.utils.Async$AsyncApiWorker.doInBackground(Async.java:73)
09-18 16:33:22.484 4019-4019/com.example.rodgerwilson.particleblinker W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:305)
09-18 16:33:22.484 4019-4019/com.example.rodgerwilson.particleblinker W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-18 16:33:22.484 4019-4019/com.example.rodgerwilson.particleblinker W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
09-18 16:33:22.484 4019-4019/com.example.rodgerwilson.particleblinker W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
09-18 16:33:22.484 4019-4019/com.example.rodgerwilson.particleblinker W/System.err:     at java.lang.Thread.run(Thread.java:761)
09-18 16:33:22.485 4019-4019/com.example.rodgerwilson.particleblinker D/info: Unknown error communicating with server.

#4

I changed to the newer API and it still fails.

package com.example.rodgerwilson.particleblinker;

import android.content.Intent;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.rodgerwilson.particleblinker.RodgersHttpClient.ErrorResponse;
import com.example.rodgerwilson.particleblinker.RodgersHttpClient.GenericResponseListener;
import com.example.rodgerwilson.particleblinker.RodgersHttpClient.ParticleApis;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import io.particle.android.sdk.cloud.ParticleCloud;
import io.particle.android.sdk.cloud.ParticleCloudException;
import io.particle.android.sdk.cloud.ParticleCloudSDK;
import io.particle.android.sdk.cloud.ParticleDevice;
import io.particle.android.sdk.utils.Async;
import io.particle.android.sdk.utils.Toaster;

public class MainActivity extends AppCompatActivity {
    private TextView login_Button;

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

        findViewById(R.id.login_button).setOnClickListener(
                v -> {
                    final String email = ((EditText) findViewById(R.id.email)).getText().toString();
                    final String password = ((EditText) findViewById(R.id.password)).getText().toString();

                    // Don't:
                    AsyncTask task = new AsyncTask() {
                        @Override
                        protected Object doInBackground(Object[] params) {
                            try {
                                ParticleCloud.get(MainActivity.this).logIn(email, password);

                            } catch (final ParticleCloudException e) {
                                Runnable mainThread = () -> {
                                    Toaster.l(MainActivity.this, e.getBestMessage());
                                    e.printStackTrace();
                                    Log.d("info", e.getBestMessage());
//                                            Log.d("info", e.getCause().toString());
                                };
                                runOnUiThread(mainThread);

                            }

                            return null;
                        }

                    };
//                        task.execute();

                    //-------

                    // DO!:
                    Async.executeAsync(ParticleCloudSDK.getCloud(), new Async.ApiWork<ParticleCloud, Object>() {

                        private ParticleDevice mDevice;

                        @Override
                        public Object callApi(@NonNull ParticleCloud sparkCloud) throws ParticleCloudException, IOException {
                            sparkCloud.logIn(email, password);
                            sparkCloud.getDevices();
                            mDevice = sparkCloud.getDevice("1f0034000747343232361234");
                            Object obj;

                            try {
                                obj = mDevice.getVariable("analogvalue");
                                Log.d("BANANA", "analogvalue: " + obj);
                            } catch (ParticleDevice.VariableDoesNotExistException e) {
                                Toaster.s(MainActivity.this, "Error reading variable");
                                obj = -1;
                            }

                            try {
                                String strVariable = mDevice.getStringVariable("stringvalue");
                                Log.d("BANANA", "stringvalue: " + strVariable);
                            } catch (ParticleDevice.VariableDoesNotExistException e) {
                                Toaster.s(MainActivity.this, "Error reading variable");
                            }

                            try {
                                double dVariable = mDevice.getDoubleVariable("doublevalue");
                                Log.d("BANANA", "doublevalue: " + dVariable);
                            } catch (ParticleDevice.VariableDoesNotExistException e) {
                                Toaster.s(MainActivity.this, "Error reading variable");
                            }

                            try {
                                int intVariable = mDevice.getIntVariable("analogvalue");
                                Log.d("BANANA", "int analogvalue: " + intVariable);
                            } catch (ParticleDevice.VariableDoesNotExistException e) {
                                Toaster.s(MainActivity.this, "Error reading variable");
                            }

                            return -1;

                        }

                        @Override
                        public void onSuccess(@NonNull Object value) {
                            Toaster.l(MainActivity.this, "Logged in");
//                            Intent intent = ValueActivity.buildIntent(MainActivity.this, 123, mDevice.getID());
//                            startActivity(intent);
                        }

                        @Override
                        public void onFailure(@NonNull ParticleCloudException e) {
                            Toaster.l(MainActivity.this, e.getBestMessage());
                            e.printStackTrace();
                            Log.d("info", e.getBestMessage());
                        }
                    });
                }
        );
    }
}

#5

I am also looking for an answer on how to get it to login to my particle account. Any tips you’ve learned since your original post?


#6

I am having an issue in reading variable from my device after logging in.It is giving me error reading variable and “variable temp does not exist on device” however we have defined this variable as global in Web IDE of particle. Kindly suggest how we can get this variable on our Android App.
image


Issues in using Android SDK of photon
#7

Double posted here
Issues in using Android SDK of photon

Closing this thread - OP can ask via PM to any mod to reopen his own topic if needed


#8