1 //******************************************************************
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 package com.oc.ub.sample;
8 import com.oc.OCDiscovery;
9 import com.oc.OCObserver;
10 import com.oc.OCResource;
11 import com.oc.OCResourceResultHandler;
12 import com.oc.OCServer;
13 import com.oc.ub.sampleclient.R;
15 import android.app.Activity;
16 import android.app.Fragment;
17 import android.os.Bundle;
18 import android.view.LayoutInflater;
19 import android.view.Menu;
20 import android.view.MenuItem;
21 import android.view.View;
22 import android.view.ViewGroup;
24 public class OCClient extends Activity {
25 private static String TAG = "OCCLIENT";
26 private OCServer ocServer = null;
27 private OCDiscovery ocDiscovery = null;
30 protected void onCreate(Bundle savedInstanceState) {
31 super.onCreate(savedInstanceState);
32 setContentView(R.layout.activity_occlient);
34 if (savedInstanceState == null) {
35 getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment())
39 ocServer = new OCServer();
41 ocDiscovery = ocServer.getDiscoverySingleton();
44 * APPLICATION NOTE: All callbacks will come through on a new thread and will not interfere with
45 * the UI thread or the Framework Thread (i.e. Networking Thread) which means that you need to
46 * use a Handler() to post back to the UI.
48 ocDiscovery.findResourceByType(null, null, new OCResourceResultHandler() {
50 public void onResourcesFound(String serviceURL, OCResource[] resources) {
51 // NOTE: This means that a service is reporting back zero or more resources
52 // During the callback, lets check to see if the resource supports the
53 // light control interface and turn the light on and then subscribe to any changes in the
54 // light's power state.
55 for (OCResource resource : resources) {
57 LightControl control = resource.getInterface(LightControl.class);
59 if (control != null) {
60 control.setPowered(true);
63 * APPLICATION NOTE: All callbacks will come through on a new thread and will not
64 * interfere with the UI thread or the Framework Thread (i.e. Networking Thread) which
65 * means that you need to use a Handler() to post back to the UI.
67 control.addPoweredObserver(new OCObserver<Boolean>() {
69 public void onChanged(Boolean data) {
70 // TODO: Tell the UI! The state of the Light has changed.
75 public void onFailed(Throwable throwable) {
76 // TODO: Tell the UI! We are no longer subscribed to events for the Light's power
81 } catch (Exception exception) {
89 public void onFailed(Throwable throwable) {
90 // TODO: Handle the error. The request to find resources has failed.
94 public void onCompleted() {
95 // Nothing to do here... we are just done finding resources is all.
101 public boolean onCreateOptionsMenu(Menu menu) {
103 // Inflate the menu; this adds items to the action bar if it is present.
104 getMenuInflater().inflate(R.menu.occlient, menu);
109 public boolean onOptionsItemSelected(MenuItem item) {
110 // Handle action bar item clicks here. The action bar will automatically handle clicks on the
111 // Home/Up button, so long as you specify a parent activity in AndroidManifest.xml.
112 int id = item.getItemId();
113 if (id == R.id.action_settings) {
116 return super.onOptionsItemSelected(item);
120 protected void onDestroy() {
127 * A placeholder fragment containing a simple view.
129 public static class PlaceholderFragment extends Fragment {
131 public PlaceholderFragment() {}
134 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
135 View rootView = inflater.inflate(R.layout.fragment_occlient, container, false);