You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@edgent.apache.org by James Cancilla <ca...@gmail.com> on 2016/04/01 17:41:03 UTC

Instructions for Android

Hello,

Are there any instructions for running Quarks on Android? I am trying to
use Quarks to collect and analyze phone GPS data. Any documentation or
starter code would be appreciated.

Thanks,
James

Re: Instructions for Android

Posted by Dan Debrunner <dj...@debrunners.com>.
> On Friday, April 1, 2016 8:41 AM, James Cancilla <ca...@gmail.com> wrote:


> Are there any instructions for running Quarks on Android?

Sample code, this was in the MainActivity.onCreate method:

SensorManager mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 

dp = new DirectProvider(); 

topology = dp.newTopology("QuarkSimpleSensor"); 

// Create a source stream of SensorEvents for temp & pressure 
// Topology has a variety of methods to create sources 
// This is a utility method for Android sensors 

TStream<SensorEvent> sensors = SensorStreams.sensors(topology, mSensorManager, 
Sensor.TYPE_AMBIENT_TEMPERATURE, Sensor.TYPE_PRESSURE); 

// Example of a filter 
// (Java 8 would be: 
// e -> e.values[0] > 15.0 
sensors = sensors.filter(new Predicate<SensorEvent>() { 
public boolean test(SensorEvent event) { 
return event.values[0] > 15.0; 
} 
}); 

final TextView tv = (TextView) findViewById(R.id.text); 

// Terminate the stream by printing to a TextView 
ActivityStreams.sinkOnUIThread(this, sensors, new Consumer<SensorEvent>() { 
@Override 
public void accept(SensorEvent event) { 
tv.setText("QUARKS:" + event.timestamp + event.sensor.getName() + 
event.values[0]); 
} 
}); 


// Convert sensors stream to a JSON event for IoTF 
TStream<JsonObject> sensorsJson = 
sensors.map(new Function<SensorEvent, JsonObject>() { 
@Override 
public JsonObject apply(SensorEvent event) { 
JsonObject j = new JsonObject(); 
j.addProperty("name", event.sensor.getName()); 
j.addProperty("value", event.values[0]); 
return j; 
} 
}); 


// Send to iotf  as device events - need valid device id from an IoTF server 
// Can also load config from a file. 
Properties options = new Properties(); 
options.setProperty("org", "XXXXX"); // Use your IoTF org and other properties 

IotfDevice device = new IotfDevice(topology, options); 

// publish to IoTF as Sensor events 
// Note at this point there is no local 
// analytics,. every event (after the filter) 
// is sent to IoTF. Sample will be improved 
// to show some aggregation. 
device.events(sensorsJson, "sensors", 0); 

// Separate source stream 
// Get the time every 5 seconds. 
TStream<Date> dates = topology.poll(new Supplier<Date>() { 
@Override 
public Date get() { 
return new Date(); 
} 
}, 5, TimeUnit.SECONDS); 

// Utility method to map/transform a string to a stream of string objects 
TStream<String> datesStr = dates.asString(); 

final TextView tv2 = (TextView) findViewById(R.id.text2); 

// Terminate the stream by printing to a TextView 
ActivityStreams.sinkOnUIThread(this, datesStr, new Consumer<String>() { 
@Override 
public void accept(String msg) { 
tv2.setText("QUARKS_2:" + msg); 
} 
}); 

// At the point the topology (streaming graph) 
// has just been declared. Nothing is runing, 
// TStream instances are a declaration of a stream 
// not a runtime object. 

// Now submit (start) the application. 
// Will run on its own threads 
dp.submit(topology);

Re: Instructions for Android

Posted by James Cancilla <ca...@gmail.com>.
Hi Dan,

Thank you for your help. I was able to get my application working with the
information you provided.

There are 2 additional steps that I had to do that I want to mention in
case someone else tries this:

1. When I added the 'quarks' directory to app/libs, I had to navigate into
each of the sub-directories, right-click on the jar files and select "Add
to library...". I'm not sure if there is an easier way to do this in
Android Studio.
2. When I first tried to add the code to resolve the connectivity issue,
Android Studio could not find ProviderInstaller. I had to add the following
lines to the build.gradle file:

dependencies {
    compile 'com.google.android.gms:play-services:8.4.0'
}


Thanks,
James




On Fri, Apr 1, 2016 at 1:03 PM, Dan Debrunner <dj...@debrunners.com> wrote:

> One more item, I had to add this code to allow connectivity to IBM Watson
> IoT Platform, it relates to security issues around SSL.
>
> http://developer.android.com/training/articles/security-gms-provider.html
>
>
> try {
>
> com.google.android.gms.security.ProviderInstaller.installIfNeeded(getApplicationContext());
> } catch (GooglePlayServicesRepairableException e) {
> e.printStackTrace();
> } catch (GooglePlayServicesNotAvailableException e) {
> e.printStackTrace();
> }
>
> Dan.
>

Re: Instructions for Android

Posted by Dan Debrunner <dj...@debrunners.com>.
One more item, I had to add this code to allow connectivity to IBM Watson IoT Platform, it relates to security issues around SSL.

http://developer.android.com/training/articles/security-gms-provider.html


try { 
com.google.android.gms.security.ProviderInstaller.installIfNeeded(getApplicationContext()); 
} catch (GooglePlayServicesRepairableException e) { 
e.printStackTrace(); 
} catch (GooglePlayServicesNotAvailableException e) { 
e.printStackTrace(); 
}

Dan.

Re: Instructions for Android

Posted by Dan Debrunner <dj...@debrunners.com>.
> On Friday, April 1, 2016 8:41 AM, James Cancilla <ca...@gmail.com> wrote:


> Are there any instructions for running Quarks on Android? I am trying to
> use Quarks to collect and analyze phone GPS data. Any documentation or
> starter code would be appreciated.


Thanks for the nudge, I've been meaning to write something up. 

Here's a quick overview. 

First you have to build Quarks with Android enabled, see: 

https://github.com/apache/incubator-quarks/blob/master/DEVELOPMENT.md 

With ANDROID_SDK_PLATFORM set then 'ant release' will build Android jars under target/android. 

I created an Android application that read a couple of the phone sensors 
(e.g. air temp and pressure) displayed them to a text box and also sent them 
to IBM Watson IoT Platform as device events. 


I'm not an Android expert so I'll just list what I did, or what I can remember doing. 


1) Using Android Studio I created a new project. 
2) Under app/libs I added a quarks folder and copied the contents of target/android to that directory. 
3) Code your application, there is some Android specific utilities to get Android sensors (not sure about GPS) as a stream and to have a stream processing execute on the main UI thread.
4) To build the application I did need to modify build.gradle to exclude files from the Quarks jars that have the same path, adding this:

packagingOptions { 
exclude 'META-INF/license.txt' 
exclude 'META-INF/LICENSE' 
exclude 'META-INF/LICENSE.txt' 
exclude 'META-INF/notice.txt' 
exclude 'META-INF/NOTICE' 
exclude 'META-INF/NOTICE.txt' 
exclude 'META-INF/DEPENDENCIES' 
}
Note this was only for a test application, I provide no advice on legality of just removing such legal notices and not having them in your Android application in some other form.

I was pushing the application to my phone using a usb cable with the phone in development mode.

I'll provide some sample code in a separate reply.

HTH,
Dan.