1 package org.iotivity.service.easysetup.impl;
3 import java.util.Timer;
4 import java.util.TimerTask;
6 import org.iotivity.service.easysetup.core.ConnectionInterface;
7 import org.iotivity.service.easysetup.core.EnrolleeDevice;
8 import org.iotivity.service.easysetup.core.EnrolleeState;
9 import org.iotivity.service.easysetup.core.IpConnection;
10 import org.iotivity.service.easysetup.core.OnBoardingConfig;
11 import org.iotivity.service.easysetup.core.ProvisioningConfig;
12 import org.iotivity.service.easysetup.mediator.EasySetupCallbackHandler;
13 import org.iotivity.service.easysetup.mediator.EasySetupManager;
14 import org.iotivity.service.easysetup.mediator.EnrolleeInfo;
15 import org.iotivity.service.easysetup.mediator.IOnBoardingStatus;
16 import org.iotivity.service.easysetup.mediator.IProvisioningListener;
17 import org.iotivity.service.easysetup.mediator.ProvisionEnrollee;
18 import org.iotivity.service.easysetup.mediator.ip.WiFiSoftAPManager;
20 import android.content.Context;
21 import android.net.wifi.WifiConfiguration;
22 import android.util.Log;
23 import android.widget.Toast;
25 public class EnrolleeDeviceWiFiOnboarding extends EnrolleeDevice {
27 public static final String TAG = EnrolleeDeviceWiFiOnboarding.class.getName();
29 final Context mContext;
30 final WiFiSoftAPManager mWifiSoftAPManager;
31 EnrolleeInfo connectedDevice;
32 private EasySetupManager easySetupManagerNativeInstance ;
33 ProvisionEnrollee provisionEnrolleInstance;
35 IOnBoardingStatus deviceScanListener = new IOnBoardingStatus() {
38 public void deviceOnBoardingStatus(EnrolleeInfo enrolleStatus) {
39 Log.d("ESSoftAPOnBoarding", "Entered");
40 if (enrolleStatus != null && enrolleStatus.getIpAddr() != null) {
41 String finalResult = "Easy Connect : ";
43 if (enrolleStatus.isReachable()) {
44 finalResult = "Device OnBoarded" + "["
45 + enrolleStatus.getIpAddr() + "]";
48 * easySetupInstance.StartEasySetup(enrolleStatus.getIpAddr()) ;
49 * easySetupCount++; Log.i("EasyConnect",
50 * "easy Setup Count-"+easySetupCount); Log.i("EasyConnect",
51 * "IP Address-"+enrolleStatus.getIpAddr());
53 connectedDevice = enrolleStatus;
54 IpConnection conn = new IpConnection();
55 conn.setConnectivity(true);
56 conn.setIp(connectedDevice.getIpAddr());
57 Log.d("ESSoftAPOnBoarding", "Entered");
58 mOnBoardingCallback.onFinished(conn);
63 /* textView1.setText("");
64 textView1.append("Clients: \n");
65 textView1.append("####################\n");
66 textView1.append("IP Address : " + enrolleStatus.getIpAddr()
68 textView1.append("Device : " + enrolleStatus.getDevice() + "\n");
69 textView1.append("HW Address : " + enrolleStatus.getHWAddr()
71 textView1.append("Is OnBoarded : " + enrolleStatus.isReachable()
74 Toast.makeText(getApplicationContext(), finalResult,
75 Toast.LENGTH_LONG).show();*/
80 IpConnection conn = new IpConnection();
81 conn.setConnectivity(false);
82 mOnBoardingCallback.onFinished(conn);
87 protected EnrolleeDeviceWiFiOnboarding(Context context, OnBoardingConfig onBoardingConfig, ProvisioningConfig provConfig) {
88 super(onBoardingConfig, provConfig);
90 mWifiSoftAPManager = new WiFiSoftAPManager(mContext);
94 protected void startOnBoardingProcess() {
95 // TODO Auto-generated method stub
97 //2. Get call back once enrolling device connect to the Soft AP
98 //3. fetch the IP assigned to enrolling device
101 Log.i(TAG, "Starging on boarding process");
104 boolean status = mWifiSoftAPManager.setWifiApEnabled((WifiConfiguration)mOnBoardingConfig.getConfig(), true);
106 Log.i(TAG, "Soft AP is created with status " + status);
110 myTimer = new Timer();
111 myTimer.schedule(new TimerTask() {
114 // Below function to be called after 5 seconds
115 mWifiSoftAPManager.getClientList(deviceScanListener, 300);
124 protected void stopOnBoardingProcess() {
125 Log.i(TAG, "Stopping on boarding process");
126 boolean status = mWifiSoftAPManager.setWifiApEnabled(null, false);
127 Log.i(TAG, "Soft AP is disabled with status " + status);
131 protected void startProvisioningProcess(ConnectionInterface conn) {
133 if(mProvConfig.getConnType() == ProvisioningConfig.ConnType.WiFi) {
135 provisionEnrolleInstance = new ProvisionEnrollee(mContext);
136 provisionEnrolleInstance.registerProvisioningHandler(new IProvisioningListener() {
138 public void onFinishProvisioning(int statuscode) {
139 mState = ( statuscode == 0) ? EnrolleeState.DEVICE_PROVISIONING_SUCCESS_STATE : EnrolleeState.DEVICE_PROVISIONING_FAILED_STATE;
140 mProvisioningCallback.onFinished(EnrolleeDeviceWiFiOnboarding.this);
144 IpConnection connection = (IpConnection)conn;
145 WiFiProvConfig wifiProvConfig = (WiFiProvConfig)mProvConfig;
146 easySetupManagerNativeInstance = EasySetupManager.getInstance();
147 easySetupManagerNativeInstance.initEasySetup();
148 easySetupManagerNativeInstance.provisionEnrollee(connection .getIp(), wifiProvConfig.getSsId(), wifiProvConfig.getPassword(), 0 /*In base code '0' is hard coded*/);