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("icv", "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));
142 msg("\tDevice types:");
144 for (String type : ocRepresentation.getResourceTypes()) {
147 } catch (OcException e) {
148 Log.e(TAG, e.toString());
149 msg("Failed to read device info values.");
155 //******************************************************************************
156 // End of the OIC specific code
157 //******************************************************************************
159 private final static String TAG = DeviceDiscoveryClient.class.getSimpleName();
160 private TextView mConsoleTextView;
161 private ScrollView mScrollView;
164 protected void onCreate(Bundle savedInstanceState) {
165 super.onCreate(savedInstanceState);
166 setContentView(R.layout.activity_device_discovery_client);
168 mConsoleTextView = (TextView) findViewById(R.id.consoleTextView);
169 mConsoleTextView.setMovementMethod(new ScrollingMovementMethod());
170 mScrollView = (ScrollView) findViewById(R.id.scrollView);
171 mScrollView.fullScroll(View.FOCUS_DOWN);
172 final Button button = (Button) findViewById(R.id.button);
174 if (null == savedInstanceState) {
175 button.setOnClickListener(new View.OnClickListener() {
177 public void onClick(View v) {
178 button.setText("Re-start");
179 button.setEnabled(false);
180 new Thread(new Runnable() {
182 startDeviceDiscoveryClient();
188 String consoleOutput = savedInstanceState.getString("consoleOutputString");
189 mConsoleTextView.setText(consoleOutput);
194 protected void onSaveInstanceState(Bundle outState) {
195 super.onSaveInstanceState(outState);
196 outState.putString("consoleOutputString", mConsoleTextView.getText().toString());
200 protected void onRestoreInstanceState(Bundle savedInstanceState) {
201 super.onRestoreInstanceState(savedInstanceState);
203 String consoleOutput = savedInstanceState.getString("consoleOutputString");
204 mConsoleTextView.setText(consoleOutput);
207 private void enableStartButton() {
208 runOnUiThread(new Runnable() {
210 Button button = (Button) findViewById(R.id.button);
211 button.setEnabled(true);
216 private void sleep(int seconds) {
218 Thread.sleep(seconds * 1000);
219 } catch (InterruptedException e) {
221 Log.e(TAG, e.toString());
225 private void msg(final String text) {
226 runOnUiThread(new Runnable() {
228 mConsoleTextView.append("\n");
229 mConsoleTextView.append(text);
230 mScrollView.fullScroll(View.FOCUS_DOWN);
236 private void printLine() {
237 msg("------------------------------------------------------------------------");