2 Replies Latest reply on Apr 1, 2016 11:27 AM by Davide_sd

    Cordova plugin for Windows Universal with repeated success callback

    Davide_sd

      I'm trying to add support to Windows Universal Platform for the following plugin (BluetoothSerial). This plugin reads the data sent by bluetooth devices over Serial Port. As you can see from this code, I'm very close to the goal, but seems like I'm not able to find a solution to my problem.

      Let me give you a basic understanding of what the plugin does: initially the app calls the connect method, so a writer and a reader are initialized, and the reading loop is launched. If the app calls the subscribe method, it means is expecting the plugin to send back data every time the specified delimiter character is found in the incoming data.

      At this moment the subscribeCallback is only executed the first time, but seems like it's disposed shortly after that. Looking at the Android/iOS code, it was possible to set the property setKeepCallbackAsBool or KeepCallback (or something similar) in the PluginResult object. Unfortunately I wasn't able to find something similar for Windows Universal. The only bit of information I was able to find came from this article:


      The Cordova exec proxy API is based on passing success and error callback functions as first and second parameter. On other platforms, one can choose to call the success callback several times, but on Windows, you can only call the success callback once. We worked around this by passing the callback function as an additional parameter and using that as the success callback in cases where we knew multiple calls is expected (like in case of event listeners).


      So far, though, I wasn0't able to implement this work around.

      Do you have any suggestions?

        • 1. Re: Cordova plugin for Windows Universal with repeated success callback
          kerrishotts Adobe Community Professional

          I notice that you have a console.log before calling the callback (line 60) -- are you receiving those messages? Are you getting any other error messages?

          • 2. Re: Cordova plugin for Windows Universal with repeated success callback
            Davide_sd Level 1

            kerrishotts, thank you for taking the time to look at this problem, I was able to make sense to the quote I posted in the first post.

             

            I write this in the hope it's going to be usefull to somebody else. By default Cordova don't allow to call the success callback more than once. On other platforms (Android, iOS), it is possible to force Cordova by setting to true the property setKeepCallbackAsBool (or KeepCallback) of the PluginResult object. When you develop a plugin for Windows Universal (using javascript), there is no PluginResult object. The workaround is to pass the callback as an additional parameter. Let me post a code to explain.


            This is a piece of code without the workaround of the file myPluginFolder/www/myPlugin.js

            subscribe: function (delimiter, success, failure) {

                cordova.exec(success, failure, "BluetoothSerial", "subscribe", [delimiter]);

            }

             

            And this is the related piece of code of the file myPluginFolder/src/windows/MyPlugin.js

            subscribe: function(success, failure, args) {

                delimiter = args[0];

                subscribeCallback = success;

            }

             

            In this case the subscribeCallback will be executed only once. Now let me post the workaround code:

            This is a piece of code of the file myPluginFolder/www/myPlugin.js

            subscribe: function (delimiter, success, failure) {

                cordova.exec(success, failure, "BluetoothSerial", "subscribe", [delimiter, success]);

            }

             

            Note how I placed the success callback also into the array arguments. And this is the related piece of code of the file myPluginFolder/src/windows/MyPlugin.js

            subscribe: function(success, failure, args) {

                delimiter = args[0];

                subscribeCallback = args[1];

            }

             

            With this workaround I can finally call the subscribeCallback as many times as I need.