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 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 package org.iotivity.base.examples;
25 import android.app.Activity;
26 import android.content.BroadcastReceiver;
27 import android.content.Context;
28 import android.content.Intent;
29 import android.content.IntentFilter;
30 import android.nfc.NfcAdapter;
31 import android.os.Bundle;
32 import android.text.method.ScrollingMovementMethod;
33 import android.util.Log;
34 import android.view.View;
35 import android.widget.CompoundButton;
36 import android.widget.ScrollView;
37 import android.widget.TextView;
38 import android.widget.ToggleButton;
40 import org.iotivity.base.ModeType;
41 import org.iotivity.base.OcException;
42 import org.iotivity.base.OcPlatform;
43 import org.iotivity.base.PlatformConfig;
44 import org.iotivity.base.QualityOfService;
45 import org.iotivity.base.ServiceType;
47 import java.util.LinkedList;
48 import java.util.List;
53 * SimpleServer is a sample OIC server application.
54 * It creates a Light and waits for the incoming client calls to handle
55 * various request scenarios.
57 public class SimpleServer extends Activity {
59 List<Light> lights = new LinkedList<>();
62 * A local method to configure and initialize platform, and then create a light resource.
64 private void startSimpleServer() {
65 Context context = this;
67 PlatformConfig platformConfig = new PlatformConfig(
71 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
72 0, // Uses randomly available port
76 msg("Configuring platform.");
77 OcPlatform.Configure(platformConfig);
79 createNewLightResource("/a/light", "John's light");
81 msg("Waiting for the requests...");
84 enableStartStopButton();
87 public void createNewLightResource(String resourceUri, String resourceName){
88 msg("Creating a light");
89 Light light = new Light(
95 msg(light.toString());
96 light.setContext(this);
98 msg("Registering light as a resource");
100 light.registerResource();
101 } catch (OcException e) {
102 Log.e(TAG, e.toString());
103 msg("Failed to register a light resource");
108 private void stopSimpleServer() {
109 for (Light light : lights) {
111 light.unregisterResource();
112 } catch (OcException e) {
113 Log.e(TAG, e.toString());
114 msg("Failed to unregister a light resource");
119 msg("All created resources have been unregistered");
121 enableStartStopButton();
124 //******************************************************************************
125 // End of the OIC specific code
126 //******************************************************************************
128 private final static String TAG = SimpleServer.class.getSimpleName();
129 private MessageReceiver mMessageReceiver = new MessageReceiver();
130 private TextView mConsoleTextView;
131 private ScrollView mScrollView;
134 protected void onCreate(Bundle savedInstanceState) {
135 super.onCreate(savedInstanceState);
136 setContentView(R.layout.activity_simple_server);
138 registerReceiver(mMessageReceiver,
139 new IntentFilter("org.iotivity.base.examples.simpleserver"));
141 mConsoleTextView = (TextView) findViewById(R.id.consoleTextView);
142 mConsoleTextView.setMovementMethod(new ScrollingMovementMethod());
143 mScrollView = (ScrollView) findViewById(R.id.scrollView);
144 mScrollView.fullScroll(View.FOCUS_DOWN);
145 final ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
147 if (null == savedInstanceState) {
148 toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
149 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
150 toggleButton.setEnabled(false);
152 new Thread(new Runnable() {
158 new Thread(new Runnable() {
167 String consoleOutput = savedInstanceState.getString("consoleOutputString");
168 mConsoleTextView.setText(consoleOutput);
169 boolean buttonCheked = savedInstanceState.getBoolean("toggleButtonChecked");
170 toggleButton.setChecked(buttonCheked);
175 public void onDestroy() {
181 protected void onStop() {
182 //unregisterReceiver(mMessageReceiver);
187 protected void onSaveInstanceState(Bundle outState) {
188 super.onSaveInstanceState(outState);
189 outState.putString("consoleOutputString", mConsoleTextView.getText().toString());
190 ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
191 outState.putBoolean("toggleButtonChecked", toggleButton.isChecked());
195 protected void onRestoreInstanceState(Bundle savedInstanceState) {
196 super.onRestoreInstanceState(savedInstanceState);
198 String consoleOutput = savedInstanceState.getString("consoleOutputString");
199 mConsoleTextView.setText(consoleOutput);
201 final ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
202 boolean buttonCheked = savedInstanceState.getBoolean("toggleButtonChecked");
203 toggleButton.setChecked(buttonCheked);
206 private void msg(final String text) {
207 runOnUiThread(new Runnable() {
209 mConsoleTextView.append("\n");
210 mConsoleTextView.append(text);
211 mScrollView.fullScroll(View.FOCUS_DOWN);
217 private void printLine() {
218 msg("------------------------------------------------------------------------");
221 private void sleep(int seconds) {
223 Thread.sleep(seconds * 1000);
224 } catch (InterruptedException e) {
226 Log.e(TAG, e.toString());
230 private void enableStartStopButton() {
231 runOnUiThread(new Runnable() {
233 ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
234 toggleButton.setEnabled(true);
239 public class MessageReceiver extends BroadcastReceiver {
241 public void onReceive(Context context, Intent intent) {
242 final String message = intent.getStringExtra("message");
248 public void onNewIntent(Intent intent) {
249 super.onNewIntent(intent);
250 Log.d(TAG, "onNewIntent with changes sending broadcast IN ");
252 Intent i = new Intent();
253 i.setAction(intent.getAction());
254 i.putExtra(NfcAdapter.EXTRA_NDEF_MESSAGES,
255 intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES));
257 Log.d(TAG, "Initialize Context again resetting");