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.OcConnectivityType;
37 import org.iotivity.base.OcException;
38 import org.iotivity.base.OcPlatform;
39 import org.iotivity.base.OcPresenceHandle;
40 import org.iotivity.base.OcPresenceStatus;
41 import org.iotivity.base.OcResource;
42 import org.iotivity.base.PlatformConfig;
43 import org.iotivity.base.QualityOfService;
44 import org.iotivity.base.ServiceType;
46 import java.util.EnumSet;
49 * A client example for presence notification
51 public class PresenceClient extends Activity implements
52 OcPlatform.OnResourceFoundListener,
53 OcPlatform.OnPresenceListener {
54 private final static String TAG = PresenceClient.class.getSimpleName();
55 private OcResource mResource;
56 private OcPresenceHandle mPresenceHandle;
57 private TextView mConsoleTextView;
58 private ScrollView mScrollView;
60 private void startPresenceClient() {
61 Context context = this;
63 PlatformConfig platformConfig = new PlatformConfig(
67 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
68 0, // Uses randomly available port
72 msg("Configuring platform.");
73 OcPlatform.Configure(platformConfig);
76 msg("Finding Resource...");
77 OcPlatform.findResource("", OcPlatform.WELL_KNOWN_QUERY,
78 EnumSet.of(OcConnectivityType.CT_DEFAULT), this);
79 } catch (OcException e) {
80 Log.e(TAG, e.toString());
81 msg("Failed to find resource(s). ");
84 enableStartStopButton();
87 //******************************************************************************
88 // End of the OIC specific code
89 //******************************************************************************
92 public synchronized void onResourceFound(OcResource foundResource) {
93 String resourceUri = foundResource.getUri();
94 if (null != mResource || !resourceUri.equals("/a/light")) {
95 msg("Found resource, ignoring");
99 msg("Found Resource");
100 String hostAddress = foundResource.getHost();
101 msg("\tResource URI : " + resourceUri);
102 msg("\tResource Host : " + hostAddress);
103 msg("\tResource Interfaces : ");
104 for (String resInterface : foundResource.getResourceInterfaces()) {
105 msg("\t\t" + resInterface);
107 msg("\tResource Type : ");
108 for (String resTypes : foundResource.getResourceTypes()) {
109 msg("\t\t" + resTypes);
113 msg("Subscribing to unicast address:" + hostAddress);
114 OcPlatform.subscribePresence(hostAddress,
115 EnumSet.of(OcConnectivityType.CT_DEFAULT), this);
116 } catch (OcException e) {
117 Log.e(TAG, e.toString());
118 msg("Failed to subscribe to unicast address:" + hostAddress);
120 mResource = foundResource;
125 public void onPresence(OcPresenceStatus ocPresenceStatus, int nonce, String hostAddress) {
126 msg("Received presence notification from : " + hostAddress);
127 switch (ocPresenceStatus) {
129 msg("Nonce# " + nonce);
132 msg("Presence Stopped");
135 msg("Presence Timeout");
138 msg("Presence Do Not Handle");
143 private void stopPresenceClient() {
144 if (null != mPresenceHandle) {
146 msg("Unsubscribing from the presence notifications.");
147 OcPlatform.unsubscribePresence(mPresenceHandle);
148 mPresenceHandle = null;
149 } catch (OcException e) {
150 Log.e(TAG, e.toString());
151 msg("Failed to unsubscribe from the presence notifications" + e.toString());
156 enableStartStopButton();
160 protected void onCreate(Bundle savedInstanceState) {
161 super.onCreate(savedInstanceState);
162 setContentView(R.layout.activity_presence_client);
164 mConsoleTextView = (TextView) findViewById(R.id.consoleTextView);
165 mConsoleTextView.setMovementMethod(new ScrollingMovementMethod());
166 mScrollView = (ScrollView) findViewById(R.id.scrollView);
167 mScrollView.fullScroll(View.FOCUS_DOWN);
168 final ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
170 if (null == savedInstanceState) {
171 toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
172 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
173 toggleButton.setEnabled(false);
175 new Thread(new Runnable() {
177 startPresenceClient();
181 new Thread(new Runnable() {
183 stopPresenceClient();
190 String consoleOutput = savedInstanceState.getString("consoleOutputString");
191 mConsoleTextView.setText(consoleOutput);
192 boolean buttonCheked = savedInstanceState.getBoolean("toggleButtonChecked");
193 toggleButton.setChecked(buttonCheked);
198 protected void onSaveInstanceState(Bundle outState) {
199 super.onSaveInstanceState(outState);
200 outState.putString("consoleOutputString", mConsoleTextView.getText().toString());
201 ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
202 outState.putBoolean("toggleButtonChecked", toggleButton.isChecked());
206 protected void onRestoreInstanceState(Bundle savedInstanceState) {
207 super.onRestoreInstanceState(savedInstanceState);
209 String consoleOutput = savedInstanceState.getString("consoleOutputString");
210 mConsoleTextView.setText(consoleOutput);
212 final ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
213 boolean buttonCheked = savedInstanceState.getBoolean("toggleButtonChecked");
214 toggleButton.setChecked(buttonCheked);
217 private void msg(final String text) {
218 runOnUiThread(new Runnable() {
220 mConsoleTextView.append("\n");
221 mConsoleTextView.append(text);
222 mScrollView.fullScroll(View.FOCUS_DOWN);
228 private void printLine() {
229 msg("------------------------------------------------------------------------");
232 private void enableStartStopButton() {
233 runOnUiThread(new Runnable() {
235 ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
236 toggleButton.setEnabled(true);