18

So I have a billing client which I instantiate with

billingClient = BillingClient.newBuilder(this).setListener(this).build();

I then call

billingClient.startConnection(new BillingClientStateListener() {
        @Override
        public void onBillingSetupFinished(int responseCode) {
            //TODO: use this for stuff
            com.android.billingclient.api.Purchase.PurchasesResult result;
result = billingClient.queryPurchases(BillingClient.SkuType.SUBS);
    Timber.d(result.toString());

        }

        @Override
        public void onBillingServiceDisconnected() {
            //TODO: use this for stuff
            Timber.d("something went wrong ");
        }
    });

for whatever reason the breakpoint on the timber line always returns disconnected. can anyone provide insight or an example to how i would do this?

1tSurge
  • 663
  • 2
  • 7
  • 20
  • 1
    i faced the same problem.how did you solve this? – metis May 22 '18 at 13:49
  • I'm running into this right now and it's driving me nuts. – Kris B May 27 '19 at 21:16
  • See if you BillingClient is actually ready, Please refer to my answer on the below thread https://stackoverflow.com/questions/56332090/testing-billing-with-static-products-returns-service-disconnected/63332353#63332353 – MG Developer Aug 10 '20 at 02:08

4 Answers4

9

I came across this problem. Be also sure to start the connection:

mBillingClient = BillingClient.newBuilder(mContext).setListener(purchasesUpdatedListener).build();

        mBillingClient.startConnection(new BillingClientStateListener() {
            @Override
            public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponseCode) {
                if (billingResponseCode == BillingClient.BillingResponse.OK) {

                    Log.d(TAG, "onBillingSetupFinished: BillingClient.BillingResponse.OK ");

                }


            }
            @Override
            public void onBillingServiceDisconnected() {
                // Try to restart the connection on the next request to
                // Google Play by calling the startConnection() method.
            }


        });
Paolo
  • 3,624
  • 2
  • 12
  • 13
  • 2
    At first I thought your comment wasn't helpful, however I have been able to check for myself that if you receive a SERVICE_DISCONNECTED response, then you need to call (again) to the startConnection() method. Thumbs up! – xarlymg89 Dec 23 '19 at 12:05
  • @xarlymg89 is enough to call the startConnection() in the onBillingServiceDisconnected() or do we have to call it also in the onBillingSetupFinished() when the billingResponseCode == SERVICE_DISCONNECTED? – Giovesoft Oct 24 '22 at 14:36
3

Turns out I was not using a version of my apk that was signed with the right version numbering and such. Once I fixed that I was able to connect to play services and figure out what i wanted.

1tSurge
  • 663
  • 2
  • 7
  • 20
  • 3
    How'd you fix that? – COYG Jun 19 '19 at 05:40
  • It was just ensuring that the versioning on the apk and play services was the same. I have various logging and what not for that in app, so just making sure that matched what the play console says – 1tSurge Feb 24 '20 at 21:40
  • @1tSurge you mean about the app versionNumber and versionCode? or maybe the billing dependency version added in the `build.gradle`? Otherwise, where do you check? – JCarlosR May 12 '20 at 01:00
  • 1
    I am also curious about this, I ran into the same issue and I have not been able to fix it. What do you mean by the apk and play services versioning being the same? – Noah-1 Jul 21 '20 at 03:50
1

I know it's too late to answer this, but i was missing this permission in manifest,

<uses-permission android:name="com.android.vending.BILLING" />

hope that helps someone

  • That's not required since the version 1.0 https://developer.android.com/google/play/billing/release-notes#release-1_0 – Mark Delphi Mar 31 '23 at 10:16
0

If you use custom rom or rooted device it probably won't work.

Run a system image on which the Google Play client application is preinstalled. If Google Play is not preinstalled in the system image, your application won't be able to communicate with the Google Play licensing server.

https://developer.android.com/google/play/licensing/setting-up

sucicf1
  • 335
  • 3
  • 6