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.OcDeviceInfo;
36 import org.iotivity.base.OcException;
37 import org.iotivity.base.OcPlatform;
38 import org.iotivity.base.OcPlatformInfo;
39 import org.iotivity.base.PlatformConfig;
40 import org.iotivity.base.QualityOfService;
41 import org.iotivity.base.ServiceType;
43 import java.util.Arrays;
46 * This sample demonstrates platform and device discovery feature.
47 * The server sets the platform and device related info. which can be later retrieved by a client.
49 public class DeviceDiscoveryServer extends Activity {
51 private void startDeviceDiscoveryServer() {
52 Context context = this;
54 PlatformConfig platformConfig = new PlatformConfig(
58 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
59 0, // Uses randomly available port
63 msg("Configuring platform.");
64 OcPlatform.Configure(platformConfig);
66 OcDeviceInfo deviceInfo = new OcDeviceInfo(
68 Arrays.asList(new String[]{"oic.d.phone"})
72 msg("Registering device info");
73 OcPlatform.registerDeviceInfo(deviceInfo);
74 } catch (OcException e) {
75 Log.e(TAG, e.toString());
76 msg("Failed to register device info.");
79 OcPlatformInfo platformInfo = new OcPlatformInfo(
80 "myPlatformId", //Platform ID
81 "myManufactName", //Manufacturer Name
82 "www.myurl.com", //Manufacturer URL
83 "myModelNumber", //Model Number
84 "myDateOfManufacture", //Date of Manufacture
85 "myPlatformVersion", //Platform Version
86 "Manufacturer OS version", //Operating System Version
87 "myHardwareVersion", //Hardware Version
88 "myFirmwareVersion", //Firmware Version
89 "www.mysupporturl.com", //Support URL
90 String.valueOf(System.currentTimeMillis()) // System Time
93 msg("Registering platform info");
94 OcPlatform.registerPlatformInfo(platformInfo);
95 } catch (OcException e) {
96 Log.e(TAG, e.toString());
97 msg("Failed to register platform info.");
100 msg("Waiting for the requests...");
104 //******************************************************************************
105 // End of the OIC specific code
106 //******************************************************************************
108 private final static String TAG = DeviceDiscoveryServer.class.getSimpleName();
109 private TextView mConsoleTextView;
110 private ScrollView mScrollView;
113 protected void onCreate(Bundle savedInstanceState) {
114 super.onCreate(savedInstanceState);
115 setContentView(R.layout.activity_device_discovery_server);
117 mConsoleTextView = (TextView) findViewById(R.id.consoleTextView);
118 mConsoleTextView.setMovementMethod(new ScrollingMovementMethod());
119 mScrollView = (ScrollView) findViewById(R.id.scrollView);
120 mScrollView.fullScroll(View.FOCUS_DOWN);
121 final Button button = (Button) findViewById(R.id.button);
123 if (null == savedInstanceState) {
124 button.setOnClickListener(new View.OnClickListener() {
126 public void onClick(View v) {
127 button.setEnabled(false);
128 new Thread(new Runnable() {
130 startDeviceDiscoveryServer();
136 String consoleOutput = savedInstanceState.getString("consoleOutputString");
137 mConsoleTextView.setText(consoleOutput);
142 protected void onSaveInstanceState(Bundle outState) {
143 super.onSaveInstanceState(outState);
144 outState.putString("consoleOutputString", mConsoleTextView.getText().toString());
148 protected void onRestoreInstanceState(Bundle savedInstanceState) {
149 super.onRestoreInstanceState(savedInstanceState);
151 String consoleOutput = savedInstanceState.getString("consoleOutputString");
152 mConsoleTextView.setText(consoleOutput);
155 private void msg(final String text) {
156 runOnUiThread(new Runnable() {
158 mConsoleTextView.append("\n");
159 mConsoleTextView.append(text);
160 mScrollView.fullScroll(View.FOCUS_DOWN);
166 private void printLine() {
167 msg("------------------------------------------------------------------------");