1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 package com.oc.ub.sample;
23 import com.oc.OCDiscovery;
24 import com.oc.OCObserver;
25 import com.oc.OCResource;
26 import com.oc.OCResourceResultHandler;
27 import com.oc.OCServer;
28 import com.oc.ub.sampleclient.R;
30 import android.app.Activity;
31 import android.app.Fragment;
32 import android.os.Bundle;
33 import android.view.LayoutInflater;
34 import android.view.Menu;
35 import android.view.MenuItem;
36 import android.view.View;
37 import android.view.ViewGroup;
39 public class OCClient extends Activity {
40 private static String TAG = "OCCLIENT";
41 private OCServer ocServer = null;
42 private OCDiscovery ocDiscovery = null;
45 protected void onCreate(Bundle savedInstanceState) {
46 super.onCreate(savedInstanceState);
47 setContentView(R.layout.activity_occlient);
49 if (savedInstanceState == null) {
50 getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment())
54 ocServer = new OCServer();
56 ocDiscovery = ocServer.getDiscoverySingleton();
59 * APPLICATION NOTE: All callbacks will come through on a new thread and will not interfere with
60 * the UI thread or the Framework Thread (i.e. Networking Thread) which means that you need to
61 * use a Handler() to post back to the UI.
63 ocDiscovery.findResourceByType(null, null, new OCResourceResultHandler() {
65 public void onResourcesFound(String serviceURL, OCResource[] resources) {
66 // NOTE: This means that a service is reporting back zero or more resources
67 // During the callback, lets check to see if the resource supports the
68 // light control interface and turn the light on and then subscribe to any changes in the
69 // light's power state.
70 for (OCResource resource : resources) {
72 LightControl control = resource.getInterface(LightControl.class);
74 if (control != null) {
75 control.setPowered(true);
78 * APPLICATION NOTE: All callbacks will come through on a new thread and will not
79 * interfere with the UI thread or the Framework Thread (i.e. Networking Thread) which
80 * means that you need to use a Handler() to post back to the UI.
82 control.addPoweredObserver(new OCObserver<Boolean>() {
84 public void onChanged(Boolean data) {
85 // TODO: Tell the UI! The state of the Light has changed.
90 public void onFailed(Throwable throwable) {
91 // TODO: Tell the UI! We are no longer subscribed to events for the Light's power
96 } catch (Exception exception) {
104 public void onFailed(Throwable throwable) {
105 // TODO: Handle the error. The request to find resources has failed.
109 public void onCompleted() {
110 // Nothing to do here... we are just done finding resources is all.
116 public boolean onCreateOptionsMenu(Menu menu) {
118 // Inflate the menu; this adds items to the action bar if it is present.
119 getMenuInflater().inflate(R.menu.occlient, menu);
124 public boolean onOptionsItemSelected(MenuItem item) {
125 // Handle action bar item clicks here. The action bar will automatically handle clicks on the
126 // Home/Up button, so long as you specify a parent activity in AndroidManifest.xml.
127 int id = item.getItemId();
128 if (id == R.id.action_settings) {
131 return super.onOptionsItemSelected(item);
135 protected void onDestroy() {
142 * A placeholder fragment containing a simple view.
144 public static class PlaceholderFragment extends Fragment {
146 public PlaceholderFragment() {}
149 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
150 View rootView = inflater.inflate(R.layout.fragment_occlient, container, false);