org.symbian.tools.wrttools.doc.WebDeveloper/html/GUID-350F3C29-923E-4C30-8B14-C3D594BB8E39.html
changeset 229 716254ccbcc0
equal deleted inserted replaced
228:913c9751c067 229:716254ccbcc0
       
     1 
       
     2 <!DOCTYPE html
       
     3   PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
       
     4 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta name="copyright" content="(C) Copyright 2009"/><meta name="DC.rights.owner" content="(C) Copyright 2009"/><meta name="DC.Type" content="mobileconcept"/><meta name="DC.Title" content="Sensor Service API example"/><meta name="DC.Relation" scheme="URI" content="GUID-46FAFCDA-BCF3-41CB-B154-C17BBF7CD755"/><meta name="DC.Relation" scheme="URI" content="GUID-7C69DDA4-16F1-4A8F-BDB2-4CB0015B4E81"/><meta name="DC.Relation" scheme="URI" content="GUID-65AAF569-D347-462B-B59A-9D7CA184AB9C"/><meta name="DC.Relation" scheme="URI" content="GUID-B77C2006-879F-4AC6-B7BF-04B25B563A29"/><meta name="DC.Format" content="XHTML"/><meta name="DC.Identifier" content="GUID-350F3C29-923E-4C30-8B14-C3D594BB8E39"/><title>Sensor Service API example </title><script type="text/javascript">
       
     5       function initPage() {}
       
     6     </script><link href="../PRODUCT_PLUGIN/book.css" rel="stylesheet" type="text/css"/><link href="css/s60/style.css" rel="stylesheet" type="text/css" media="all"/></head><body onload="initPage();"><div class="body"><div class="contentLeft prTxt"><h1 class="pageHeading" id="GUID-350F3C29-923E-4C30-8B14-C3D594BB8E39">Sensor Service API example</h1><div>
       
     7 <p/>
       
     8 <p>This section presents the full source code of a working sample widget
       
     9 for the <a href="GUID-6FBE64B7-0949-4AAE-B835-52B276540A59.html#GUID-6FBE64B7-0949-4AAE-B835-52B276540A59">Sensor Service</a>.
       
    10 You can download the <code>wgz</code> package for this widget from
       
    11 section <a href="GUID-775005BC-2FF8-45A9-BBA6-6CED6B5780A2.html#GUID-775005BC-2FF8-45A9-BBA6-6CED6B5780A2">Example widgets</a>.</p>
       
    12 <p>For general information about creating widgets, see section <a href="GUID-0E3095DB-03FF-4240-83F2-6D876AD2083A.html#GUID-0E3095DB-03FF-4240-83F2-6D876AD2083A">Widget component files</a>.</p>
       
    13 <p>For widget development and debugging purposes, this example writes its
       
    14 output to <code>c:\data\jslog_widget.log</code> using <code>console.info</code>.
       
    15 For instructions on how to enable logging in the Web browser for S60, see
       
    16 section <a href="GUID-B584CA90-543B-4AED-B134-A3A616259DB9.html#GUID-B584CA90-543B-4AED-B134-A3A616259DB9">JavaScript console</a>.</p>
       
    17 <div><h3>Info.plist</h3>
       
    18 <pre class="codeblock" id="GUID-CD42B8F8-DF33-4605-B365-0834C7DF4A9F">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
       
    19 &lt;!DOCTYPE plist PUBLIC "-//Nokia//DTD PLIST 1.0//EN" "http://www.nokia.com/NOKIA_COM_1/DTDs/plist-1.0.dtd"&gt;
       
    20 &lt;plist version="1.0"&gt;
       
    21 &lt;dict&gt;
       
    22   &lt;key&gt;DisplayName&lt;/key&gt;
       
    23   &lt;string&gt;SensorSample&lt;/string&gt;
       
    24   &lt;key&gt;Identifier&lt;/key&gt;
       
    25   &lt;string&gt;com.nokia.widget.sapi.sensor.sample&lt;/string&gt;
       
    26   &lt;key&gt;Version&lt;/key&gt;
       
    27   &lt;string&gt;1.0&lt;/string&gt;
       
    28   &lt;key&gt;MainHTML&lt;/key&gt;
       
    29   &lt;string&gt;sensor-sample.html&lt;/string&gt;
       
    30 &lt;/dict&gt;
       
    31 &lt;/plist&gt;</pre>
       
    32 </div>
       
    33 <div><h3>sensor-sample.html</h3>
       
    34 <pre class="codeblock" id="GUID-2530A43F-A36C-472E-8B99-CD98B03CE5BE">&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
       
    35 &lt;html&gt;
       
    36   &lt;head&gt;
       
    37     &lt;script type="text/javascript" src="js/common.js"&gt;&lt;/script&gt;
       
    38     &lt;script type="text/javascript" src="js/sensor-sample.js" charset="utf-8"&gt;&lt;/script&gt;
       
    39   &lt;/head&gt;
       
    40   &lt;body onload='setup()' bgcolor="#ddeeff"&gt;
       
    41     &lt;form name="frm"&gt;
       
    42       &lt;h3&gt;Sapi Sensor Sample Widget&lt;/h3&gt;
       
    43       &lt;input type="button" onclick="findSensorChannel('img1')" value="FindSensorChannel"&gt;&lt;img id="img1" src="pic/blank.png" width="25" height="25" align="center"&gt;&lt;br&gt;
       
    44       &lt;input type="button" onclick="getChannelProperty('img2')" value="GetChannelProperty"&gt;&lt;img id="img2" src="pic/blank.png" width="25" height="25" align="center"&gt;&lt;br&gt;
       
    45       &lt;input type="button" onclick="setChannelProperty('img3')" value="RegisterForNotification"&gt;&lt;img id="img5" src="pic/blank.png" width="25" height="25" align="center"&gt;&lt;br&gt;
       
    46       &lt;input type="button" onclick="stopReceivingNotification('img6')" value="Cancel"&gt;&lt;img id="img6" src="pic/blank.png" width="25" height="25" align="center"&gt;&lt;hr&gt;
       
    47       &lt;div class='sensor' id='sensor' bgcolor="#ddeeff" style=width:100%;height:100%;overflow:auto&gt;&lt;/div&gt;
       
    48     &lt;/form&gt;
       
    49   &lt;/body&gt;
       
    50 &lt;/html&gt;
       
    51 </pre>
       
    52 </div>
       
    53 <div><h3>common.js</h3>
       
    54 <pre class="codeblock" id="GUID-9276D901-4CAC-4CDD-AFC4-6A94C816785C">// common.js
       
    55 //
       
    56 // This file contains some utility functions
       
    57 
       
    58 // Check the error code and show the information to users
       
    59 function checkError(message, resultList, divId, imgId)
       
    60 {
       
    61   var errCode = resultList.ErrorCode;
       
    62   var msg = "";
       
    63   
       
    64   if (errCode) {
       
    65     msg = message + "&lt;BR&gt;" + "Failed Error: " + errCode + "&lt;BR&gt;";
       
    66     if(resultList.ErrorMessage != undefined)
       
    67       msg += "Error Message: " + resultList.ErrorMessage;
       
    68     showIMG(imgId,"no"); 
       
    69   } else {
       
    70     showIMG(imgId,"yes"); 
       
    71   }
       
    72 
       
    73   //print error message
       
    74   if(divId != null &amp;&amp; divId != undefined)
       
    75     document.getElementById(divId).innerHTML = msg;
       
    76   console.info(msg);
       
    77   
       
    78   return errCode;
       
    79 }
       
    80 
       
    81 // Build the message by reading a iteratorable list in a recursive manner
       
    82 function showIterableList(iterator)
       
    83 {
       
    84   var msg = "";
       
    85   try
       
    86   {
       
    87     iterator.reset();
       
    88     var item;
       
    89     while (( item = iterator.getNext()) != undefined ){
       
    90       msg += showObject( item );
       
    91     }
       
    92   }
       
    93   catch(e)
       
    94   {
       
    95     alert('&lt;showIterableList&gt; ' + e);
       
    96   }
       
    97   return msg;
       
    98 }
       
    99 
       
   100 // Build the message by reading a JS object in a recursive manner
       
   101 function showObject( obj )
       
   102 {
       
   103   var txt = "";
       
   104   try { 
       
   105     if ( typeof obj != 'object' )
       
   106       return "" + obj + '&lt;BR/&gt;';
       
   107     else {
       
   108       for(var key in obj) {
       
   109         txt +=  key + ":";
       
   110         txt += showObject( obj[key] );
       
   111         txt += '&lt;BR/&gt;';
       
   112       }
       
   113       txt += '&lt;BR/&gt;';
       
   114     }
       
   115   } 
       
   116   catch (e) 
       
   117   {
       
   118     alert("showObject: " + e);
       
   119   }
       
   120   return txt;
       
   121 }
       
   122 
       
   123 // Show the image to indicate the test result
       
   124 function showIMG(imgId, isOK) 
       
   125 {
       
   126   if(imgId == null || imgId == undefined)
       
   127     return;
       
   128   
       
   129   if(isOK == "yes")
       
   130     document.getElementById(imgId).src = "pic/yes.png";
       
   131   else if(isOK == "no")
       
   132     document.getElementById(imgId).src = "pic/no.png";
       
   133   else
       
   134     document.getElementById(imgId).src = "pic/blank.png";
       
   135 }
       
   136 
       
   137 // Show elements in object by using 'alert'
       
   138 function testObject(obj)
       
   139 {
       
   140   var msg = "";
       
   141   for(var key in obj) {
       
   142     msg = msg + ":" + key + "=" + obj[key];
       
   143   }
       
   144   alert(msg);
       
   145 }
       
   146 
       
   147 // Test whether the input is numeric
       
   148 function IsNumeric(sText)
       
   149 {
       
   150   var ValidChars = "0123456789.";
       
   151   var IsNumber=true;
       
   152   var Char;
       
   153   
       
   154   for (i = 0; i &lt; sText.length &amp;&amp; IsNumber == true; i++) 
       
   155   { 
       
   156     Char = sText.charAt(i); 
       
   157     if (ValidChars.indexOf(Char) == -1) 
       
   158     {
       
   159       IsNumber = false;
       
   160     }
       
   161   }
       
   162   return IsNumber; 
       
   163 }</pre>
       
   164 </div>
       
   165 <div><h3>sensor-sample.js</h3>
       
   166 <pre class="codeblock" id="GUID-55D3CB19-580C-4F4F-9564-5D268A27A7A7">// sensor-sample.js
       
   167 //
       
   168 // In this sample the sensor will be found, get and set;
       
   169 // Also, async operation will be canceled.
       
   170 //
       
   171 
       
   172 // SAPI Error Codes
       
   173 // 0  - Success
       
   174 // 1000 - InvalidServiceArgument
       
   175 // 1001 - UnknownArgumentName
       
   176 // 1002 - BadArgumentType
       
   177 // 1003 - MissingArgument
       
   178 // 1004 - ServiceNotSupported
       
   179 // 1005 - ServiceInUse
       
   180 // 1006 - ServiceNotReady
       
   181 // 1007 - NoMemory
       
   182 // 1008 - HardwareNotAvailable
       
   183 // 1009 - ServerBusy
       
   184 // 1010 - EntryExists
       
   185 // 1011 - AccessDenied
       
   186 // 1012 - NotFound
       
   187 // 1013 - UnknownFormat
       
   188 // 1014 - GeneralError
       
   189 // 1015 - CancelSuccess
       
   190 // 1016 - ServiceTimedOut
       
   191 // 1017 - PathNotFound
       
   192 
       
   193 // Usage:
       
   194 // 1. Call FindSensorChannel (input searchCriterion) - this call obtains a list of maps.The list can be fetched using the key : ReturnValue
       
   195 // 2. From the list you need to fetch one ChannelInfo as a map.The fetching depends on how the runtime fetches items from an CLiwDefaultList.
       
   196 // 3. It is this ChannelInfo map that needs to be supplied to all the other APIs that follow.
       
   197  
       
   198 // Declare the service object
       
   199 var so;
       
   200 
       
   201 // imgid for callback1 function
       
   202 var imgid_callback1;
       
   203 
       
   204 // id of the div used to display information
       
   205 const DIV_ID = 'sensor';
       
   206 
       
   207 // transaction id
       
   208 var transactionID;
       
   209 
       
   210 // Called from onload()
       
   211 function setup()
       
   212 {
       
   213   try {
       
   214     so = device.getServiceObject("Service.Sensor", "ISensor");
       
   215     console.info("setup: so: %s", so);
       
   216   }
       
   217   catch (e) {
       
   218     alert('&lt;setup&gt; ' +e);
       
   219   }
       
   220 }
       
   221 
       
   222 //FindSensorChannel - searches and list available sensor channels.
       
   223 function findSensorChannel(imgId)
       
   224 {
       
   225   var criteria = new Object();
       
   226   criteria.SearchCriterion = "Rotation";
       
   227 
       
   228   try
       
   229   {
       
   230     var result = so.ISensor.FindSensorChannel(criteria);
       
   231     if(!checkError("ISensor::findSensorChannel",result,DIV_ID,imgId)) {
       
   232       document.getElementById(DIV_ID).innerHTML = showIterableList(result.ReturnValue);
       
   233     }
       
   234   }
       
   235   catch (e)
       
   236   {
       
   237     showIMG(imgId,"no");
       
   238     alert ("findSensorChannel: " + e);
       
   239   }
       
   240 }
       
   241 
       
   242 // GetChannelProperty
       
   243 function getChannelProperty(imgId)
       
   244 {
       
   245   var channelInfoMap = new Object();
       
   246   channelInfoMap.ChannelId = 1;
       
   247   channelInfoMap.ContextType = 0;  // 0-not defined; 1- to measure pressure, ...
       
   248   channelInfoMap.Quantity = 0;
       
   249   channelInfoMap.ChannelType = 0 ;
       
   250 
       
   251   var criteria = new Object();
       
   252   criteria.PropertyId = "Description";
       
   253   criteria.ChannelInfoMap = channelInfoMap;
       
   254 
       
   255   try
       
   256   {
       
   257     var result = so.ISensor.GetChannelProperty(criteria);
       
   258     if(!checkError("ISensor::getChannelProperty",result,DIV_ID,imgId)) {
       
   259       document.getElementById(DIV_ID).innerHTML = showIterableList(result.ReturnValue);
       
   260     }
       
   261   }
       
   262   catch (e)
       
   263   {
       
   264     showIMG(imgId,"no");
       
   265     alert ("getChannelProperty: " + e);
       
   266   }
       
   267 }
       
   268 
       
   269 // RegisterForNotification - register to receive
       
   270 // notification about channel data.
       
   271 function registerForNotification(imgId)
       
   272 {
       
   273   var channelInfoMap = new Object();
       
   274   channelInfoMap.ChannelId = 1;
       
   275   channelInfoMap.ContextType = 0;  // 0-not defined; 1- to measure pressure, ...
       
   276   channelInfoMap.Quantity = 0;
       
   277   channelInfoMap.ChannelType = 0 ;
       
   278 
       
   279   var criteria = new Object();
       
   280   criteria.ListeningType = "ChannelData";
       
   281   criteria.ChannelInfoMap = channelInfoMap;
       
   282 
       
   283   try
       
   284   {
       
   285     imgid_callback1 = imgId
       
   286     var result = so.ISensor.RegisterForNotification(criteria, callback1);
       
   287     transactionID = result.TransactionID;
       
   288     if(!checkError("ISensor::registerForNotification",result,DIV_ID,imgId)){
       
   289       showIMG(imgId,"");
       
   290     }
       
   291   }
       
   292   catch (e)
       
   293   {
       
   294     showIMG(imgId,"no");
       
   295     alert ("registerForNotification: " + e);
       
   296   }
       
   297 }
       
   298 
       
   299 //Cancel Notification
       
   300 function Cancel(imgId)
       
   301 {
       
   302   var criteria = new Object();
       
   303   criteria.TransactionID = transactionID;
       
   304   try
       
   305   {
       
   306     var result = so.ISensor.Cancel(criteria);
       
   307     checkError("ISensor::Cancel",result,DIV_ID,imgId)
       
   308   }
       
   309   catch (e)
       
   310   {
       
   311     showIMG(imgId,"no");
       
   312     alert ("Cancel: " + e);
       
   313   }
       
   314 }
       
   315 
       
   316 // This is the asynchronous callback handler
       
   317 function callback1(transId, eventCode, result)
       
   318 {
       
   319   console.info("callback1: transId: %d  eventCode: %d result.ErrorCode: %d", transId, eventCode, result.ErrorCode);
       
   320   if(!checkError("ISensor::registerForNotification",result,DIV_ID,imgid_callback1)) {
       
   321     document.getElementById(DIV_ID).innerHTML = "Channel Property changes";
       
   322   }
       
   323 }</pre>
       
   324 </div>
       
   325 </div></div></div><div class="footer"><hr/><div class="copy">© Nokia 2009.</div></div></body></html>