watson-developer-cloud / unity-sdk

:video_game: Unity SDK to use the IBM Watson services.
Apache License 2.0
569 stars 206 forks source link

[speech-to-text] Infinite Loop on Android when attempting to create credentials for Watson's Speech To Text. while (!credentials.HasIamTokenData()) #515

Closed Jonathanwam closed 5 years ago

Jonathanwam commented 5 years ago

I created a simple app to test IBM Watson's Speech To Text. My plan is to deploy an Android app using it. My app works when playing on Unity, but does not work after building and running on my Android device.

Through exploring Logcat on Android Studio, I found that it was hitting an infinite loop in the while loop mentioned in the title. My credentials part of my code is below as well.

Again, it works on my PC within Unity. Does not work after building and deploying on Android.

Also, it works fine when I build it on Windows. So I think it's an Android specific issue.

private IEnumerator CreateService()
     {
         if (string.IsNullOrEmpty(_iamApikey))
         {
             throw new WatsonException("Plesae provide IAM ApiKey for the service.");
         }
         //  Create credential and instantiate service
         Credentials credentials = null;
         //  Authenticate using iamApikey
         TokenOptions tokenOptions = new TokenOptions()
         {
             IamApiKey = _iamApikey
         };
         credentials = new Credentials(tokenOptions, _serviceUrl);
         if(credentials != null) Debug.Log("Credentials is not null!");
         //  Wait for tokendata
         while (!credentials.HasIamTokenData())
         {
             Debug.Log("In the while loop"); // INFINITE LOOP ON ANDROID
             yield return null;
         }
         Debug.Log("OUT of the while loop");
         _speechToText = new SpeechToText(credentials); // NEVER REACHES ON ANDROID
     }
mediumTaj commented 5 years ago

Sorry for the delay, let me take a look at this today - I had trouble getting hold of an android device

mediumTaj commented 5 years ago

What version of android are you publishing to?

mediumTaj commented 5 years ago

Sorry for the delay, I needed to set up my Unity Android environment. I have the ExampleStreaming scene working in Android with no issues. This also uses IAM authentication. Can you try this scene and see if it works for you?

mediumTaj commented 5 years ago

I also tried the AssistantV2 example in ServiceExamples scene with no issues. Are you using Android prior to 5.0? It looks like TLS 1.2 is not supported in android below 5

https://issuetracker.unity3d.com/issues/the-www-and-unitywebrequest-classes-currently-dont-support-https-tls-1-dot-2-on-android-versions-prior-to-5-dot-0

Jonathanwam commented 5 years ago

My Android version is 7.0. I tested the ServiceExamples scene on both ExampleAssistantV2 and ExampleSpeechToText. They work fine when running in Unity but I get the same loop problem after building and running on Android.

I'll update my phone to the latest version to see if that helps.

mediumTaj commented 5 years ago

Hmm I will try to update my device tomorrow as well. I was testing on Android 5

mediumTaj commented 5 years ago

I updated to Android 7.1.1 on my Samsung Galaxy Tab A. I can still connect to Watson Services. Are you still having issues getting IAM token?

mediumTaj commented 5 years ago

@Jonathanwam Any update on this issue?

Jonathanwam commented 5 years ago

Yes, same problem. I thought it could be my device but I just tested on an Android emulator and had the same issue. I'll try reinstalling Unity and the Watson SDK on my other laptop and see if that helps.

benhylak commented 5 years ago

Interesting -- I'm having the sample problem on my Magic Leap 1

mediumTaj commented 5 years ago

Any steps to reproduce? I haven’t been able to reproduce on Android. Does it happen only on magic leap device or on the simulator?

mamoonraja commented 5 years ago

@Jonathanwam @bhylak We have not been able to reproduce the issue, can you please provide the info @mediumTaj requested?

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has had no recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

lncslncs commented 4 years ago

Hi there, I am experiencing this same issue when building to Android. This is when trying to create an Assistant session, I get stuck in an infinite loop of "No Assistant Session created" (see below code, (while (!sessionCreated))). I am using Unity 2019.2.7f2 for Mac building to Android 8.0.0 on a Motorola G6. When I run in the editor on Mac it works perfectly but cannot get around this issue when building to Android.

if (string.IsNullOrEmpty(assistantIamApikey)) { throw new IBMException("Plesae provide IAM ApiKey for the Assistant service."); }

    IamAuthenticator assistantAuthenticator = new IamAuthenticator(apikey: assistantIamApikey);

    while (!assistantAuthenticator.CanAuthenticate()) {
        Debug.Log("Not yet authenticated Assitant");
            yield return null; }

    _assistant = new AssistantService("2019-02-08", assistantAuthenticator);
    if (!string.IsNullOrEmpty(AssistantURL))
        {
            _assistant.SetServiceUrl(AssistantURL);
        }

    _assistant.CreateSession(OnCreateSession, assistantId);
    Debug.Log("Created Assistant Instance");

    while (!sessionCreated) {
        yield return null;
        Debug.Log("No Assistant Instance Created"); // Here stuck in infinite loop

    }