语言

Menu
Sites
Language
[Wearable] Read/Write Widget Content
Hi, I have a wearable web app that has a widget and I want this widget to be customizable. I followed the following guide to read/write data per widget instance: https://developer.tizen.org/development/api-references/web-application?redirect=https://developer.tizen.org/dev-guide/2.3.2/org.tizen.web.apireference/html/device_api/wearable/tizen/widgetservice.html#WidgetInstance::sendContent
Unfortunately this does not work but I also do not get an error or anything. What I do in my prototype is the following:
1) retrieve all widgets and then all instances of them.
2) I then send some test data to this instances using sendContent -> which seems fine as I do not get any errors
3) After that I try to read the content from each instance using getContent -> I dont get an error but the returned object is empty and not what I've sent before
Note:
 - Yes I have set the required privileges, otherwise the code throws exceptions.
 - Yes there is a sample for that, but a) not for wearables and b) it does not work anyway.
With Tizen 2.3.1 for wearables there was already a bug where the widget instanced did not had different id's - Is this just the next bug (if so: who the f*ck is testing this code?!?=) or am I doing something wrong?
Here is my code:
contentSuccessCb = function(data) {
  console.log("Data has been obtained successfully");
  for (var prop in data) {
    alert(prop + " : " + data[prop]);
  }
 }
 contentErrorCb = function(error) {
  alert("Error occurred: " + error.name + ":" + error.message);
 }

function readData(){
 tizen.widgetservice.getWidgets(function(widgets)
 {
  widgets.forEach(function(widget){
   if(widget.applicationId == "s29U0dfg8I.WidgetSampleHost"){
    widget.getInstances(
    function(instances){
          instances.forEach(function(instance) {
        try {
          instance.getContent(contentSuccessCb, contentErrorCb);
        } catch (error) {
          alert("Error: " + error.message);
        }
        });
    }
    , function(error){alert("error while getting the instance: "+error);});
   }
  });
 }, function(error)
 {
    alert("Error: " + error.message);
 });
}
function writeData(){
 tizen.widgetservice.getWidgets(function(widgets)
 {
  widgets.forEach(function(widget){
   if(widget.applicationId == "s29U0dfg8I.WidgetSampleHost"){
    widget.getInstances(
    function(instances){
          instances.forEach(function(instance) {
            var data = {
          data1 : 'test1',
          data2 : 'test2'
         };
        
         try {
           instance.sendContent(data, true);
           alert("Data has been successfully sent");
         } catch (error) {
           alert("Error: " + error.message);
         }
        });
    }
    , function(error){alert("error while getting the instance: "+error);});
   }
  });
 }, function(error)
 {
    alert("Error: " + error.message);
 });
}

响应

4 回复
Iqbal Hossain

hi~ 

Try this

widgetSuccessCb = function(instances) {
   //forEach Instance // use loop or anything you like
    var data = {
      data1 : 'test1',
	  data2 : 'test2'
	 };

	 try {
	   instance.sendContent(data, true);
	   console.log("Data has been successfully sent");
	 } catch (error) {
	   console.log("Error: " + error.message);
	 }
 }

 widgetErrorCb = function(error) {
   console.log("Error occurred: " + error.name + ": " + error.message);
 }
 
 
//variable id should contain valid id of the installed widget
 var id = "org.tizen.gallery.widget";

 try {
   var widget = tizen.widgetservice.getWidget(id);
   widget.getInstances(widgetSuccessCb, widgetErrorCb);
 } catch (error) {
   console.log("Error: " + error.message);
 }

 

Philippe Wechsler

Hi, this is only the code from the sample, which is equal to mine and which IS NOT WORKING. Is this a bug or what is wrong with that code?

 

Iqbal Hossain

Yes @philippe, 

Ref: https://developer.tizen.org/development/guides/web-application/application-management/application-information-and-controls/widget-information

Can you get the instances with widgetSuccessCb()

And as the  Jeongkyun Pu telling it needs Native Widget to send and get Content, may be it is not completed in web widget till now. 

 

 

Jeongkyun Pu

If 's29U0dfg8I.WidgetSampleHost' is native widget (C API), you have to handle content(bundle) in native widget .

 

For example. Below is native widget code.

static int widget_instance_update(widget_context_h context, bundle *content,    int force, void *user_data){

/* Take necessary actions when widget instance should be updated. */

widget_app_context_set_content_info(context, content);  //update content from sendContent() at web application.

return WIDGET_ERROR_NONE;

}

 

If your sample widget is web widget. there is no way to set content in web widget. sendContent/getContent() is only supported in native widget.

You should use preference API for sending/getting data.

https://developer.tizen.org/development/api-references/web-application?redirect=https://developer.tizen.org/dev-guide/2.3.2/org.tizen.web.apireference/html/device_api/wearable/tizen/preference.html