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.mediator;
25 import java.util.Timer;
26 import java.util.TimerTask;
28 import org.iotivity.service.easysetup.core.EnrolleeInfo;
29 import org.iotivity.service.easysetup.core.IOnBoardingStatus;
30 import org.iotivity.service.easysetup.core.ip.WiFiSoftAPManager;
32 import android.content.Context;
33 import android.net.wifi.WifiConfiguration;
34 import android.util.Log;
37 * This is a ready to use class for Enrollee device having Soft AP as on-boarding connectivity.
39 public class EnrolleeDeviceWiFiOnboarding extends EnrolleeDevice {
41 public static final String TAG = EnrolleeDeviceWiFiOnboarding.class.getName();
43 final Context mContext;
44 final WiFiSoftAPManager mWifiSoftAPManager;
45 EnrolleeInfo connectedDevice;
48 IOnBoardingStatus deviceScanListener = new IOnBoardingStatus() {
51 public void deviceOnBoardingStatus(EnrolleeInfo enrolleStatus) {
53 Log.d("ESSoftAPOnBoarding", "Entered");
54 if (mState == EnrolleeState.DEVICE_ON_BOARDING_STATE) {
55 Log.d("ESSoftAPOnBoarding", "Device in OnBoarding State");
56 if (enrolleStatus != null && enrolleStatus.getIpAddr() != null) {
57 String finalResult = "Easy Connect : ";
59 if (enrolleStatus.isReachable()) {
60 finalResult = "Device OnBoarded" + "["
61 + enrolleStatus.getIpAddr() + "]";
63 connectedDevice = enrolleStatus;
64 IpOnBoardingConnection conn = new IpOnBoardingConnection();
66 conn.setConnectivity(true);
67 conn.setIp(connectedDevice.getIpAddr());
68 conn.setHardwareAddress(enrolleStatus.getHWAddr());
69 conn.setDeviceName(enrolleStatus.getDevice());
71 Log.d("ESSoftAPOnBoarding", "Entered" + finalResult);
72 mOnBoardingCallback.onFinished(conn);
78 IpOnBoardingConnection conn = new IpOnBoardingConnection();
79 conn.setConnectivity(false);
80 mOnBoardingCallback.onFinished(conn);
82 Log.e("ESSoftAPOnBoarding", "Device NOT in OnBoarding State. Ignoring the event");
88 protected EnrolleeDeviceWiFiOnboarding(Context context, OnBoardingConfig onBoardingConfig,
89 ProvisioningConfig provConfig) {
90 super(onBoardingConfig, provConfig);
92 mState = EnrolleeState.DEVICE_INIT_STATE;
93 mWifiSoftAPManager = new WiFiSoftAPManager(mContext);
96 protected EnrolleeDeviceWiFiOnboarding(Context context, IpOnBoardingConnection conn,
97 ProvisioningConfig provConfig) {
98 super(new WiFiOnBoardingConfig(), provConfig);
100 mState = EnrolleeState.DEVICE_ON_BOARDED_STATE;
102 mWifiSoftAPManager = new WiFiSoftAPManager(mContext);
106 protected void startOnBoardingProcess() {
107 Log.i(TAG, "Starting on boarding process");
110 boolean status = mWifiSoftAPManager.setWifiApEnabled((WifiConfiguration)
111 mOnBoardingConfig.getConfig(), true);
113 mState = EnrolleeState.DEVICE_ON_BOARDING_STATE;
115 Log.i(TAG, "Soft AP is created with status " + status);
117 myTimer = new Timer();
118 myTimer.schedule(new TimerTask() {
121 // Below function to be called after 5 seconds
122 mWifiSoftAPManager.getClientList(deviceScanListener, 300);
128 protected void stopOnBoardingProcess() {
129 Log.i(TAG, "Stopping on boarding process");
134 boolean status = mWifiSoftAPManager.setWifiApEnabled(null, false);
135 Log.i(TAG, "Soft AP is disabled with status " + status);
139 protected void startProvisioningProcess(OnBoardingConnection conn) {
141 mState = EnrolleeState.DEVICE_PROVISIONING_STATE;
142 mProvisioningCallback.onProgress(this);
143 final EnrolleeDevice device = this;
144 if (mProvConfig.getConnType() == ProvisioningConfig.ConnType.WiFi) {
146 mRemoteEnrollee.registerProvisioningHandler(new IProvisionStatusNativeHandler() {
148 public void onStatusRecieved(int state) {
149 device.mState = convertIntToProvisioningState(state);
150 Log.i(TAG,"Device state changed :"+device.mState);
151 mProvisioningCallback.onProgress(device);
153 mProvisioningCallback.onFinished(EnrolleeDeviceWiFiOnboarding.this);
158 mRemoteEnrollee.startProvision();
159 }catch(ESException e) {
160 Log.i(TAG,"startProvisioningProcess Register Listener to native exception");
165 protected void stopProvisioningProcess(){
166 if(mState == EnrolleeState.DEVICE_PROVISIONING_STATE)
169 mRemoteEnrollee.stopProvision();
170 } catch (ESException e) {
171 Log.i(TAG,"stopProvisioningProcess exception");
174 Log.i(TAG,"stopProvisioningProcess : Provisioning is not in progress");
178 private EnrolleeState convertIntToProvisioningState(int state){
183 return EnrolleeState.DEVICE_PROVISIONED_STATE;
185 return EnrolleeState.DEVICE_NOT_PROVISIONED;
187 return EnrolleeState.DEVICE_PROVISIONED_STATE;
189 return EnrolleeState.DEVICE_NOT_PROVISIONED;
191 return EnrolleeState.DEVICE_INIT_STATE;