Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot reconnect after disconnect on Android #141

Open
v3vinvin opened this issue Feb 25, 2019 · 10 comments
Open

Cannot reconnect after disconnect on Android #141

v3vinvin opened this issue Feb 25, 2019 · 10 comments
Labels
help wanted I am not sure I can solve this, help needed!

Comments

@v3vinvin
Copy link

Good day to you Dario,

I'm quite confused on this matter because whenever I've successfully used "health.disconnect()" a account. I can't seem to get data on 2nd attempt on "health.requestAuthorization()" after selecting an account. I'll provide you a simple step by step scenario.

  1. Selecting a Google Account. (health.requestAuthorization())

  2. Grabbing data from the Google Account. (health.query())

  3. Disconnecting from the Google Account. (health.disconnect())

  4. Selecting a Google Account again. (health.requestAuthorization()).

  5. Problem Occurs

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == REQUEST_OAUTH) {
            Log.d(TAG, requestCode + "requestCode == REQUEST_OAUTH ->" + resultCode);
            if (resultCode == Activity.RESULT_OK) {
                Log.i(TAG, "Got authorisation from Google Fit");
                if (!mClient.isConnected() && !mClient.isConnecting()) {
                    Log.d(TAG, "Re-trying connection with Fit");
                    mClient.connect();
                    // the connection success / failure will be taken care of by ConnectionCallbacks in checkAuthorization()
                }
            } else if (resultCode == Activity.RESULT_CANCELED) {
                // The user cancelled the login dialog before selecting any action.
                authReqCallbackCtx.error("User cancelled the dialog");
            } else authReqCallbackCtx.error("Authorisation failed, result code " + resultCode);
        } 
        **else {
            Log.d(TAG, requestCode + "onActivityResult ->" + resultCode);
            Log.d(TAG, "Re-trying connection with Fit in onActivityResult <><><> " + cordova.getActivity());
            mClient.reconnect();
        }**
    }

On the 2nd attempt, the onActivityResult returns requestCode of "0" and resultCode is "0". I noticed it's supposed to return requestCode of "1" and resultCode is "-1".

@ghost
Copy link

ghost commented Feb 25, 2019

the mysterious Google APIs....

I have no idea why you have this issue, maybe try giving it some time between disconnect and reconnect.

@v3vinvin
Copy link
Author

the mysterious Google APIs....

I have no idea why you have this issue, maybe try giving it some time between disconnect and reconnect.

I did gave it a moment between health.requestAuthorization() and health.disconnect().

I/chromium: [INFO:CONSOLE(236)] "null", source: file:///android_asset/www/build/48.js (236) I/chromium: [INFO:CONSOLE(236)] "[object Object]", source: file:///android_asset/www/build/48.js (236) I/chromium: [INFO:CONSOLE(1453)] "customIsAvailable res", source: file:///android_asset/www/build/main.js (1453) I/cordova-plugin-health: Connection to Fit failed, cause: 4 D/cordova-plugin-health: Connection to Fit failed, probably because of authorization, giving up now I/chromium: [INFO:CONSOLE(1475)] "isAuthorized res", source: file:///android_asset/www/build/main.js (1475) I/cordova-plugin-health: Connection to Fit failed, cause: 4 Attempting to resolve failed connection I/chromium: [INFO:CONSOLE(12227)] "pause", source: file:///android_asset/www/build/main.js (12227) D/cordova-plugin-health: 1requestCode == REQUEST_OAUTH ->-1 I/cordova-plugin-health: Got authorisation from Google Fit D/cordova-plugin-health: Re-trying connection with Fit I/cordova-plugin-health: Connection to Fit failed, cause: 4 Attempting to resolve failed connection I/chromium: [INFO:CONSOLE(12227)] "pause", source: file:///android_asset/www/build/main.js (12227)

This is log shown when I tried to connect the 2nd time.

@v3vinvin
Copy link
Author

The Request Authorization would bring up the following 2 popup right? Does this 2 popup supposed to be completed in one flow?
1

2

Because after disconnect, and reconnect. The 2nd Image does not have a callback.

@v3vinvin
Copy link
Author

!!Update!!

Is it supposed to prompt user for OAuth "Allow" or "Deny" every time when I've disconnect and reconnecting? "Application needs OAuth consent from the user".

@ghost
Copy link

