1 //******************************************************************
3 // Copyright 2014 MediaTek All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 package org.iotivity.simpleclient;
23 import android.app.Activity;
24 import android.os.Bundle;
25 import android.util.Log;
26 import android.view.Menu;
27 import android.view.MenuItem;
29 import org.iotivity.base.OCPlatform;
30 import org.iotivity.base.OCRepresentation;
31 import org.iotivity.base.OCResource;
32 import org.iotivity.base.ObserveType;
33 import org.iotivity.base.PlatformConfig;
36 public class SimpleClient extends Activity {
37 final private static String TAG = "SimpleClient";
40 public boolean m_state;
51 static int OBSERVE_TYPE_TO_USE = ObserveType.Observe;
53 public static Light mylight;
54 public static OCResource curResource;
56 static Activity mActivity;
59 protected void onCreate(Bundle savedInstanceState) {
60 super.onCreate(savedInstanceState);
63 mylight = new Light();
65 PlatformConfig cfg = new PlatformConfig(PlatformConfig.ServiceType.INPROC,
66 PlatformConfig.ModeType.CLIENT,
69 PlatformConfig.QualityOfService.LO_QOS);
71 OCPlatform.configure(cfg);
72 FoundResource foundResource = new FoundResource();
73 OCPlatform.findResource("", "coap://224.0.1.187/oc/core?rt=core.light", foundResource);
75 setContentView(R.layout.activity_simple_client);
80 public boolean onCreateOptionsMenu(Menu menu) {
81 // Inflate the menu; this adds items to the action bar if it is present.
82 getMenuInflater().inflate(R.menu.simple_client, menu);
87 public boolean onOptionsItemSelected(MenuItem item) {
88 // Handle action bar item clicks here. The action bar will
89 // automatically handle clicks on the Home/Up button, so long
90 // as you specify a parent activity in AndroidManifest.xml.
91 int id = item.getItemId();
92 if (id == R.id.action_settings) {
95 return super.onOptionsItemSelected(item);
98 protected static void getLightRepresentation(OCResource resource) {
99 if(resource != null) {
100 Log.e(TAG, "Getting Light Representation ...");
102 OnGet onGet = new OnGet();
107 protected static void putLightRepresentation(OCResource resource) {
108 if(resource != null) {
110 OCRepresentation rep = new OCRepresentation();
112 Log.e(TAG, "Putting Light Representation ...");
114 mylight.m_state = true;
115 mylight.m_power = 15;
117 Log.i(TAG, "setValueBool : " + SimpleClient.mylight.m_state);
118 Log.i(TAG, "setValueInt : " + SimpleClient.mylight.m_power);
119 rep.setValueBool("state", mylight.m_state);
120 rep.setValueInt("power", mylight.m_power);
122 OnPut onPut = new OnPut();
123 resource.put(rep, onPut);
127 protected static void postLightRepresentation(OCResource resource) {
128 if(resource != null) {
130 OCRepresentation rep = new OCRepresentation();
132 Log.e(TAG, "Posting Light Representation ...");
134 mylight.m_state = false;
135 mylight.m_power = 105;
137 Log.i(TAG, "setValueBool : " + SimpleClient.mylight.m_state);
138 Log.i(TAG, "setValueInt : " + SimpleClient.mylight.m_power);
139 rep.setValueBool("state", mylight.m_state);
140 rep.setValueInt("power", mylight.m_power);
142 OnPost onPost = new OnPost();
143 resource.post(rep, onPost);
147 protected static int observe_count() {