package
com.example.wifimanager;
import
java.util.ArrayList;
import
android.app.Activity;
import
android.os.Bundle;
import
android.view.Menu;
import
android.view.MenuItem;
import
android.widget.TextView;
import
com.example.wifimanager.R;
import
com.example.wifimanager.WifiApManager;
import
com.example.wifimanager.ClientScanResult;
public
class
MainActivity
extends
Activity {
TextView textView1;
WifiApManager wifiApManager;
/** Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView) findViewById(R.id.textView1);
wifiApManager =
new
WifiApManager(MainActivity.
this
);
scan();
}
private
void
scan() {
ArrayList<ClientScanResult> clients = wifiApManager.getClientList(
false
);
textView1.append(
"WifiApState: "
+ wifiApManager.getWifiApState() +
"\n\n"
);
textView1.append(
"Clients: \n"
);
for
(ClientScanResult clientScanResult : clients) {
textView1.append(
"####################\n"
);
textView1.append(
"IpAddr: "
+ clientScanResult.getIpAddr() +
"\n"
);
textView1.append(
"Device: "
+ clientScanResult.getDevice() +
"\n"
);
textView1.append(
"HWAddr: "
+ clientScanResult.getHWAddr() +
"\n"
);
textView1.append(
"isReachable: "
+ clientScanResult.isReachable() +
"\n"
);
}
}
public
boolean
onCreateOptionsMenu(Menu menu) {
menu.add(
0
,
0
,
0
,
"Get Clients"
);
menu.add(
0
,
1
,
0
,
"Open AP"
);
menu.add(
0
,
2
,
0
,
"Close AP"
);
return
super
.onCreateOptionsMenu(menu);
}
public
boolean
onMenuItemSelected(
int
featureId, MenuItem item) {
switch
(item.getItemId()) {
case
0
:
scan();
break
;
case
1
:
wifiApManager.setWifiApEnabled(
null
,
true
);
break
;
case
2
:
wifiApManager.setWifiApEnabled(
null
,
false
);
break
;
}
return
super
.onMenuItemSelected(featureId, item);
}
}
=========================
package
com.example.wifimanager;
import
java.io.BufferedReader;
import
java.io.FileReader;
import
java.io.IOException;
import
java.lang.reflect.Method;
import
java.net.InetAddress;
import
java.util.ArrayList;
import
android.content.Context;
import
android.net.wifi.WifiConfiguration;
import
android.net.wifi.WifiManager;
import
android.util.Log;
public
class
WifiApManager {
private
final
WifiManager mWifiManager;
public
WifiApManager(Context context) {
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
}
public
boolean
setWifiApEnabled(WifiConfiguration wifiConfig,
boolean
enabled) {
try
{
if
(enabled) {
mWifiManager.setWifiEnabled(
false
);
}
Method method = mWifiManager.getClass().getMethod(
"setWifiApEnabled"
, WifiConfiguration.
class
,
boolean
.
class
);
return
(Boolean) method.invoke(mWifiManager, wifiConfig, enabled);
}
catch
(Exception e) {
Log.e(
this
.getClass().toString(),
""
, e);
return
false
;
}
}
public
WIFI_AP_STATE getWifiApState() {
try
{
Method method = mWifiManager.getClass().getMethod(
"getWifiApState"
);
int
tmp = ((Integer)method.invoke(mWifiManager));
if
(tmp >
10
) {
tmp = tmp -
10
;
}
return
WIFI_AP_STATE.
class
.getEnumConstants()[tmp];
}
catch
(Exception e) {
Log.e(
this
.getClass().toString(),
""
, e);
return
WIFI_AP_STATE.WIFI_AP_STATE_FAILED;
}
}
public
boolean
isWifiApEnabled() {
return
getWifiApState() == WIFI_AP_STATE.WIFI_AP_STATE_ENABLED;
}
public
WifiConfiguration getWifiApConfiguration() {
try
{
Method method = mWifiManager.getClass().getMethod(
"getWifiApConfiguration"
);
return
(WifiConfiguration) method.invoke(mWifiManager);
}
catch
(Exception e) {
Log.e(
this
.getClass().toString(),
""
, e);
return
null
;
}
}
public
boolean
setWifiApConfiguration(WifiConfiguration wifiConfig) {
try
{
Method method = mWifiManager.getClass().getMethod(
"setWifiApConfiguration"
, WifiConfiguration.
class
);
return
(Boolean) method.invoke(mWifiManager, wifiConfig);
}
catch
(Exception e) {
Log.e(
this
.getClass().toString(),
""
, e);
return
false
;
}
}
public
ArrayList<ClientScanResult> getClientList(
boolean
onlyReachables) {
return
getClientList(onlyReachables,
300
);
}
public
ArrayList<ClientScanResult> getClientList(
boolean
onlyReachables,
int
reachableTimeout) {
BufferedReader br =
null
;
ArrayList<ClientScanResult> result =
null
;
try
{
result =
new
ArrayList<ClientScanResult>();
br =
new
BufferedReader(
new
FileReader(
"/proc/net/arp"
));
String line;
while
((line = br.readLine()) !=
null
) {
String[] splitted = line.split(
" +"
);
if
((splitted !=
null
) && (splitted.length >=
4
)) {
String mac = splitted[
3
];
if
(mac.matches(
"..:..:..:..:..:.."
)) {
boolean
isReachable = InetAddress.getByName(splitted[
0
]).isReachable(reachableTimeout);
if
(!onlyReachables || isReachable) {
result.add(
new
ClientScanResult(splitted[
0
], splitted[
3
], splitted[
5
], isReachable));
}
}
}
}
}
catch
(Exception e) {
Log.e(
this
.getClass().toString(), e.getMessage());
}
finally
{
try
{
br.close();
}
catch
(IOException e) {
Log.e(
this
.getClass().toString(), e.getMessage());
}
}
return
result;
}
}