ghost commented Feb 26, 2019

the process is simple: once you've disconnected, you need to re-connect, exactly as the first time you connect. So, the user needs to select the account he wants to use and then allow the app to access the data.

I have had a quick look at the code (see this part) and I can't see anything wrong with it.

@ghost ghost changed the title onActivityResult issue Cannot reconnect after disconnect on Android Feb 26, 2019
@ghost ghost added the help wanted I am not sure I can solve this, help needed! label Feb 26, 2019
@v3vinvin
Copy link
Author

v3vinvin commented Feb 26, 2019

Hmm... once I've disconnected. I need to use health.requestAuthorization(dataType) right? After selecting an account. There would be a popup asking about the Deny Allow oAuth right? that oAuth popup has no callback. -Edit- On a fresh install, it has no problem. But after a second attempt, the process in-between the "Google Account Selection" and "oAuth Popup". these two process it's in "health.requestAuthorization(dataType)" right? the "oAuth Popup" does not return a callback on any selection that I've made.

@ghost
Copy link

ghost commented Feb 26, 2019

the answer is "yes" to all your "right?".
I have checked the code, I can't see anything that would make the app "lose" the callback.

When you call disconnect() it disconnects the Google API client from the APIs:

mClient.disconnect();

and when you request authorization again, it rebuilds the client from the scratch:

mClient = builder.build();
mClient.connect();

One reason could be that you are running the re-connection from a different activity, but that's a very uncommon case for a cordova app.

I'm afraid that this needs proper debugging, with Android Studio. I am not able to work on this now, and won't be able for about a month. If you can do it and you find the solution, please send a pull request and I will be happy to merge it.

If you're not familiar with Android programming, I would suggest you:

  1. always debug in an empty cordova app and run the commands from the command line. No Ionic, no fancy stuff. This is to understand if you need to blame the plugin, or the way you are using the plugin.
  2. have logcat running on a console and see all messages produced by your app and the plugin, you may find interesting.

@v3vinvin
Copy link
Author

I've tried to debug it with Android Studio with a clean new ionic project with using the plugin only.
on the requestAuthorization(dataType), on second attempt, the log shown in Android Studio is as the following result.

D/SystemWebChromeClient: http://localhost/build/main.js: Line 74 : true
I/chromium: [INFO:CONSOLE(74)] "true", source: http://localhost/build/main.js (74)
W/CordovaPlugin: Attempted to send a second callback for ID: health931996332
    Result was: false
D/SystemWebChromeClient: http://localhost/build/main.js: Line 106 : customIsAvailable res
I/chromium: [INFO:CONSOLE(106)] "customIsAvailable res", source: http://localhost/build/main.js (106)
I/cordova-plugin-health: Connection to Fit failed, cause: null
    Attempting to resolve failed connection
I/cordova-plugin-health: Attemped to resolvecom.bundleId.MainActivity@f3751b4
D/CordovaActivity: Paused the activity.
D/CordovaActivity: Incoming Result. Request code = 1
D/CordovaInterfaceImpl: Sending activity result to plugin
I/cordova-plugin-health: Got authorisation from Google Fit
D/cordova-plugin-health: Re-trying connection with Fit
D/CordovaActivity: Resumed the activity.
I/cordova-plugin-health: Connection to Fit failed, cause: null
    Attempting to resolve failed connection
I/cordova-plugin-health: Attemped to resolvecom.bundleId.MainActivity@f3751b4
D/CordovaActivity: Paused the activity.
D/CordovaActivity: Incoming Result. Request code = 1
W/CordovaInterfaceImpl: Got an activity result, but no plugin was registered to receive it.
D/CordovaActivity: Resumed the activity.

@ghost
Copy link

ghost commented Feb 27, 2019

the key is clearly the message that says:

W/CordovaInterfaceImpl: Got an activity result, but no plugin was registered to receive it.

I'm puzzled because the request code 1, is actually taken care of by the overridden function onActivityResult(). It's exactly the same code that works when executing the requestAuthorization the first time.

I really don't know, I would like to test it myself, but I won't be able for a month or so.

@dariosalvi78
Copy link
Owner

has this problem been fixed? I don't think it's related to the plugin, but I'd be happy to be contradicted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted I am not sure I can solve this, help needed!
Projects
None yet
Development

No branches or pull requests

2 participants