John Bristow John Bristow - 4 months ago 10
Android Question

receiving error when building an android project with Neura

I'm working with neura sdk in order to detect current data of a user(where he/she is, where were they 10 min ago etc).

I want to login to their api, and authenticate my user, however - when i call NeuraApiClient.authenticate(...) nothing happens.

I followed neura documentations, but still - nothing happens.

Here's my code :

public class MainActivity extends AppCompatActivity {

private ArrayList<Permission> mPermissions;
private AuthenticationRequest mAuthenticateRequest;

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

Builder builder = new Builder(this);
NeuraApiClient neuraApiClient = builder.build();
neuraApiClient.setAppUid(getResources().getString(R.string.app_uid));
neuraApiClient.setAppSecret(getResources().getString(R.string.app_secret));
neuraApiClient.connect();

fetchPermissions(neuraApiClient);
neuraApiClient.authenticate(100, mAuthenticateRequest);
}

private void fetchPermissions(final NeuraApiClient client) {
client.getAppPermissions(new GetPermissionsRequestCallbacks() {
@Override
public void onSuccess(final List<Permission> permissions) throws RemoteException {
runOnUiThread(new Runnable() {
@Override
public void run() {
mPermissions = new ArrayList<>(permissions);
mAuthenticateRequest = new AuthenticationRequest();
mAuthenticateRequest.setAppId(client.getAppUid());
mAuthenticateRequest.setAppSecret(client.getAppSecret());
mAuthenticateRequest.setPermissions(mPermissions);
}
});
}

@Override
public void onFailure(Bundle resultData, int errorCode) throws RemoteException {
}

@Override
public IBinder asBinder() {
return null;
}
});
}
}

Answer

getAppPermissions is an asynchronous call, and the data is fetched on GetPermissionsRequestCallbacks. in GetPermissionsRequestCallbacks you're initiating mAuthenticateRequest which is in use of authenticate method. Which means you have to wait untill onSuccess of GetPermissionsRequestCallbacks is called, and only then you can call

neuraApiClient.authenticate(100, mAuthenticateRequest);

Basically, if you don't wait for mAuthenticateRequest to be fetched, you authenticate with mAuthenticateRequest = null, and neuraApiClient.authenticate(..) fails.

You can do something like this : call authenticate when the results are received -

private void fetchPermissions(final NeuraApiClient client) {
        client.getAppPermissions(new GetPermissionsRequestCallbacks() {
            @Override
            public void onSuccess(final List<Permission> permissions) throws RemoteException {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mPermissions = new ArrayList<>(permissions);
                        mAuthenticateRequest = new AuthenticationRequest();
                        mAuthenticateRequest.setAppId(client.getAppUid());
                        mAuthenticateRequest.setAppSecret(client.getAppSecret());
                        mAuthenticateRequest.setPermissions(mPermissions);

                        client.authenticate(100, mAuthenticateRequest);
                    }
                });
            }
            ...
        });
    }