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.Button;
31 import android.widget.ScrollView;
32 import android.widget.TextView;
34 import org.iotivity.base.ModeType;
35 import org.iotivity.base.OcConnectivityType;
36 import org.iotivity.base.OcException;
37 import org.iotivity.base.OcPlatform;
38 import org.iotivity.base.OcRepresentation;
39 import org.iotivity.base.PlatformConfig;
40 import org.iotivity.base.QualityOfService;
41 import org.iotivity.base.ServiceType;
43 import java.util.EnumSet;
44 import java.util.HashMap;
48 * This sample demonstrates the device discovery feature.
49 * The client queries for the device related information stored by the server.
51 public class DeviceDiscoveryClient extends Activity implements
52 OcPlatform.OnDeviceFoundListener,
53 OcPlatform.OnPlatformFoundListener {
54 private void startDeviceDiscoveryClient() {
55 Context context = this;
57 PlatformConfig platformConfig = new PlatformConfig(
61 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
62 0, // Uses randomly available port
66 msg("Configuring platform.");
67 OcPlatform.Configure(platformConfig);
71 msg("Querying for platform information...");
72 OcPlatform.getPlatformInfo("",
73 OcPlatform.WELL_KNOWN_PLATFORM_QUERY,
74 EnumSet.of(OcConnectivityType.CT_DEFAULT),
76 } catch (OcException e) {
77 Log.e(TAG, e.toString());
78 msg("Failed to query for platform information");
83 msg("Querying for device information...");
84 OcPlatform.getDeviceInfo("",
85 OcPlatform.WELL_KNOWN_DEVICE_QUERY,
86 EnumSet.of(OcConnectivityType.CT_DEFAULT),
88 } catch (OcException e) {
89 Log.e(TAG, e.toString());
90 msg("Failed to query for device information");
98 private final static Map<String, String> PLATFORM_INFO_KEYS = new HashMap<String, String>() {{
99 put("pi", "Platform ID: ");
100 put("mnmn", "Manufacturer name: ");
101 put("mnml", "Manufacturer url: ");
102 put("mnmo", "Manufacturer Model No: ");
103 put("mndt", "Manufactured Date: ");
104 put("mnpv", "Manufacturer Platform Version: ");
105 put("mnos", "Manufacturer OS version: ");
106 put("mnhw", "Manufacturer hardware version: ");
107 put("mnfv", "Manufacturer firmware version: ");
108 put("mnsl", "Manufacturer support url: ");
109 put("st", "Manufacturer system time: ");
113 public synchronized void onPlatformFound(OcRepresentation ocRepresentation) {
114 msg("Platform Information received:");
116 for (String key : PLATFORM_INFO_KEYS.keySet()) {
117 msg("\t" + PLATFORM_INFO_KEYS.get(key) + ocRepresentation.getValue(key));
119 } catch (OcException e) {
120 Log.e(TAG, e.toString());
121 msg("Failed to read platform info values.");
127 private final static Map<String, String> DEVICE_INFO_KEYS = new HashMap<String, String>() {{
128 put("di", "Device ID: ");
129 put("n", "Device name: ");
130 put("lcv", "Spec version url: ");
131 put("dmv", "Data Model: ");
135 public synchronized void onDeviceFound(OcRepresentation ocRepresentation) {
136 msg("Device Information received:");
138 for (String key : DEVICE_INFO_KEYS.keySet()) {
139 msg("\t" + DEVICE_INFO_KEYS.get(key) + ocRepresentation.getValue(key));
141 } catch (OcException e) {
142 Log.e(TAG, e.toString());
143 msg("Failed to read device info values.");
149 //******************************************************************************
150 // End of the OIC specific code
151 //******************************************************************************
153 private final static String TAG = DeviceDiscoveryClient.class.getSimpleName();
154 private TextView mConsoleTextView;
155 private ScrollView mScrollView;
158 protected void onCreate(Bundle savedInstanceState) {
159 super.onCreate(savedInstanceState);
160 setContentView(R.layout.activity_device_discovery_client);
162 mConsoleTextView = (TextView) findViewById(R.id.consoleTextView);
163 mConsoleTextView.setMovementMethod(new ScrollingMovementMethod());
164 mScrollView = (ScrollView) findViewById(R.id.scrollView);
165 mScrollView.fullScroll(View.FOCUS_DOWN);
166 final Button button = (Button) findViewById(R.id.button);
168 if (null == savedInstanceState) {
169 button.setOnClickListener(new View.OnClickListener() {
171 public void onClick(View v) {
172 button.setText("Re-start");
173 button.setEnabled(false);
174 new Thread(new Runnable() {
176 startDeviceDiscoveryClient();
182 String consoleOutput = savedInstanceState.getString("consoleOutputString");
183 mConsoleTextView.setText(consoleOutput);
188 protected void onSaveInstanceState(Bundle outState) {
189 super.onSaveInstanceState(outState);
190 outState.putString("consoleOutputString", mConsoleTextView.getText().toString());
194 protected void onRestoreInstanceState(Bundle savedInstanceState) {
195 super.onRestoreInstanceState(savedInstanceState);
197 String consoleOutput = savedInstanceState.getString("consoleOutputString");
198 mConsoleTextView.setText(consoleOutput);
201 private void enableStartButton() {
202 runOnUiThread(new Runnable() {
204 Button button = (Button) findViewById(R.id.button);
205 button.setEnabled(true);
210 private void sleep(int seconds) {
212 Thread.sleep(seconds * 1000);
213 } catch (InterruptedException e) {
215 Log.e(TAG, e.toString());
219 private void msg(final String text) {
220 runOnUiThread(new Runnable() {
222 mConsoleTextView.append("\n");
223 mConsoleTextView.append(text);
224 mScrollView.fullScroll(View.FOCUS_DOWN);
230 private void printLine() {
231 msg("------------------------------------------------------------------------");