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.OcResourceHandle;
33 import org.iotivity.base.PlatformConfig;
34 import org.iotivity.base.QualityOfService;
35 import org.iotivity.base.ServiceType;
37 import android.app.Activity;
38 import android.os.Bundle;
39 import android.util.Log;
40 import android.view.View;
41 import android.view.View.OnClickListener;
42 import android.widget.TextView;
43 import android.widget.Toast;
46 * 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 OnClickListener
54 private final int OCSTACK_OK = 0;
55 private final int OCSTACK_ERROR = 255;
56 private final int RESOURCEHOSTING_DO_NOT_THREADRUNNING = -2;
58 private String TAG = "ResourceHosting";
59 private OcResourceHandle mResourceHandle;
60 private String mIpAddress;
61 private TextView mLogTextView;
62 private String mLog = "";
63 private ResourceHosting resourceHosting;
65 * To initialize UI Function Setting.
66 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
67 * @see Method method : onCreate</br>
69 protected void onCreate(Bundle savedInstanceState)
71 super.onCreate(savedInstanceState);
72 setContentView(R.layout.activity_main);
73 mLogTextView = (TextView) findViewById(R.id.txtLog);
74 findViewById(R.id.btnStartHosting).setOnClickListener(this);
75 findViewById(R.id.btnStopHosting).setOnClickListener(this);
76 findViewById(R.id.btLogClear).setOnClickListener(this);
78 PlatformConfig platformConfigObj;
79 resourceHosting = new ResourceHosting();
80 platformConfigObj = new PlatformConfig(this,ServiceType.IN_PROC,
81 ModeType.CLIENT_SERVER, "0.0.0.0", 0, QualityOfService.LOW);
83 Log.i(TAG, "Before Calling Configure of ocPlatform");
84 OcPlatform.Configure(platformConfigObj);
85 Log.i(TAG, "Configuration done Successfully");
89 * To execute initOICStack for running Resource hosting.
90 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
91 * @see Method method : onStart</br>
94 protected void onStart()
101 * To terminate Resource hosting.
102 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
103 * @see Method method : onStop</br>
106 protected void onStop()
110 result = resourceHosting.ResourceHostingTerminate();
111 Log.d(TAG, "ResourceHostingTerminate : "+ result);
114 protected void onResume()
120 * To execute initOICStack for running Resource hosting.
121 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
122 * @see Method method : onRestart</br>
125 protected void onRestart()
132 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
133 * @see Method method : onDestroy</br>
135 protected void onDestroy()
141 * get IpAddress and execute resourceHostingInit() method.
142 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
143 * @see Method method : initOICStack</br>
145 private void initOICStack()
149 mIpAddress = getIpAddress();
151 result = resourceHosting.ResourceHostingInit(mIpAddress);
152 Log.d(TAG, "ResourceHostingInit : " + result);
161 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
162 * @see Method method : getIpAddress</br>
164 private String getIpAddress()
168 for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
169 en.hasMoreElements();)
171 NetworkInterface intf = (NetworkInterface) en.nextElement();
172 for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();)
174 InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
175 if (!inetAddress.isLoopbackAddress())
177 if (inetAddress instanceof Inet4Address)
178 return inetAddress.getHostAddress().toString();
183 catch (SocketException e)
191 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
192 * @see Method method : onClick</br>
193 * @param v view to choice
195 public void onClick(View v)
197 int getId = v.getId();
201 case R.id.btnStartHosting:
205 result = resourceHosting.OICCoordinatorStart();
206 Log.d(TAG, "OICCoordinatorStart : " + result);
210 Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
213 case R.id.btnStopHosting:
215 result = resourceHosting.OICCoordinatorStop();
216 Log.d(TAG, "OICCoordinatorStop : "+ result);
218 case R.id.btLogClear: