简体   繁体   中英

How to upgrade javascript to utilize phonegap 2.3 custom plugin?

I have built a custom plugin called TCPIPCommPlugin that uses Phonegap/Cordova 1.6.1. Everything is all great and the callbacks/plugin results work just fine. However I need to get it upgraded to Cordova 2.3. The reason is that we are now starting dev on Win8 as well as iOS/Android.

That aside, I have the following code in javascript.

var TCPComm = function() {
};

TCPComm.prototype.Open = function(Cmd,successCallback, failureCallback) {
 return PhoneGap.exec(    successCallback,    //Success callback from the plugin
                          failureCallback,     //Error callback from the plugin
                          'TCPIPCommPlugin',  //Tell PhoneGap to run "DirectoryListingPlugin" Plugin
                          'Open',              //Tell plugin, which action we want to perform
                          [Cmd]);        //Passing list of args to the plugin
};

This code continues with about 10-12 different function calls to the plugin, which is then concluded with...

PhoneGap.addConstructor(function() {
    PhoneGap.addPlugin("TCPComm", new TCPComm());
});

Within the javascript itself, the actual function call looks like this.

window.plugins.TCPComm.Open(g_IpAddr, OpenOK,OpenFail);

Additionally here is what the JAVA Plugin looks like.

@Override
    public PluginResult execute(String action, JSONArray data, String callbackId) {
           PluginResult result = null;
        try {
            Actions currentAction = Actions.valueOf(action.toUpperCase());
            JSONObject Resp = new JSONObject();
            String RespStr;
            switch(currentAction){
            case OPEN:
            {
                       //do work
                    }catch (JSONException jsonEx) { 
        System.out.println(jsonEx.toString());
        result = new PluginResult(Status.JSON_EXCEPTION);
    }
    return result;}

This works great with Cordova 1.6.1. However not so much with Cordova 2.xx Now with all this said and done, I have perused the web trying to find a way to convert the JAVA. I came up with the following.

public boolean execute(String action, JSONArray data, CallbackContext callbackContext) {

    PluginResult result = null;
    try {
        Actions currentAction = Actions.valueOf(action.toUpperCase());
        JSONObject Resp = new JSONObject();
        String RespStr;
        switch(currentAction){
        case OPEN:
        {
                       //do work
                     }catch (JSONException jsonEx) {    
        System.out.println(jsonEx.toString());
        result = new PluginResult(Status.JSON_EXCEPTION);
    }
    return true;
}

This seems to match the updated code. What I have not been able to find is a way to update the JAVASCRIPT calls to make this plugin work with the updated CORDOVA.

Any help/points in the right direction would be much appreciated!

I have used the following documentation with no success. http://docs.phonegap.com/en/2.3.0/guide_plugin-development_index.md.html#Plugin%20Development%20Guide

https://github.com/apache/cordova-android/tree/master/framework/src/org/apache/cordova

UPDATE

Thanks for the response Simon. In the interim I had replaced my javascript with the following. Do I need to revert it back to what it was prior?

cordova.define("cordova/plugin/TCPIPCommPlugin", function(require, exports, module){
    var exec = require('cordova/exec');
    var TCPComm = function() {};
//  var TCPCommError = function(code, message) {
//        this.code = code || null;
//        this.message = message || '';
//    };

    TCPComm.prototype.Open = function(success,fail) {
          return cordova.exec(              successCallback,    //Success callback from the plugin
                                  failureCallback,     //Error callback from the plugin
                                  'TCPIPCommPlugin',  //Tell PhoneGap to run "DirectoryListingPlugin" Plugin
                                  'Open',              //Tell plugin, which action we want to perform
                                  [Cmd]);
    };
    var TCPIPCommPlugin = new TCPComm();
    module.exports = TCPIPCommPlugin;
});

UPDATE #2 - Fixed some errors

I went back to the old javascript and replaced it all so it looks like this now..

var TCPComm = function() {}; 


TCPComm.prototype.Open = function(Cmd, successCallback,failureCallback) {
      return cordova.exec(              successCallback,    //Success callback from the plugin
                              failureCallback,     //Error callback from the plugin
                              'TCPIPCommPlugin',  //Tell PhoneGap to run "DirectoryListingPlugin" Plugin
                              'Open',              //Tell plugin, which action we want to perform
                              [Cmd]);
};

I also replaced the constructor with..

    if(!window.plugins) {
    window.plugins = {};
}
if (!window.plugins.TCPComm) {
    window.plugins.TCPComm = new TCPComm();
}

Now when I run it in Chrome, (UI debugging), I can see the plugin built with all the proper functions inside the function.

The Java was fine, I forgot to include the return on the forum code. Whoops.

Now I tried calling the function like I always have and I get a Object # has no method 'exec' at the first JAVASCRIPT call of either Close/Open..

    window.plugins.TCPComm.Close("", Dummy, Dummy);
window.plugins.TCPComm.Open(g_IpAddr, OpenOK,OpenFail);

I have breakpoints set in the Java on the execute to let me know when the plugin has called successfully to the Java and still no luck.

Any other thoughts? Thanks again for your insight.

Replace PhoneGap.exec with the following code:

var TCPComm = function() {
}; 

TCPComm.prototype.Open = function(Cmd,successCallback, failureCallback) {
 return cordova.exec(    successCallback,    //Success callback from the plugin
                      failureCallback,     //Error callback from the plugin
                      'TCPIPCommPlugin',  //Tell PhoneGap to run "DirectoryListingPlugin" Plugin
                      'Open',              //Tell plugin, which action we want to perform
                      [Cmd]);        //Passing list of args to the plugin
};

The add constructor method is deprecated so do:

if(!window.plugins) {
    window.plugins = {};
}
if (!window.plugins.videoPlayer) {
    window.plugins.TCPComm = new TCPComm();
}

For your Java code you are most of the way there but you need to return the result:

public boolean execute(String action, JSONArray data, CallbackContext callbackContext) {

    PluginResult result = null;
    try {
        Actions currentAction = Actions.valueOf(action.toUpperCase());
        JSONObject Resp = new JSONObject();
        String RespStr;
        switch(currentAction){
        case OPEN:
            //do work
            this.callbackContext.sendPluginResult(
                new PluginResult(PluginResult.Status.OK, results));
    } catch (JSONException jsonEx) {    
        System.out.println(jsonEx.toString());
        result = new PluginResult(Status.JSON_EXCEPTION);
    }
    return true;
}

That should get you upgraded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM