cloudManager.getLocation doesn't work on Android - unable to get location from cloud

#1

Using Android indoor sdk i’m trying to locate a user in a room.
When the app starts it crashes because of “locazione” that is null.
I put some logs in the getLocation function and I don’t see them so, obviously, locazione is null.
Some ideas of what’s wrong?

package com.example.marco.open;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.estimote.coresdk.common.config.Flags;
import com.estimote.coresdk.common.requirements.SystemRequirementsChecker;
import com.estimote.coresdk.observation.region.beacon.BeaconRegion;
import com.estimote.coresdk.recognition.packets.Beacon;
import com.estimote.coresdk.service.BeaconManager;
import com.estimote.indoorsdk.EstimoteCloudCredentials;
import com.estimote.indoorsdk.IndoorLocationManagerBuilder;
import com.estimote.indoorsdk_module.algorithm.OnPositionUpdateListener;
import com.estimote.indoorsdk_module.algorithm.ScanningIndoorLocationManager;
import com.estimote.indoorsdk_module.cloud.CloudCallback;
import com.estimote.indoorsdk_module.cloud.EstimoteCloudException;
import com.estimote.indoorsdk_module.cloud.IndoorCloudManager;
import com.estimote.indoorsdk_module.cloud.IndoorCloudManagerFactory;
import com.estimote.indoorsdk_module.cloud.Location;
import com.estimote.indoorsdk_module.cloud.LocationBeacon;
import com.estimote.indoorsdk_module.cloud.LocationLinearObject;
import com.estimote.indoorsdk_module.cloud.LocationPosition;
import com.estimote.indoorsdk_module.cloud.LocationWall;
import com.example.marco.open.R;

import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

import static android.view.View.GONE;
import static com.estimote.coresdk.observation.region.RegionUtils.computeAccuracy;
import static java.util.Collections.emptyList;

public class LocationActivity extends AppCompatActivity {

    private BeaconManager beaconManager;
    private BeaconRegion regione;
    private Context CONTESTO = this;
    private List<Coordinate> coordinate;
    private int altezza;
    private int larghezza;
    private int contatore;
    private double coefficiente;
    private boolean flag = true;
    private double DISTANZA_MISURAZIONI = 200;
    private int NUMERO_MISURAZIONI = 10;
    private Coordinate beaconMisurazioni;
    private boolean f2;
    private int d[];

    private WebView web1;
    private WebView web2;
    private WebView v;

    private Toolbar toolbar;
    private Button b1;
    private TextView crediti;

    private int xCookie;
    private int yCookie;
    private ParametriTrilaterazione parametriTrilaterazione;
    private boolean info;
    private boolean tele;
    private boolean obp;

    //---------------------------------------------------------------------
    private IndoorCloudManager cloudManager;
    public Location locazione;
    ScanningIndoorLocationManager indoorLocationManager;
    //---------------------------------------------------------------------

    private void informatica() {
        tele = true;
        info = false;
        obp = true;

        v.setVisibility(View.GONE);
        web2.setVisibility(View.GONE);
        web1.setVisibility(View.VISIBLE);


    }

    private void telecomunicazioni() {
        info = true;
        tele = false;
        obp = true;


        v.setVisibility(View.GONE);
        web1.setVisibility(View.GONE);
        web2.setVisibility(View.VISIBLE);


    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);
        xCookie = 100;
        yCookie = 100;
        parametriTrilaterazione = new ParametriTrilaterazione();
        cloudManager = new IndoorCloudManagerFactory().create(getApplicationContext(), new EstimoteCloudCredentials("indoor-location-2jh", "01ceed5427c92df4d956c973adb70e1f"));
        /*List<LocationWall> locationWall=new LinkedList<>();
        locationWall(0,0, 0, 10.68, )
        locazione=new Location("amministrazione's location", "amministrazione-s-location-6pi", false, "amministrazione@softeam.it", 85.9398572994113, locationWall, locationBeacon, locationLinearObject new LinkedList<LocationWall>(), new LinkedList<LocationBeacon>(), new LinkedList<LocationLinearObject>());
       */ b1 = (Button) findViewById(R.id.button);
        //      toolbar= (Toolbar) findViewById(R.id.toolbar);
        crediti = (TextView) findViewById(R.id.crediti);

        b1.setVisibility(GONE);
        crediti.setVisibility(GONE);
//        toolbar.setVisibility(View.GONE);

        (findViewById(R.id.webview3)).setVisibility(View.GONE);

        web1 = (WebView) findViewById(R.id.webview1);
        web1.setWebViewClient(new WebViewClient());
        web1.loadUrl("https://www.ilmeteo.it/");
        web1.setVisibility(GONE);


        web2 = findViewById(R.id.webview2);
        web2.setWebViewClient(new WebViewClient());
        web1.loadUrl("https://www.ilmeteo.it/");
        web2.setVisibility(GONE);

