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(
72 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
73 0, // Uses randomly available port
77 msg("Configuring platform.");
78 OcPlatform.Configure(platformConfig);
80 createNewLightResource("/a/light", "John's light");
82 msg("Waiting for the requests...");
85 enableStartStopButton();
88 public void createNewLightResource(String resourceUri, String resourceName){
89 msg("Creating a light");
90 Light light = new Light(
96 msg(light.toString());
97 light.setContext(this);
99 msg("Registering light as a resource");
101 light.registerResource();
102 } catch (OcException e) {
103 Log.e(TAG, e.toString());
104 msg("Failed to register a light resource");
109 private void stopSimpleServer() {
110 for (Light light : lights) {
112 light.unregisterResource();
113 } catch (OcException e) {
114 Log.e(TAG, e.toString());
115 msg("Failed to unregister a light resource");
120 msg("All created resources have been unregistered");
122 enableStartStopButton();
125 //******************************************************************************
126 // End of the OIC specific code
127 //******************************************************************************
129 private final static String TAG = SimpleServer.class.getSimpleName();
130 private MessageReceiver mMessageReceiver = new MessageReceiver();
131 private TextView mConsoleTextView;
132 private ScrollView mScrollView;
135 protected void onCreate(Bundle savedInstanceState) {
136 super.onCreate(savedInstanceState);
137 setContentView(R.layout.activity_simple_server);
139 registerReceiver(mMessageReceiver,
140 new IntentFilter("org.iotivity.base.examples.simpleserver"));
142 mConsoleTextView = (TextView) findViewById(R.id.consoleTextView);
143 mConsoleTextView.setMovementMethod(new ScrollingMovementMethod());
144 mScrollView = (ScrollView) findViewById(R.id.scrollView);
145 mScrollView.fullScroll(View.FOCUS_DOWN);
146 final ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
148 if (null == savedInstanceState) {
149 toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
150 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
151 toggleButton.setEnabled(false);
153 new Thread(new Runnable() {
159 new Thread(new Runnable() {
168 String consoleOutput = savedInstanceState.getString("consoleOutputString");
169 mConsoleTextView.setText(consoleOutput);
170 boolean buttonCheked = savedInstanceState.getBoolean("toggleButtonChecked");
171 toggleButton.setChecked(buttonCheked);
176 public void onDestroy() {
182 protected void onStop() {
183 //unregisterReceiver(mMessageReceiver);
188 protected void onSaveInstanceState(Bundle outState) {
189 super.onSaveInstanceState(outState);
190 outState.putString("consoleOutputString", mConsoleTextView.getText().toString());
191 ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
192 outState.putBoolean("toggleButtonChecked", toggleButton.isChecked());
196 protected void onRestoreInstanceState(Bundle savedInstanceState) {
197 super.onRestoreInstanceState(savedInstanceState);
199 String consoleOutput = savedInstanceState.getString("consoleOutputString");
200 mConsoleTextView.setText(consoleOutput);
202 final ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
203 boolean buttonCheked = savedInstanceState.getBoolean("toggleButtonChecked");
204 toggleButton.setChecked(buttonCheked);
207 private void msg(final String text) {
208 runOnUiThread(new Runnable() {
210 mConsoleTextView.append("\n");
211 mConsoleTextView.append(text);
212 mScrollView.fullScroll(View.FOCUS_DOWN);
218 private void printLine() {
219 msg("------------------------------------------------------------------------");
222 private void sleep(int seconds) {
224 Thread.sleep(seconds * 1000);
225 } catch (InterruptedException e) {
227 Log.e(TAG, e.toString());
231 private void enableStartStopButton() {
232 runOnUiThread(new Runnable() {
234 ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
235 toggleButton.setEnabled(true);
240 public class MessageReceiver extends BroadcastReceiver {
242 public void onReceive(Context context, Intent intent) {
243 final String message = intent.getStringExtra("message");
249 public void onNewIntent(Intent intent) {
250 super.onNewIntent(intent);
251 Log.d(TAG, "onNewIntent with changes sending broadcast IN ");
253 Intent i = new Intent();
254 i.setAction(intent.getAction());
255 i.putExtra(NfcAdapter.EXTRA_NDEF_MESSAGES,
256 intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES));
258 Log.d(TAG, "Initialize Context again resetting");