Changes between Version 8 and Version 9 of UsingSensors

Changes between Version 8 and Version 9 of UsingSensors

Please note that these Trac pages are no longer being updated. Wiki contents/documentation have moved to GitHub.

Changes between Version 8 and Version 9 of UsingSensors

Please note that these Trac pages are no longer being updated. Wiki contents/documentation have moved to GitHub.

Changes between Version 8 and Version 9 of UsingSensors

Show
Ignore:
Timestamp:
08/16/12 18:15:05 (7 years ago)
Author:
yanyan
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • UsingSensors

    v8 v9  
    131131 
    132132}}} 
     133 
     134== Sensors on Android ==  
     135 
     136Smartphones often come with a number of embedded sensors and chips, such as an accelerometer, GPS, Wifi card, etc. New sensing applications therefore can turn an everyday phone into a smart sensor. As we have Seattle ported on Android platform ([https://seattle.cs.washington.edu/wiki/SeattleOnAndroid SeattleOnAndroid]), here is a brief introduction how to provide APIs that an Android application can use to make the sensor available to Seattle on Android. 
     137 
     138=== XMLRPC Library === 
     139 
     140Python has a set of quick and easy [http://docs.python.org/library/simplexmlrpcserver.html the SimpleXMLRPCAPI] available. However, Android Java applications have to import their own library. [http://ws.apache.org/xmlrpc/ Apache WS-XMLRPC] is one of the most popular Java XMLRPC libraries. Here are a few steps how to import it, and create a light weight SimpleXmlRPcServer for Android.  
     141 
     142==== Importing Apache ==== 
     143 
     144Download the Apache library from [http://ws.apache.org/xmlrpc/ Apache WS-XMLRPC]. The jar files in /apache-xmlrpc-version-number/lib/ are what you need. Create a new folder ''libs'' in your Eclipse Android project (the libs folder will be automatically created by Eclipse with higher version (>15) of Android API). 
     145 
     146Right click libs and choose Import -> General -> File System, then Next. Browse in the filesystem to find the library's parent directory (i.e., /apache-xmlrpc-version-number/lib/). Click the directory name, then check the relevant jar files in the right pane. This puts the library physically into your project.  
     147 
     148Right click on your Android project, choose properties -> "Java Build Path" tab, click the Libraries tab, then "Add JARs...", navigate to the new jar files you just imported in the ''libs'' directory and add it. They should now appear under “Referenced Libraries” in your project. This is the moment when your new jar files are converted for use on Android. 
     149 
     150==== SimpleXmlRPcServer for Android ==== 
     151 
     152[http://www.zugiart.com/2010/09/java-apache-simple-xmlrpc-server/ zen] developed a simple and light weight SimpleXmlRPcServer using the above Apache library, which allows us to very quickly craft a standalone, simple XMLRPC server using the above Apache library. No servlet or container is required.  
     153 
     154Two classes are needed for the server, '''SimpleXmlRPcServer''' and '''SimpleXmlRPcRequestHandlerFactory''' on zen's page. Simply place these two classes in your project then you are ready to go. According to zen, the code "supports single instance handler object, but not yet multi-threaded".  
     155 
     156=== Example Use === 
     157 
     158Here are some example code for building Android sensor (XMLRPC server) and a client.  
     159 
     160Sensor code: 
     161{{{ 
     162public class RpcServer extends IntentService { 
     163        private int[] portArray = new int[] {63090, 63091, 63092, 63093, 63094, 63095, 63096, 63097, 63098, 63099}; 
     164        private int port = 0; 
     165        ......... 
     166         
     167        @Override 
     168        protected void onHandleIntent(Intent intent) {   
     169                SimpleXmlRpcServer server; 
     170                int i; 
     171                for (i = 0; i < portArray.length; i++) { 
     172                        port = portArray[i]; 
     173                        try { 
     174                                server = new SimpleXmlRpcServer(port); 
     175                                server.addHandler("yanyan", new SimpleXMLRpcTest()); 
     176                                server.serve_forever(); 
     177                                Toast.makeText(context, "Server Listening on: " + Integer.toString(port), Toast.LENGTH_LONG).show(); 
     178                                break; 
     179                        } catch (Exception e) { 
     180                                String text = Log.getStackTraceString(e); 
     181                                Log.e("RpcServer Exception", text); 
     182                        } 
     183                }                
     184                if (i == portArray.length) { 
     185                        Log.e("RpcServer Exception", "Could not locate a port!"); 
     186                } 
     187        } 
     188} 
     189 
     190public class SimpleXMLRpcTest { 
     191 
     192        public String hello(){ 
     193                return "hello world"; 
     194        } 
     195  
     196        public boolean isSeattleSensor() { 
     197                return true; 
     198        } 
     199}        
     200}}} 
     201 
     202Sensor reading code: 
     203{{{ 
     204XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); 
     205int i; 
     206for (i = 0; i < portArray.length; i++) { 
     207        port = portArray[i]; 
     208        try { 
     209                config.setServerURL(new URL("http://127.0.0.1:" + Integer.toString(port))); 
     210                XmlRpcClient client = new XmlRpcClient(); 
     211                client.setConfig(config); 
     212                Object[] params = new Object[]{}; 
     213 
     214                String result = (String) client.execute("yanyan." + hello, params); 
     215                response = result; 
     216                Log.d("ClientActivity got response ", result); 
     217                break; 
     218        } catch (XmlRpcException e) { 
     219                String text = Log.getStackTraceString(e); 
     220                Log.e("RpcClient Exception", text); 
     221        } catch (MalformedURLException e) { 
     222                if (e.getMessage().contains("refused")) continue; 
     223        }  
     224} 
     225 
     226if (i == portArray.length) { 
     227        Log.e("ClientActivity", "Could not locate a port!"); 
     228} 
     229}}} 
     230 
     231In sensor code ''server.addHandler("yanyan", new SimpleXMLRpcTest())'', the string label for the handler can be anything. But the client code ''client.execute("yanyan." + hello, params)'' has to use the same string label. Another important thing is, XMLRPC can only work on a normal Java class, not Activity or Service. Therefore I made a class SimpleXMLRpcTest outside RPcServer, which extends IntentService. The client code can be placed in Activity and has no constraints.