2 *******************************************************************
4 * Copyright 2015 Intel Corporation.
6 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 package org.iotivity.base.examples;
24 import android.app.Activity;
25 import android.content.Context;
26 import android.os.Bundle;
27 import android.text.method.ScrollingMovementMethod;
28 import android.util.Log;
29 import android.view.View;
30 import android.widget.CompoundButton;
31 import android.widget.ScrollView;
32 import android.widget.TextView;
33 import android.widget.ToggleButton;
35 import org.iotivity.base.ModeType;
36 import org.iotivity.base.OcException;
37 import org.iotivity.base.OcPlatform;
38 import org.iotivity.base.OcResourceHandle;
39 import org.iotivity.base.PlatformConfig;
40 import org.iotivity.base.QualityOfService;
41 import org.iotivity.base.ResourceProperty;
42 import org.iotivity.base.ServiceType;
44 import java.util.EnumSet;
47 * A server example for presence notification
49 public class PresenceServer extends Activity {
50 private OcResourceHandle mResourceHandle;
52 private void startPresenceServer() {
53 Context context = this;
55 PlatformConfig platformConfig = new PlatformConfig(
59 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
60 0, // Uses randomly available port
64 msg("Configuring platform.");
65 OcPlatform.Configure(platformConfig);
68 msg("Creating resource of type \"core.light\".");
72 msg("Starting presence notifications.");
73 OcPlatform.startPresence(OcPlatform.DEFAULT_PRESENCE_TTL);
75 } catch (OcException e) {
76 Log.e(TAG, e.toString());
77 msg("Error: " + e.toString());
80 enableStartStopButton();
84 * This function internally calls registerResource API.
86 private void createResource() {
87 String resourceUri = "/a/light"; // URI of the resource
88 String resourceTypeName = "core.light"; // resource type name.
89 String resourceInterface = OcPlatform.DEFAULT_INTERFACE; // resource interface.
92 // This will internally create and register the resource.
93 mResourceHandle = OcPlatform.registerResource(
97 null, //Use default entity handler
98 EnumSet.of(ResourceProperty.DISCOVERABLE));
99 } catch (OcException e) {
100 msg("Resource creation was unsuccessful.");
101 Log.e(TAG, e.toString());
105 private void stopPresenceServer() {
107 msg("Stopping presence notifications.");
108 OcPlatform.stopPresence();
110 msg("Unregister resource.");
111 if (null != mResourceHandle) OcPlatform.unregisterResource(mResourceHandle);
112 } catch (OcException e) {
113 Log.e(TAG, e.toString());
114 msg("Error: " + e.toString());
118 enableStartStopButton();
121 //******************************************************************************
122 // End of the OIC specific code
123 //******************************************************************************
125 private final static String TAG = PresenceServer.class.getSimpleName();
126 private TextView mConsoleTextView;
127 private ScrollView mScrollView;
130 protected void onCreate(Bundle savedInstanceState) {
131 super.onCreate(savedInstanceState);
132 setContentView(R.layout.activity_presence_server);
134 mConsoleTextView = (TextView) findViewById(R.id.consoleTextView);
135 mConsoleTextView.setMovementMethod(new ScrollingMovementMethod());
136 mScrollView = (ScrollView) findViewById(R.id.scrollView);
137 mScrollView.fullScroll(View.FOCUS_DOWN);
138 final ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
140 if (null == savedInstanceState) {
141 toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
142 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
143 toggleButton.setEnabled(false);
145 new Thread(new Runnable() {
147 startPresenceServer();
151 new Thread(new Runnable() {
153 stopPresenceServer();
160 String consoleOutput = savedInstanceState.getString("consoleOutputString");
161 mConsoleTextView.setText(consoleOutput);
162 boolean buttonCheked = savedInstanceState.getBoolean("toggleButtonChecked");
163 toggleButton.setChecked(buttonCheked);
168 protected void onSaveInstanceState(Bundle outState) {
169 super.onSaveInstanceState(outState);
170 outState.putString("consoleOutputString", mConsoleTextView.getText().toString());
171 ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
172 outState.putBoolean("toggleButtonChecked", toggleButton.isChecked());
176 protected void onRestoreInstanceState(Bundle savedInstanceState) {
177 super.onRestoreInstanceState(savedInstanceState);
179 String consoleOutput = savedInstanceState.getString("consoleOutputString");
180 mConsoleTextView.setText(consoleOutput);
182 final ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
183 boolean buttonCheked = savedInstanceState.getBoolean("toggleButtonChecked");
184 toggleButton.setChecked(buttonCheked);
187 private void msg(final String text) {
188 runOnUiThread(new Runnable() {
190 mConsoleTextView.append("\n");
191 mConsoleTextView.append(text);
192 mScrollView.fullScroll(View.FOCUS_DOWN);
198 private void printLine() {
199 msg("------------------------------------------------------------------------");
202 private void sleep(int seconds) {
204 Thread.sleep(seconds * 1000);
205 } catch (InterruptedException e) {
207 Log.e(TAG, e.toString());
211 private void enableStartStopButton() {
212 runOnUiThread(new Runnable() {
214 ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
215 toggleButton.setEnabled(true);