1 package org.iotivity.service.easysetup.core;
3 import android.content.Context;
4 import android.util.Log;
6 import org.iotivity.service.easysetup.impl.EnrolleeDeviceFactory;
8 import java.io.IOException;
9 import java.util.ArrayList;
12 * This is facade class, a single point of contact for Application.
13 * It contains set of APIs to do easy setup of the enrolling device.
14 * ON-BOARDING - This is a step to establish connectivity between the device & Mediator device.
15 * PROVISION - This is a step where the netowork's detail & credentials are given to the enrolling device.
17 public class EasySetupService {
19 private static EasySetupService sInstance;
21 private final EasySetupStatus mCallback;
23 private ArrayList<EnrolleeDevice> mEnrolleeDeviceList;
25 private final ProvisioningCallback mProvisioningCallback;
27 private static Context mContext;
29 public EnrolleeDeviceFactory mDeviceFactory;
32 private EasySetupService(EasySetupStatus callback) {
34 mProvisioningCallback = new ProvisioningCallbackImpl(mCallback);
35 mEnrolleeDeviceList = new ArrayList<EnrolleeDevice>();
37 mDeviceFactory = null;
41 * Gives a singleton instance of Easy setup service.
43 * @param callback Application needs to provide this callback to receive the status of of easy setup process.
46 public synchronized static EasySetupService getInstance(Context context, EasySetupStatus callback) {
47 if (sInstance == null) {
48 sInstance = new EasySetupService(callback);
55 * Starts Easy setup process for the enrolling device.
57 * @param enrolledevice Device to be enrolled in network
58 * @throws IOException Throws exception in case of any connection error.
61 public synchronized void startSetup(final EnrolleeDevice enrolledevice) throws IOException {
63 mEnrolleeDeviceList.add(enrolledevice);
65 // Starts the provisioning directly if the device is already on boarded on the network.
66 if (enrolledevice.onBoarded()) {
67 enrolledevice.startProvisioning(mProvisioningCallback);
71 enrolledevice.startOnBoarding(new OnBoardingCallback() {
74 public void onFinished(ConnectionInterface connection) {
75 if (connection.isConnected()) {
77 Thread.sleep(15000);//Sleep for allowing thin device to start the services
78 } catch (InterruptedException e) {
82 // Start provisioning here
83 enrolledevice.setConnection(connection);
84 enrolledevice.startProvisioning(mProvisioningCallback);
86 enrolledevice.mState = EnrolleeState.DEVICE_PROVISIONING_FAILED_STATE;
87 mProvisioningCallback.onFinished(enrolledevice);
97 * Stops on-going Easy setup process for enrolling device.
99 * @param enrolledevice Device to be enrolled in network
101 public synchronized void stopSetup(EnrolleeDevice enrolledevice) {
102 enrolledevice.stopOnBoardingProcess();
103 mEnrolleeDeviceList.remove(enrolledevice);
106 public synchronized void getEnrolleeDevice(OnBoardingConfig connType) {
107 mDeviceFactory = EnrolleeDeviceFactory.newInstance(mContext);
110 class ProvisioningCallbackImpl extends ProvisioningCallback {
112 private final EasySetupStatus mCallback;
114 ProvisioningCallbackImpl(EasySetupStatus callback) {
115 mCallback = callback;
119 public void onFinished(EnrolleeDevice enrolledevice) {
120 //if(mEnrolleeDeviceList.contains(enrolledevice)) {
121 mCallback.onFinished(enrolledevice);