How can I use Communications module to work with webservices in Garmin WatchFace App.

Former Member
Former Member

Hi everyone,

I have one requirement which needs to be app-type as WatchFace. In that app, I need to use the Communications module to use web services. I am a newbie to this technology. Please help me with it. In which I need to use clientId which I am not aware of. Please guide me how to get and use the clientId as well.
Thanks in advance.

  • When you have a watch face, the way you do comm is by way of a background process.  The watch face has a background process that does the comm, say every 10 minutes, which returns the data to the watch face.

    Here's a blog post I did a few years back with the basics of doing a background service:

    https://developer.garmin.com/index.php/blog/post/guest-post-creating-a-connect-iq-background-service

  • Former Member
    Former Member in reply to jim_m_58

    Okay. I will try your solution.

    Thank you for the quick reply. Will you please clear my doubt related to the ClientId, please.
    Thanks

  • I'm not sure what you mean with client id.  I'n many of mine I use an APIKEY, which I get from Application.properties in the background - the user sets it with app settings.

  • Former Member
    Former Member in reply to jim_m_58

    Okay,
    btw is the below code snippet will be enough for doing the web-service action?  

            // Make the request to display the OAuth page in GCM
            Communications.registerForOAuthMessages(method(:onOAuthComplete));
            Communications.makeOAuthRequest(Constants.PROVIDER_URL, 
            {"redirectUrl" => Constants.REDIRECT_URL}, 
            Constants.REDIRECT_URL, 
            Communications.OAUTH_RESULT_TYPE_URL, 
            {"token" => "token", "errorMessage" => "errorMessage"});
            
            // Callback for OAuth completion
            function onOAuthComplete(data) {
            // If OK, store the token. Otherwise show the error.
            if ((data.responseCode == 200) && (data.data != null)) {
                var token = data.data["token"];
                if (token != null) {
                    Application.getApp().setProperty(Properties.AUTHENTICATION_TOKEN, token);
                    WatchUi.popView(WatchUi.SLIDE_IMMEDIATE);
                }
            } else if (data.data != null) {
                var message = "Unable to get token:\n" + data.data["errorMessage"];
                WatchUi.switchToView(new ErrorView(message), null, WatchUi.SLIDE_IMMEDIATE);
            } else {
                var message = "Unable to get token:\nUnknown error";
                WatchUi.switchToView(new ErrorView(message), null, WatchUi.SLIDE_IMMEDIATE);
            }
    
            Communications.registerForOAuthMessages(null);
        }

  • Former Member
    Former Member in reply to jim_m_58

    for ClientId, please check the below snippet and URL from which I am taking reference for integration:

    reference link: https://developer.garmin.com/downloads/connect-iq/monkey-c/doc/Toybox/Communications.html#makeOAuthRequest-instance_method

    // wrap the OAuth request in a function
    function getOAuthToken() {
       status = "Look at OAuth screen\n";
       Ui.requestUpdate();
    
       // set the makeOAuthRequest parameters
       var params = {
           "scope" => Comm.encodeURL(https://www.serviceurl.com/),
           "redirect_uri" => "http://localhost",
           "response_type" => "code",
           "client_id" => "$.CLIENT_ID"
       };
    
       // makeOAuthRequest triggers login prompt on mobile device
       Comm.makeOAuthRequest(
           "https://requesturl.com",
           params,
           "http://resulturl.com",
           Comm.OAUTH_RESULT_TYPE_URL,
           {"responseCode" => "OAUTH_CODE", "responseError" => "OAUTH_ERROR"}
       );
    }

  • You should be able to do this, but all the comm has to be done in the background process.  You might want to look into onAppInstall() to get things started.

  • Former Member
    Former Member in reply to jim_m_58

    Thanks for the reply, Jim. I will try implementing now. Will ask for further queries to you.
    Thanks Again.