Languages

Menu
Sites
Language
Receiving Bundle from Native App

 

I am sending bundle from native to web.
bundle *reply= bundle_create();
bundle_add(reply,"test","A");

 message_port_send_message(remote_app_id,remote_port,reply);
This send the bundle from native to web and i can receive it and show it in web app.
But when i am trying to send a string array as a bundle, i can not receive it from web app. but the nativa app log shows that the bundle is sent correctly.
this is the following code i have written:

char *array[]={"A","B","C","D"};
bundle *reply=bundle_create();
bundle_add_str_array(reply,"service",array, len(array));
what can i do??? 

View Selected Answer

Responses

4 Replies
Armaan-Ul- Islam

Hopefully, I understood what's happening here.
# If every thing is set correctly (Native port, Web Port, Web MessagePortListener) then:

For sample code similar to this:

int ret;
bundle *reply=bundle_create();
char *array = {"A","B","C","D"};
bundle_add(reply,"test","A");
bundle_add_str_array(reply,"service",array, len(array));
ret = message_port_send_message(remote_app_id, remote_port, reply);
if (ret != MESSAGE_PORT_ERROR_NONE)
    dlog_print(DLOG_ERROR, TAG, "message_port_check_remote_port error: %d", ret);
else
    dlog_print(DLOG_INFO, TAG, "Send message done");
bundle_free(reply);


The Structure of the message received on web end would be like :

var data =
[
   {key:"test", value:"A"},
   {key:"service", value:["A","B","C","D"]}
];

 

To use the received data Callback function can be similar to:

/* MessagePortCallback instance */
function onreceived(data, remoteMsgPort) {
    
    for (var i = 0; i < data.length; i++){
        var keyX = data[i].key;
        console.log("key:" + keyX);
      
        for (var j = 0; j < data[i].value.length; j++){
            var valueX = data[i].value[j];
            console.log("value:" + valueX);        
        }
   }
   
}

 

Edit the sample code as per your need and correct if necessary. Please 'Select' the answer to promote the response to other developers stuck at same scenerio. You may check these links for more details:

Message Port: Web Guide

Message Port: Web API Reference

Message Port: Native Guide

Message Port: Native API Reference

Native Bundle API

Armaan-Ul- Islam

Hello, What's current stat ? Are you able to successfully receive the bundle on web app now ?

Mahabub Zoraf
I have also tried in this similar way. But, in web app, i cannot receive. For a single element, i can send from native side and can view in web side. but sending an array stuck me on my way.
Mark as answer
Armaan-Ul- Islam

Okay, I am sharing my working code along with Screenshot. Directy use this code as template.

 

Native service C code:

#include <tizen.h>
#include <service_app.h>
#include <bundle.h>
#include <message_port.h>
#include "messageportnativeservicesend.h"

#define TAG "xyz"

void
sendMessage(char *remote_app_id,char *remote_port,bundle *reply){

    int ret = message_port_send_message(remote_app_id, remote_port, reply);

	if (ret != MESSAGE_PORT_ERROR_NONE)
	    dlog_print(DLOG_ERROR, TAG, "message_port_check_remote_port error: %d", ret);
	else
	    dlog_print(DLOG_INFO, TAG, "Send message done");

	dlog_print(DLOG_INFO, TAG, "Send message was called");
}

void
test_check_remote_port(char *remote_app_id,char *remote_port,bool result)
{
    int ret;
    ret = message_port_check_remote_port(remote_app_id,remote_port,&result);
    if (ret != MESSAGE_PORT_ERROR_NONE)
        dlog_print(DLOG_ERROR, TAG, "message_port_check_remote_port error: %d", ret);
}

void msgPort()
{
	char *remote_app_id="mRLmiKfKmV.MessagePortWebReceiver";
	char *remote_port="CrossPort";
	bool result;

	test_check_remote_port(remote_app_id,remote_port,result);
	dlog_print(DLOG_DEBUG, TAG ,"Remote port exists: %s", result ? "true" : "false");

	bundle *reply;
	reply=bundle_create();

	char *array[] = {"XY","YZ","ZX","AB"};

	bundle_add_str(reply,"test","A");
	bundle_add_str_array(reply,"service",array, 4);

	dlog_print(DLOG_DEBUG, TAG, "Bundled Successfully");


	if (result){
		sendMessage(remote_app_id,remote_port,reply);
		bundle_free(reply);
	}

}

bool service_app_create(void *data)
{
    msgPort();
    return true;
}

void service_app_terminate(void *data)
{
    // Todo: add your code here.
    return;
}

void service_app_control(app_control_h app_control, void *data)
{
    // Todo: add your code here.
    return;
}

static void
service_app_lang_changed(app_event_info_h event_info, void *user_data)
{
	/*APP_EVENT_LANGUAGE_CHANGED*/
	return;
}

static void
service_app_region_changed(app_event_info_h event_info, void *user_data)
{
	/*APP_EVENT_REGION_FORMAT_CHANGED*/
}

static void
service_app_low_battery(app_event_info_h event_info, void *user_data)
{
	/*APP_EVENT_LOW_BATTERY*/
}

static void
service_app_low_memory(app_event_info_h event_info, void *user_data)
{
	/*APP_EVENT_LOW_MEMORY*/
}

int main(int argc, char* argv[])
{
    char ad[50] = {0,};
	service_app_lifecycle_callback_s event_callback;
	app_event_handler_h handlers[5] = {NULL, };

	event_callback.create = service_app_create;
	event_callback.terminate = service_app_terminate;
	event_callback.app_control = service_app_control;

	service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
	service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
	service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
	service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);

	return service_app_main(argc, argv, &event_callback, ad);
}

 

Web app js Code:


window.onload = function () {
    // TODO:: Do your initialization job

    // add eventListener for tizenhwkey
    document.addEventListener('tizenhwkey', function(e) {
        if(e.keyName === "back") {
    		try {
			    tizen.application.getCurrentApplication().exit();
			} catch (ignore) {
			}
		}
	});
    
    

    /* MessagePortCallback instance */
    function onReceived(data, remoteMsgPort) {
    	console.log("Local port Listener Called");
    	
        for (var i = 0; i < data.length; i++){
            var keyX = data[i].key;
            console.log("key:" + keyX);

            for (var j = 0; j < data[i].value.length; j++){
                var valueX = data[i].value[j];
                console.log("value:" + valueX);        
            }
       }
             
    }
    
    function messagePort(){
    	 var localPort = tizen.messageport.requestLocalMessagePort("CrossPort");
    	 localPort.addMessagePortListener(onReceived);
    	 console.log("Local port Listener Registered");
    }
    
    // Sample code
    var textbox = document.querySelector('.contents');
    textbox.addEventListener("click", function(){
    	messagePort();
    	var box = document.querySelector('#textbox');
    	box.innerHTML = (box.innerHTML === "Basic") ? "Sample" : "Basic";
    });
    
};

Screenshot:

 

To run this code Structure :

Run>Debug as Web app (And Click on Screen)  to register local port > Deploy Service app on Device (as I have put my code on service_app_create) > Check Logs for Native Service (Tag > xyz) > Check console on Web app

You may change the program flow according to your need later. Please 'Select the Answer' to promote the response to other developers who are stuck at similar case. Thank you.