1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20 package com.example.resourcehostingsampleapp;
22 import java.lang.reflect.Method;
23 import java.net.Inet4Address;
24 import java.net.InetAddress;
25 import java.net.NetworkInterface;
26 import java.net.SocketException;
27 import java.util.Enumeration;
29 import org.iotivity.ResourceHosting.ResourceHosting;
30 import org.iotivity.base.ModeType;
31 import org.iotivity.base.OcPlatform;
32 import org.iotivity.base.PlatformConfig;
33 import org.iotivity.base.QualityOfService;
34 import org.iotivity.base.ServiceType;
36 import android.app.Activity;
37 import android.os.Bundle;
38 import android.util.Log;
39 import android.view.View;
40 import android.view.View.OnClickListener;
41 import android.widget.TextView;
42 import android.widget.Toast;
45 * To execute resource hosting function for android sample application .
47 * @author Copyright 2015 Samsung Electronics All Rights Reserved.
48 * @see className class : ResourceHosting</br>
52 public class ResourceHostingSampleApp extends Activity implements
55 private String TAG = "ResourceHosting";
56 private String mIpAddress;
57 private TextView mLogTextView;
58 private String mLog = "";
59 private ResourceHosting resourceHosting;
62 * To initialize UI Function Setting.
65 * com_example_resourcehostingsampleapp_ResourceHosting</br>
66 * @see Method method : onCreate</br>
68 protected void onCreate(Bundle savedInstanceState) {
69 super.onCreate(savedInstanceState);
70 setContentView(R.layout.activity_main);
71 mLogTextView = (TextView) findViewById(R.id.txtLog);
72 findViewById(R.id.btnStartHosting).setOnClickListener(this);
73 findViewById(R.id.btnStopHosting).setOnClickListener(this);
74 findViewById(R.id.btLogClear).setOnClickListener(this);
76 PlatformConfig platformConfigObj;
77 resourceHosting = new ResourceHosting();
78 platformConfigObj = new PlatformConfig(this, ServiceType.IN_PROC,
79 ModeType.CLIENT_SERVER, "0.0.0.0", 0, QualityOfService.LOW);
81 Log.i(TAG, "Before Calling Configure of ocPlatform");
82 OcPlatform.Configure(platformConfigObj);
83 Log.i(TAG, "Configuration done Successfully");
87 * To execute initOICStack for running Resource hosting.
90 * com_example_resourcehostingsampleapp_ResourceHosting</br>
91 * @see Method method : onStart</br>
94 protected void onStart() {
100 * To terminate Resource hosting.
103 * com_example_resourcehostingsampleapp_ResourceHosting</br>
104 * @see Method method : onStop</br>
107 protected void onStop() {
110 result = resourceHosting.ResourceHostingTerminate();
111 Log.d(TAG, "ResourceHostingTerminate : " + result);
114 protected void onResume() {
119 * To execute initOICStack for running Resource hosting.
122 * com_example_resourcehostingsampleapp_ResourceHosting</br>
123 * @see Method method : onRestart</br>
126 protected void onRestart() {
133 * com_example_resourcehostingsampleapp_ResourceHosting</br>
134 * @see Method method : onDestroy</br>
136 protected void onDestroy() {
141 * get IpAddress and execute resourceHostingInit() method.
144 * com_example_resourcehostingsampleapp_ResourceHosting</br>
145 * @see Method method : initOICStack</br>
147 private void initOICStack() {
149 mIpAddress = getIpAddress();
151 result = resourceHosting.ResourceHostingInit(mIpAddress);
152 Log.d(TAG, "ResourceHostingInit : " + result);
153 } catch (Exception e) {
160 * com_example_resourcehostingsampleapp_ResourceHosting</br>
161 * @see Method method : getIpAddress</br>
163 private String getIpAddress() {
165 for (Enumeration<NetworkInterface> en = NetworkInterface
166 .getNetworkInterfaces(); en.hasMoreElements();) {
167 NetworkInterface intf = (NetworkInterface) en.nextElement();
168 for (Enumeration<InetAddress> enumIpAddr = intf
169 .getInetAddresses(); enumIpAddr.hasMoreElements();) {
170 InetAddress inetAddress = (InetAddress) enumIpAddr
172 if (!inetAddress.isLoopbackAddress()) {
173 if (inetAddress instanceof Inet4Address)
174 return inetAddress.getHostAddress().toString();
178 } catch (SocketException e) {
186 * com_example_resourcehostingsampleapp_ResourceHosting</br>
187 * @see Method method : onClick</br>
191 public void onClick(View v) {
192 int getId = v.getId();
195 case R.id.btnStartHosting:
198 result = resourceHosting.OICCoordinatorStart();
199 Log.d(TAG, "OICCoordinatorStart : " + result);
200 } catch (Exception e) {
201 Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT)
205 case R.id.btnStopHosting:
207 result = resourceHosting.OICCoordinatorStop();
208 Log.d(TAG, "OICCoordinatorStop : " + result);
210 case R.id.btLogClear: