2 * ***************************************************************
4 * Copyright 2015 Samsung Electronics All Rights Reserved.
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * ****************************************************************
23 package org.iotivity.service.easysetup.impl;
25 import java.util.Timer;
26 import java.util.TimerTask;
28 import org.iotivity.service.easysetup.core.ConnectionInterface;
29 import org.iotivity.service.easysetup.core.EnrolleeDevice;
30 import org.iotivity.service.easysetup.core.EnrolleeState;
31 import org.iotivity.service.easysetup.core.IpConnection;
32 import org.iotivity.service.easysetup.core.OnBoardingConfig;
33 import org.iotivity.service.easysetup.core.ProvisioningConfig;
34 import org.iotivity.service.easysetup.mediator.EasySetupCallbackHandler;
35 import org.iotivity.service.easysetup.mediator.EasySetupManager;
36 import org.iotivity.service.easysetup.mediator.EnrolleeInfo;
37 import org.iotivity.service.easysetup.mediator.IOnBoardingStatus;
38 import org.iotivity.service.easysetup.mediator.IProvisioningListener;
39 import org.iotivity.service.easysetup.mediator.ProvisionEnrollee;
40 import org.iotivity.service.easysetup.mediator.ip.WiFiSoftAPManager;
42 import android.content.Context;
43 import android.net.wifi.WifiConfiguration;
44 import android.util.Log;
45 import android.widget.Toast;
48 * This is a ready to use class for Enrollee device having Soft AP as on-boarding connectivity.
50 public class EnrolleeDeviceWiFiOnboarding extends EnrolleeDevice {
52 public static final String TAG = EnrolleeDeviceWiFiOnboarding.class.getName();
54 final Context mContext;
55 final WiFiSoftAPManager mWifiSoftAPManager;
56 EnrolleeInfo connectedDevice;
57 private EasySetupManager easySetupManagerNativeInstance;
58 ProvisionEnrollee provisionEnrolleInstance;
60 IOnBoardingStatus deviceScanListener = new IOnBoardingStatus() {
63 public void deviceOnBoardingStatus(EnrolleeInfo enrolleStatus) {
64 Log.d("ESSoftAPOnBoarding", "Entered");
65 if (enrolleStatus != null && enrolleStatus.getIpAddr() != null) {
66 String finalResult = "Easy Connect : ";
68 if (enrolleStatus.isReachable()) {
69 finalResult = "Device OnBoarded" + "["
70 + enrolleStatus.getIpAddr() + "]";
72 connectedDevice = enrolleStatus;
73 IpConnection conn = new IpConnection();
74 conn.setConnectivity(true);
75 conn.setIp(connectedDevice.getIpAddr());
76 Log.d("ESSoftAPOnBoarding", "Entered");
77 mOnBoardingCallback.onFinished(conn);
83 IpConnection conn = new IpConnection();
84 conn.setConnectivity(false);
85 mOnBoardingCallback.onFinished(conn);
90 protected EnrolleeDeviceWiFiOnboarding(Context context, OnBoardingConfig onBoardingConfig, ProvisioningConfig provConfig) {
91 super(onBoardingConfig, provConfig);
93 mWifiSoftAPManager = new WiFiSoftAPManager(mContext);
97 protected void startOnBoardingProcess() {
98 Log.i(TAG, "Starging on boarding process");
101 boolean status = mWifiSoftAPManager.setWifiApEnabled((WifiConfiguration) mOnBoardingConfig.getConfig(), true);
103 Log.i(TAG, "Soft AP is created with status " + status);
106 myTimer = new Timer();
107 myTimer.schedule(new TimerTask() {
110 // Below function to be called after 5 seconds
111 mWifiSoftAPManager.getClientList(deviceScanListener, 300);
117 protected void stopOnBoardingProcess() {
118 Log.i(TAG, "Stopping on boarding process");
119 boolean status = mWifiSoftAPManager.setWifiApEnabled(null, false);
120 Log.i(TAG, "Soft AP is disabled with status " + status);
124 protected void startProvisioningProcess(ConnectionInterface conn) {
126 if (mProvConfig.getConnType() == ProvisioningConfig.ConnType.WiFi) {
128 provisionEnrolleInstance = new ProvisionEnrollee(mContext);
129 provisionEnrolleInstance.registerProvisioningHandler(new IProvisioningListener() {
131 public void onFinishProvisioning(int statuscode) {
132 mState = (statuscode == 0) ? EnrolleeState.DEVICE_PROVISIONING_SUCCESS_STATE : EnrolleeState.DEVICE_PROVISIONING_FAILED_STATE;
133 mProvisioningCallback.onFinished(EnrolleeDeviceWiFiOnboarding.this);
137 IpConnection connection = (IpConnection) conn;
138 WiFiProvConfig wifiProvConfig = (WiFiProvConfig) mProvConfig;
139 easySetupManagerNativeInstance = EasySetupManager.getInstance();
140 easySetupManagerNativeInstance.initEasySetup();
141 easySetupManagerNativeInstance.provisionEnrollee(connection.getIp(), wifiProvConfig.getSsId(), wifiProvConfig.getPassword(), 0 /*In base code '0' is hard coded*/);