jamesmontemagno / InAppBillingPlugin

Cross-platform In App Billing Plugin for .NET
MIT License
651 stars 152 forks source link

Android - billing.ConnectAsync() hangs with no response #531

Closed hammy33897 closed 1 year ago

hammy33897 commented 1 year ago

Bug Information

Version Number of Plugin: 5.6.1 Device Tested On: Samsung Galaxy S11, TCL A509DL Simulator Tested On: N/A - Instructions say needs to be tested on actual device Version of VS: 2022 Version of Xamarin: Forms 5.0.0.2578, Essentials 1.7.5 Versions of other things you are using: NetStandard.Library 2.0.3, System.Collections 4.3.0

Steps to reproduce the Behavior

Implemented plug-in as directed. Debug on actual android device. Targeting Android v10+ Verified mainactivity.cs includes xamarin essentials

Expected Behavior

Some response from billing.ConnectAsync();

Actual Behavior

code hangs on line "var connected = await billing.ConnectAsync();". It never gets to line "if (!connected)" App shows message "wait/close"

Code snippet

       public async Task<string> PurchaseItem(string productId)
    {
        if (!CrossInAppBilling.IsSupported)
            return "Not Supported";

        // check internet first with Essentials
        if (Connectivity.NetworkAccess != NetworkAccess.Internet)
            return "Error: No Internet Connection, pleease try again later";

        string message = "";
        var billing = CrossInAppBilling.Current;
       // --try
       // --{
            var connected = await billing.ConnectAsync();
            if (!connected)
jamesmontemagno commented 1 year ago

How are you calling PurchaseItem? Look at https://github.com/jamesmontemagno/MyStreamTimer/blob/main/MyStreamTimer.Shared/ViewModel/ProViewModel.cs asn example.

hammy33897 commented 1 year ago

Hi James, sorry this was my bad, the calling function was trying to use .Result() and therefore was blocking the thread. once I changed the calling function to be async then it started working just fine.

Original Bad Code: public void Subscribe() { string subscribed; subscribed = PurchaseItem("twiceby.subscription.1").Result;

Corrected Code: public async void Subscribe() { string subscribed; subscribed = await PurchaseItem("oofity.subscription.1");