0

I am attempting to use the following plugin and am having a problem accessing the JAVA result in JS.

Below is a short version on the plugin that relates

Java:

public void sendCallback(String action, ServiceInfo info) {
    JSONObject status = new JSONObject();
    try {
        status.put("action", action);
        status.put("service", jsonifyService(info));
        Log.d("ZeroConf", "Sending result: " + status.toString());
        PluginResult result = new PluginResult(PluginResult.Status.OK,
                status);
        result.setKeepCallback(true);
        this.callback.sendPluginResult(result);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

JS

var ZeroConf = {
watch: function (type, callback) {
    return exec(function (result) {
        if (callback) {
            callback(result);
        }

    }, ZeroConf.fail, "ZeroConf", "watch", [type]);
}};module.exports = ZeroConf;

In my html file I am trying to show the result and nothing is working! I am not very familiar with JAVA so I am not sure where the source of my issue it.

Sample HTML File:

var app = {
// Application Constructor
initialize: function() {
    this.bindEvents();
},
bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function() {
    app.receivedEvent('deviceready');
    ZeroConf.watch('_http._tcp.local.', function(result) {

This is where I dont know how to access the result from JAVA

 alert(result)
       // do something with the result


    });
},
receivedEvent: function(id) {
    var parentElement = document.getElementById(id);
    var listeningElement = parentElement.querySelector('.listening');
    var receivedElement = parentElement.querySelector('.received');
    listeningElement.setAttribute('style', 'display:none;');
    receivedElement.setAttribute('style', 'display:block;');
    console.log('Received Event: ' + id);
}};

Notes: Im sure its working because I can see the results from the JAVA side "Log.d"

tyler
  • 1,273
  • 2
  • 16
  • 40

1 Answers1

0

You get an object as the result. You need to parse it or stringify it, depending on what you want to do with it.

onDeviceReady: function() {
    app.receivedEvent('deviceready');
    ZeroConf.watch('_http._tcp.local.', function(result) {
        // Stringify the result to display it all as a string
        resultText = JSON.stringify(result);
        alert(resultText);

        // just display some values from the object
        // for example PORT and NAME
        alert("port: "+result.service.port + ", name: "+result.service.name);

    });
},

The result object structure is described on the plugin's page.

benka
  • 4,732
  • 35
  • 47
  • 58
  • I am suspecting that there is a issue with my implementation of the Java and the plugin. The result is not returned and therefore I get a undefined when printing the value of the result. – tyler Oct 16 '14 at 22:31
  • I'm maintaining this plugin and I would like to have more info about your problem. Have you installed the app using cordova-cli? What Android version and device type are you using? – Vlad Stirbu Oct 28 '14 at 17:06