Commit dae92dd6 authored by Larry Sachs's avatar Larry Sachs Committed by Rick Bell

Android Client/Server Apps using Java O-O classes

Updates client/server apps to use Java O-O wrappers
for SWIG generated code.

Change-Id: I87209ac132c91d863bd221732a16ab27a28dd0d7
Signed-off-by: Larry Sachs's avatarLarry Sachs <larry.j.sachs@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/29430Tested-by: default avatarIoTivity Jenkins <jenkins-daemon@iotivity.org>
Reviewed-by: George Nash's avatarGeorge Nash <george.nash@intel.com>
Reviewed-by: default avatarRick Bell <richard.s.bell@intel.com>
parent 6d3b5a2b
......@@ -23,6 +23,8 @@ public class MyInitHandler implements OCMainInitHandler {
Log.d(TAG, "inside MyInitHandler.initialize()");
int ret = OCMain.initPlatform("Intel");
ret |= OCMain.addDevice("/oic/d", "oic.d.light", "Lamp", "ocf.1.0.0", "ocf.res.1.0.0");
OCMain.setRandomPinHandler(new RandomPinHandler(activity));
return ret;
}
......
package org.iotivity.simpleserver;
import android.util.Log;
import org.iotivity.OCRandomPinHandler;
public class RandomPinHandler implements OCRandomPinHandler {
private static final String TAG = RandomPinHandler.class.getSimpleName();
private ServerActivity activity;
public RandomPinHandler(ServerActivity activity) {
this.activity = activity;
}
@Override
public void handler(String pin) {
Log.d(TAG, "inside Random Pin Handler");
activity.msg("\n!!!!!!!!!!!!!!!!!!!!\nRandom PIN: " + pin + "\n!!!!!!!!!!!!!!!!!!!!\n");
activity.printLine();
}
}
......@@ -49,7 +49,6 @@ public class OnBoardingActivity extends AppCompatActivity {
private OcObt obt;
private OcPlatform obtPlatform;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......
*.iml
.gradle
/local.properties
/.idea
.DS_Store
/build
/captures
.externalNativeBuild
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
defaultConfig {
applicationId "org.iotivity.simpleclient"
minSdkVersion 21
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
jniLibs.srcDirs = ["src/main/jniLibs", "$buildDir/native-libs"]
}
}
splits {
abi {
enable true
reset()
include 'x86_64', 'armeabi'
universalApk false
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile 'junit:junit:4.12'
}
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /home/larrys/Android/Sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.iotivity.simpleclient">
<uses-feature android:name="android.hardware.nfc" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.NFC" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:screenOrientation="portrait"
android:name=".ClientActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package org.iotivity.simpleclient;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;
import org.iotivity.oc.OcPlatform;
public class ClientActivity extends AppCompatActivity {
private static final String TAG = ClientActivity.class.getSimpleName();
private TextView mConsoleTextView;
private ScrollView mScrollView;
private OcPlatform obtPlatform;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mConsoleTextView = (TextView) findViewById(R.id.consoleTextView);
mConsoleTextView.setMovementMethod(new ScrollingMovementMethod());
mScrollView = (ScrollView) findViewById(R.id.scrollView);
mScrollView.fullScroll(View.FOCUS_DOWN);
if (savedInstanceState == null) {
// start first time only
obtPlatform = OcPlatform.getInstance();
MyInitHandler handler = new MyInitHandler(this, obtPlatform);
obtPlatform.systemInit(handler);
}
}
@Override
protected void onDestroy() {
Log.d(TAG, "Calling Shutdown.");
obtPlatform.systemShutdown();
super.onDestroy();
}
public void msg(final String text) {
runOnUiThread(new Runnable() {
public void run() {
mConsoleTextView.append("\n");
mConsoleTextView.append(text);
mScrollView.fullScroll(View.FOCUS_DOWN);
}
});
Log.i(TAG, text);
}
public void printLine() {
msg("------------------------------------------------------------------------");
}
}
package org.iotivity.simpleclient;
import org.iotivity.OCClientResponse;
import org.iotivity.OCQos;
import org.iotivity.OCRepresentation;
import org.iotivity.OCResponseHandler;
import org.iotivity.oc.OcCborEncoder;
import org.iotivity.oc.OcUtils;
public class GetLightResponseHandler implements OCResponseHandler {
private static final String TAG = GetLightResponseHandler.class.getSimpleName();
private ClientActivity activity;
private Light light;
public GetLightResponseHandler(ClientActivity activity, Light light) {
this.activity = activity;
this.light = light;
}
@Override
public void handler(OCClientResponse response) {
activity.msg("Get Light Response Handler:");
OCRepresentation rep = response.getPayload();
while (rep != null) {
switch (rep.getType()) {
case OC_REP_BOOL:
activity.msg("\tKey " + rep.getName() + " value " + rep.getValue().getBool());
light.state = rep.getValue().getBool();
break;
case OC_REP_INT:
activity.msg("\tKey " + rep.getName() + " value " + rep.getValue().getInteger());
light.power = rep.getValue().getInteger();
break;
case OC_REP_STRING:
activity.msg("\tKey " + rep.getName() + " value " + rep.getValue().getString());
light.name = rep.getValue().getString();
break;
default:
break;
}
rep = rep.getNext();
}
activity.printLine();
PutLightResponseHandler putLight = new PutLightResponseHandler(activity, light);
if (OcUtils.initPut(light.serverUri, light.serverEndpoint, null, putLight, OCQos.LOW_QOS)) {
OcCborEncoder root = OcCborEncoder.createOcCborEncoder(OcCborEncoder.EncoderType.ROOT);
root.setBoolean("state", true);
root.setLong("power", 15);
root.done();
if (OcUtils.doPut()) {
activity.msg("\tSent PUT request");
} else {
activity.msg("\tCould not send PUT request");
}
} else {
activity.msg("\tCould not init PUT request");
}
activity.printLine();
}
}
package org.iotivity.simpleclient;
import org.iotivity.OCEndpoint;
public class Light {
public String name;
public long power;
public boolean state;
public OCEndpoint serverEndpoint;
public String serverUri;
}
package org.iotivity.simpleclient;
import org.iotivity.OCDiscoveryFlags;
import org.iotivity.OCDiscoveryHandler;
import org.iotivity.OCEndpoint;
import org.iotivity.OCInterfaceMask;
import org.iotivity.OCQos;
import org.iotivity.OCResourcePropertiesMask;
import org.iotivity.oc.OcUtils;
import java.util.Arrays;
public class MyDiscoveryHandler implements OCDiscoveryHandler {
private static final String TAG = MyDiscoveryHandler.class.getSimpleName();
private ClientActivity activity;
private Light light;
public MyDiscoveryHandler(ClientActivity activity) {
this.activity = activity;
}
@Override
public OCDiscoveryFlags handler(String anchor, String uri, String[] types, int interfaceMask, OCEndpoint endpoint, int resourcePropertiesMask) {
activity.msg("DiscoveryHandler:");
activity.msg("\tanchor: " + anchor);
activity.msg("\turi: " + uri);
activity.msg("\ttypes: " + Arrays.toString(types));
String interfacesFound = "";
if ((interfaceMask & OCInterfaceMask.S) > 0) {
interfacesFound += "S";
}
if ((interfaceMask & OCInterfaceMask.A) > 0) {
if (!interfacesFound.isEmpty()) {
interfacesFound += " | ";
}
interfacesFound += "A";
}
if ((interfaceMask & OCInterfaceMask.RW) > 0) {
if (!interfacesFound.isEmpty()) {
interfacesFound += " | ";
}
interfacesFound += "RW";
}
if ((interfaceMask & OCInterfaceMask.R) > 0) {
if (!interfacesFound.isEmpty()) {
interfacesFound += " | ";
}
interfacesFound += "R";
}
if ((interfaceMask & OCInterfaceMask.B) > 0) {
if (!interfacesFound.isEmpty()) {
interfacesFound += " | ";
}
interfacesFound += "B";
}
if ((interfaceMask & OCInterfaceMask.LL) > 0) {
if (!interfacesFound.isEmpty()) {
interfacesFound += " | ";
}
interfacesFound += "LL";
}
if ((interfaceMask & OCInterfaceMask.BASELINE) > 0) {
if (!interfacesFound.isEmpty()) {
interfacesFound += " | ";
}
interfacesFound += "BASELINE";
}
activity.msg("\tinterfaces: " + interfacesFound);
String propertiesFound = "";
if ((resourcePropertiesMask & OCResourcePropertiesMask.OC_PERIODIC) > 0) {
propertiesFound += "PERIODIC";
}
if ((resourcePropertiesMask & OCResourcePropertiesMask.OC_SECURE) > 0) {
if (!propertiesFound.isEmpty()) {
propertiesFound += " | ";
}
propertiesFound += "SECURE";
}
if ((resourcePropertiesMask & OCResourcePropertiesMask.OC_OBSERVABLE) > 0) {
if (!propertiesFound.isEmpty()) {
propertiesFound += " | ";
}
propertiesFound += "OBSERVABLE";
}
if ((resourcePropertiesMask & OCResourcePropertiesMask.OC_DISCOVERABLE) > 0) {
if (!propertiesFound.isEmpty()) {
propertiesFound += " | ";
}
propertiesFound += "DISCOVERABLE";
}
activity.msg("\tresource properties: " + propertiesFound);
for (String type : types) {
if (type.equals("core.light")) {
light = new Light();
light.serverEndpoint = endpoint;
light.serverUri = uri;
activity.msg("\tResource " + light.serverUri + " hosted at endpoint(s):");
OCEndpoint ep = endpoint;
while (ep != null) {
String endpointStr = OcUtils.endpointToString(ep);
activity.msg("\t\tendpoint: " + endpointStr);
activity.msg("\t\t\tendpoint.device " + ep.getDevice());
activity.msg("\t\t\tendpoint.flags " + ep.getFlags());
activity.msg("\t\t\tendpoint.interfaceIndex " + ep.getInterfaceIndex());
activity.msg("\t\t\tendpoint.version " + ep.getVersion().toString());
ep = ep.getNext();
}
activity.printLine();
GetLightResponseHandler responseHandler = new GetLightResponseHandler(activity, light);
OcUtils.doGet(light.serverUri, light.serverEndpoint, null, responseHandler, OCQos.LOW_QOS);
return OCDiscoveryFlags.OC_STOP_DISCOVERY;
}
}
OcUtils.freeServerEndpoints(endpoint);
return OCDiscoveryFlags.OC_CONTINUE_DISCOVERY;
}
}
package org.iotivity.simpleclient;
import android.util.Log;
import org.iotivity.OCMainInitHandler;
import org.iotivity.oc.OcDevice;
import org.iotivity.oc.OcPlatform;
import org.iotivity.oc.OcUtils;
public class MyInitHandler implements OCMainInitHandler {
private static final String TAG = MyInitHandler.class.getSimpleName();
private ClientActivity activity;
private OcPlatform obtPlatform;
OcDevice device;
public MyInitHandler(ClientActivity activity, OcPlatform obtPlatform) {
this.activity = activity;
this.obtPlatform = obtPlatform;
}
@Override
public int initialize() {
Log.d(TAG, "inside MyInitHandler.initialize()");
int ret = obtPlatform.platformInit("Android");
if (ret >= 0) {
device = new OcDevice("/oic/d", "oic.d.phone", "Kishen's Android Phone", "ocf.1.0.0", "ocf.res.1.0.0");
ret |= obtPlatform.addDevice(device);
}
return ret;
}
@Override
public void registerResources() {
Log.d(TAG, "inside MyInitHandler.registerResources()");
}
@Override
public void requestEntry() {
Log.d(TAG, "inside MyInitHandler.requestEntry()");
MyDiscoveryHandler discoveryHandler = new MyDiscoveryHandler(activity);
OcUtils.doIPDiscovery("core.light", discoveryHandler);
}
}
package org.iotivity.simpleclient;
import org.iotivity.OCClientResponse;
import org.iotivity.OCRepresentation;
import org.iotivity.OCResponseHandler;
public class ObserveLightResponseHandler implements OCResponseHandler {
private static final String TAG = ObserveLightResponseHandler.class.getSimpleName();
private ClientActivity activity;
private Light light;
public ObserveLightResponseHandler(ClientActivity activity, Light light) {
this.activity = activity;
this.light = light;
}
@Override
public void handler(OCClientResponse response) {
activity.msg("OBSERVER Light:");
OCRepresentation rep = response.getPayload();
while (rep != null) {
switch (rep.getType()) {
case OC_REP_BOOL:
activity.msg("\tKey " + rep.getName() + " value " + rep.getValue().getBool());
light.state = rep.getValue().getBool();
break;
case OC_REP_INT:
activity.msg("\tKey " + rep.getName() + " value " + rep.getValue().getInteger());
light.power = rep.getValue().getInteger();
break;
case OC_REP_STRING:
activity.msg("\tKey " + rep.getName() + " value " + rep.getValue().getString());
light.name = rep.getValue().getString();
break;
default:
break;
}
rep = rep.getNext();
}
activity.printLine();
}
}
package org.iotivity.simpleclient;
import org.iotivity.OCClientResponse;
import org.iotivity.OCQos;
import org.iotivity.OCResponseHandler;
import org.iotivity.OCStatus;
import org.iotivity.oc.OcUtils;
public class Post2LightResponseHandler implements OCResponseHandler {
private static final String TAG = Post2LightResponseHandler.class.getSimpleName();
private ClientActivity activity;
private Light light;
public Post2LightResponseHandler(ClientActivity activity, Light light) {
this.activity = activity;
this.light = light;
}
@Override
public void handler(OCClientResponse response) {
activity.msg("POST2 light:");
if (response.getCode() == OCStatus.OC_STATUS_CHANGED) {
activity.msg("\tPOST2 response: CHANGED");
} else if (response.getCode() == OCStatus.OC_STATUS_CREATED) {
activity.msg("\tPOST2 response: CREATED");
} else {
activity.msg("\tPOST2 response code " + response.getCode().toString() + " (" + response.getCode() + ")");