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;
44 * This sample demonstrates platform and device discovery feature.
45 * The server sets the platform and device related info. which can be later retrieved by a client.
47 public class DeviceDiscoveryServer extends Activity {
49 private void startDeviceDiscoveryServer() {
50 Context context = this;
52 PlatformConfig platformConfig = new PlatformConfig(
56 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
57 0, // Uses randomly available port
61 msg("Configuring platform.");
62 OcPlatform.Configure(platformConfig);
64 OcDeviceInfo deviceInfo = new OcDeviceInfo("myDeviceName");
66 msg("Registering device info");
67 OcPlatform.registerDeviceInfo(deviceInfo);
68 } catch (OcException e) {
69 Log.e(TAG, e.toString());
70 msg("Failed to register device info.");
73 OcPlatformInfo platformInfo = new OcPlatformInfo(
74 "myPlatformId", //Platform ID
75 "myManufactName", //Manufacturer Name
76 "www.myurl.com", //Manufacturer URL
77 "myModelNumber", //Model Number
78 "myDateOfManufacture", //Date of Manufacture
79 "myPlatformVersion", //Platform Version
80 "Manufacturer OS version", //Operating System Version
81 "myHardwareVersion", //Hardware Version
82 "myFirmwareVersion", //Firmware Version
83 "www.mysupporturl.com", //Support URL
84 String.valueOf(System.currentTimeMillis()) // System Time
87 msg("Registering platform info");
88 OcPlatform.registerPlatformInfo(platformInfo);
89 } catch (OcException e) {
90 Log.e(TAG, e.toString());
91 msg("Failed to register platform info.");
94 msg("Waiting for the requests...");
98 //******************************************************************************
99 // End of the OIC specific code
100 //******************************************************************************
102 private final static String TAG = DeviceDiscoveryServer.class.getSimpleName();
103 private TextView mConsoleTextView;
104 private ScrollView mScrollView;
107 protected void onCreate(Bundle savedInstanceState) {
108 super.onCreate(savedInstanceState);
109 setContentView(R.layout.activity_device_discovery_server);
111 mConsoleTextView = (TextView) findViewById(R.id.consoleTextView);
112 mConsoleTextView.setMovementMethod(new ScrollingMovementMethod());
113 mScrollView = (ScrollView) findViewById(R.id.scrollView);
114 mScrollView.fullScroll(View.FOCUS_DOWN);
115 final Button button = (Button) findViewById(R.id.button);
117 if (null == savedInstanceState) {
118 button.setOnClickListener(new View.OnClickListener() {
120 public void onClick(View v) {
121 button.setEnabled(false);
122 new Thread(new Runnable() {
124 startDeviceDiscoveryServer();
130 String consoleOutput = savedInstanceState.getString("consoleOutputString");
131 mConsoleTextView.setText(consoleOutput);
136 protected void onSaveInstanceState(Bundle outState) {
137 super.onSaveInstanceState(outState);
138 outState.putString("consoleOutputString", mConsoleTextView.getText().toString());
142 protected void onRestoreInstanceState(Bundle savedInstanceState) {
143 super.onRestoreInstanceState(savedInstanceState);
145 String consoleOutput = savedInstanceState.getString("consoleOutputString");
146 mConsoleTextView.setText(consoleOutput);
149 private void msg(final String text) {
150 runOnUiThread(new Runnable() {
152 mConsoleTextView.append("\n");
153 mConsoleTextView.append(text);
154 mScrollView.fullScroll(View.FOCUS_DOWN);
160 private void printLine() {
161 msg("------------------------------------------------------------------------");