1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
20 package org.iotivity.service.easysetup.mediator;
22 import java.io.BufferedReader;
23 import java.io.FileReader;
24 import java.io.IOException;
25 import java.lang.reflect.Method;
26 import java.net.InetAddress;
27 import java.util.ArrayList;
29 import android.content.Context;
30 import android.net.wifi.WifiConfiguration;
31 import android.net.wifi.WifiManager;
32 import android.os.Handler;
33 import android.util.Log;
35 public class WiFiSoftAPManager {
36 private final WifiManager mWifiManager;
37 private Context context;
38 static ArrayList<EnrolleeOnBoardingInfo> appNotification = new ArrayList<EnrolleeOnBoardingInfo>();
39 IOnBoardingStatus finishListener = null;
41 public enum WIFI_AP_STATE {
42 WIFI_AP_STATE_DISABLING, WIFI_AP_STATE_DISABLED, WIFI_AP_STATE_ENABLING, WIFI_AP_STATE_ENABLED, WIFI_AP_STATE_FAILED
45 public WiFiSoftAPManager(Context context) {
46 this.context = context;
47 mWifiManager = (WifiManager) this.context
48 .getSystemService(Context.WIFI_SERVICE);
52 * Start AccessPoint mode with the specified configuration. If the radio is
53 * already running in AP mode, update the new configuration Note that
54 * starting in access point mode disables station mode operation
57 * SSID, security and channel details as part of
59 * @return {@code true} if the operation succeeds, {@code false} otherwise
61 public boolean setWifiApEnabled(WifiConfiguration wifiConfig,
64 if (enabled) { // disable WiFi in any case
65 mWifiManager.setWifiEnabled(false);
68 Method method = mWifiManager.getClass().getMethod(
69 "setWifiApEnabled", WifiConfiguration.class, boolean.class);
70 return (Boolean) method.invoke(mWifiManager, wifiConfig, enabled);
71 } catch (Exception e) {
72 Log.e(this.getClass().toString(), "", e);
78 * Gets the Wi-Fi enabled state.
80 * @return {@link WIFI_AP_STATE}
81 * @see #isWifiApEnabled()
83 public WIFI_AP_STATE getWifiApState() {
85 Method method = mWifiManager.getClass().getMethod("getWifiApState");
87 int tmp = ((Integer) method.invoke(mWifiManager));
94 return WIFI_AP_STATE.class.getEnumConstants()[tmp];
95 } catch (Exception e) {
96 Log.e(this.getClass().toString(), "", e);
97 return WIFI_AP_STATE.WIFI_AP_STATE_FAILED;
102 * Return whether Wi-Fi AP is enabled or disabled.
104 * @return {@code true} if Wi-Fi AP is enabled
105 * @see #getWifiApState()
107 * @hide Dont open yet
109 public boolean isWifiApEnabled() {
110 return getWifiApState() == WIFI_AP_STATE.WIFI_AP_STATE_ENABLED;
114 * Gets the Wi-Fi AP Configuration.
116 * @return AP details in {@link WifiConfiguration}
118 public WifiConfiguration getWifiApConfiguration() {
120 Method method = mWifiManager.getClass().getMethod(
121 "getWifiApConfiguration");
122 return (WifiConfiguration) method.invoke(mWifiManager);
123 } catch (Exception e) {
124 Log.e(this.getClass().toString(), "", e);
130 * Sets the Wi-Fi AP Configuration.
132 * @return {@code true} if the operation succeeded, {@code false} otherwise
134 public boolean setWifiApConfiguration(WifiConfiguration wifiConfig) {
136 Method method = mWifiManager.getClass().getMethod(
137 "setWifiApConfiguration", WifiConfiguration.class);
138 return (Boolean) method.invoke(mWifiManager, wifiConfig);
139 } catch (Exception e) {
140 Log.e(this.getClass().toString(), "", e);
146 * Gets a list of the clients connected to the Hotspot, reachable timeout is
149 * @param onlyReachables
150 * {@code false} if the list should contain unreachable (probably
151 * disconnected) clients, {@code true} otherwise
152 * @param finishListener
153 * , Interface called when the scan method finishes
155 public void getClientList(boolean onlyReachables,
156 IOnBoardingStatus finishListener) {
157 this.finishListener = finishListener;
158 getClientList(onlyReachables, 300);
162 * Gets a list of the clients connected to the Hotspot
164 * @param onlyReachables
165 * {@code false} if the list should contain unreachable (probably
166 * disconnected) clients, {@code true} otherwise
167 * @param reachableTimeout
168 * Reachable Timeout in miliseconds
169 * @param finishListener
170 * , Interface called when the scan method finishes
172 public void getClientList(final boolean onlyReachables,
173 final int reachableTimeout) {
175 Runnable runnable = new Runnable() {
178 BufferedReader br = null;
179 final EnrolleeInfo result = new EnrolleeInfo();
182 br = new BufferedReader(new FileReader("/proc/net/arp"));
184 while ((line = br.readLine()) != null) {
185 boolean deviceAddedToList = false;
187 String[] splitted = line.split(" +");
189 if ((splitted != null) && (splitted.length >= 4)) {
190 // Basic sanity check
191 String mac = splitted[3];
193 if (mac.matches("..:..:..:..:..:..")) {
194 boolean isReachable = InetAddress.getByName(
195 splitted[0]).isReachable(
198 // String execStatement =
199 // "ping -c 1 "+splitted[0];
202 // java.lang.Runtime.getRuntime().exec(execStatement);
204 // int returnVal = p1.waitFor();
205 // boolean isReachable = (returnVal==0);
207 Log.i("exec statement", splitted[0]);
208 Log.i("Return Value", " " + isReachable);
210 if (appNotification.size() > 0) {
211 for (EnrolleeOnBoardingInfo ipDeviceOnBoardingNotification : appNotification) {
212 boolean macAddressComparison = ipDeviceOnBoardingNotification
213 .getHWAddr().equalsIgnoreCase(
216 if (macAddressComparison) {
217 deviceAddedToList = true;
219 if (ipDeviceOnBoardingNotification
220 .isAdditionNotified()
223 } else if (ipDeviceOnBoardingNotification
228 result.setIpAddr(splitted[0]);
229 result.setHWAddr(splitted[3]);
230 result.setDevice(splitted[5]);
231 result.setReachable(isReachable);
234 .remove(ipDeviceOnBoardingNotification);
237 .add(new EnrolleeOnBoardingInfo(
245 .add(new EnrolleeOnBoardingInfo(
253 NotifyApplication(result);
257 if (!deviceAddedToList) {
260 .add(new EnrolleeOnBoardingInfo(
268 .add(new EnrolleeOnBoardingInfo(
276 result.setIpAddr(splitted[0]);
277 result.setHWAddr(splitted[3]);
278 result.setDevice(splitted[5]);
279 result.setReachable(isReachable);
281 NotifyApplication(result);
288 .add(new EnrolleeOnBoardingInfo(
296 .add(new EnrolleeOnBoardingInfo(
304 result.setIpAddr(splitted[0]);
305 result.setHWAddr(splitted[3]);
306 result.setDevice(splitted[5]);
307 result.setReachable(isReachable);
309 NotifyApplication(result);
315 } catch (Exception e) {
316 Log.e(this.getClass().toString(), e.toString());
320 } catch (IOException e) {
321 Log.e(this.getClass().toString(), e.getMessage());
327 Thread mythread = new Thread(runnable);
331 void NotifyApplication(final EnrolleeInfo result) {
332 // Get a handler that can be used to post to the main thread
333 Handler mainHandler = new Handler(context.getMainLooper());
334 Runnable myRunnable = new Runnable() {
337 finishListener.deviceOnBoardingStatus(result);
340 mainHandler.post(myRunnable);