        tele = true;
        info = true;
        v = (WebView) findViewById(R.id.webview4);
        v.setWebViewClient(new WebViewClient());//Per farlo come WebView e non come browser

        CookieSyncManager.createInstance(CONTESTO);
        CookieSyncManager.getInstance().startSync();
        CookieManager.getInstance().setAcceptCookie(true);
        v.getSettings().setJavaScriptEnabled(true);
        // setContentView(v);

        WebSettings webSettings = v.getSettings();
        webSettings.setJavaScriptEnabled(true);

        webSettings.setBuiltInZoomControls(true);
        webSettings.setSupportZoom(true);
        v.getSettings().setDisplayZoomControls(false);

        //------------------------------------------------------------------
        cloudManager.getLocation("amministrazione-s-location-6pi", new CloudCallback<Location>() {
            @Override
            public void success(Location location) {
                Log.d("TAG", "mylog success");
                locazione = location;
            }

            @Override
            public void failure(EstimoteCloudException e) {
                //ops!
                Log.d("TAG", "mylog failure");
            }
        });
        Log.d("TAG", " mylog "+locazione);
            indoorLocationManager = new IndoorLocationManagerBuilder(getApplicationContext(), locazione, new EstimoteCloudCredentials("indoor-location-2jh", "01ceed5427c92df4d956c973adb70e1f")).withDefaultScanner().build();
            indoorLocationManager.setOnPositionUpdateListener(new OnPositionUpdateListener() {
                @Override
                public void onPositionUpdate(LocationPosition locationPosition) {
                    xCookie = (int) locationPosition.getX() * 100;
                    yCookie = (int) locationPosition.getY() * 100;
                    v.addJavascriptInterface(parametriTrilaterazione, "Android");
                    v.loadUrl("file:///android_asset/index.html");
                    parametriTrilaterazione.setxCookie(xCookie);
                    parametriTrilaterazione.setyCookie(yCookie);
                    v.setVisibility(View.VISIBLE);
                    v.reload();
                }

                @Override
                public void onPositionOutsideLocation() {
                    xCookie = 100;
                    yCookie = 100;
                }
            });
        }
            //------------------------------------------------------------------

    protected void onResume() {
        super.onResume();
        SystemRequirementsChecker.checkWithDefaultDialogs(this);
    }

    public Coordinate cercaBeacon(Beacon b) {
        Coordinate tmp = null;
        for (int i = 0; i < coordinate.size(); i++) {
            Coordinate c = coordinate.get(i);
            if (c.getUuid().equals(b.getProximityUUID().toString()) && c.getMajor() == b.getMajor() && c.getMinor() == b.getMinor())
                tmp = coordinate.get(i);
        }
        return tmp;
    }

    @Override
    public void onBackPressed() {
        if (obp) {
            v.setVisibility(View.VISIBLE);
            web1.setVisibility(GONE);
            web2.setVisibility(GONE);


            obp = false;
            return;
        }


        v.setVisibility(GONE);
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        this.finish();

    }

    public void setXCookie(int x) {
        this.xCookie = x;
    }

    public void setYCookie(int y) {
        this.yCookie = y;
    }

    public int getXCookie() {
        return xCookie;
    }

    public int getYCookie() {
        return yCookie;
    }

    @Override
    protected void onStart() {
        super.onStart();
            indoorLocationManager.startPositioning();
    }

    @Override
    protected void onStop()
    {
        super.onStop();
        indoorLocationManager.stopPositioning();
    }
}
#2

Hello Marco,

The problem here is related to the location creation process. You should not manually create location in the code. like below:

locazione=new Location("amministrazione's location", "amministrazione-s-location-6pi", false, "amministrazione@softeam.it", 85.9398572994113, locationWall, locationBeacon, locationLinearObject new LinkedList<LocationWall>(), new LinkedList<LocationBeacon>(), new LinkedList<LocationLinearObject>());

Location can be created manually with Location Builder engine on iOS platform or automatically using auto-mapping mechanism available in Indoor Location AppStore app. Created Location can be fetch to the application using following code:

val cloudManager = IndoorCloudManagerFactory().create(applicationContext, cloudCredentials)
        
cloudManager.getLocation("your location id here", object : CloudCallback<Location> {
    override fun success(locations: Location?) {
    // do something with your location here. You will need it to init IndoorManager and IndoorView           
}

Hope it helps!
Marcin

#3

This is a commemment (the comment starts a few lines upper), i tried to do it insetad of downloading info from the cloud but it also didn’t work.

Anyway I solved, the problem was that the getLocation method is asynchronous so i have to wait for a while before start using the location.
The code was executed too fast and the lcation was null when accessed.

Thanks
Marco