博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IP address of device using phone as access point
阅读量:5998 次
发布时间:2019-06-20

本文共 4837 字,大约阅读时间需要 16 分钟。

http://stackoverflow.com/questions/8324215/ip-address-of-device-using-phone-as-access-point

 

The following code will give you the ip adrress & other details of the wifi enabled devices connected to the the android hotspot device

 

Main.java

 

import java.util.ArrayList;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;import com.whitebyte.hotspotclients.R;import com.whitebyte.wifihotspotutils.ClientScanResult;import com.whitebyte.wifihotspotutils.WifiApManager;public class Main extends Activity {      TextView textView1;      WifiApManager wifiApManager;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    textView1 = (TextView) findViewById(R.id.textView1);    wifiApManager = new WifiApManager(this);    scan();}private void scan() {    ArrayList
clients = wifiApManager.getClientList(false); 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"); }}

ClientScanResult.java

public class ClientScanResult {private String IpAddr;private String HWAddr;private String Device;private boolean isReachable;public ClientScanResult(String ipAddr, String hWAddr, String device, boolean isReachable) {    super();    IpAddr = ipAddr;    HWAddr = hWAddr;    Device = device;    this.setReachable(isReachable);}public String getIpAddr() {    return IpAddr;}public void setIpAddr(String ipAddr) {    IpAddr = ipAddr;}public String getHWAddr() {    return HWAddr;}public void setHWAddr(String hWAddr) {    HWAddr = hWAddr;}public String getDevice() {    return Device;}public void setDevice(String device) {    Device = device;}public void setReachable(boolean isReachable) {    this.isReachable = isReachable;}public boolean isReachable() {    return isReachable;}

 

WIFI_AP_STATE.java

 

public enum WIFI_AP_STATE      {        WIFI_AP_STATE_DISABLING, WIFI_AP_STATE_DISABLED, WIFI_AP_STATE_ENABLING, WIFI_AP_STATE_ENABLED, WIFI_AP_STATE_FAILED     }

 

WifiApManager.java

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);}/** * Gets a list of the clients connected to the Hotspot, reachable timeout is 300 * @param onlyReachables {
@code false} if the list should contain unreachable (probably disconnected) clients, {
@code true} otherwise * @return ArrayList of {
@link ClientScanResult} */public ArrayList
getClientList(boolean onlyReachables) { return getClientList(onlyReachables, 300);}/** * Gets a list of the clients connected to the Hotspot * @param onlyReachables {
@code false} if the list should contain unreachable (probably disconnected) clients, {
@code true} otherwise * @param reachableTimeout Reachable Timout in miliseconds * @return ArrayList of {
@link ClientScanResult} */public ArrayList
getClientList(boolean onlyReachables, int reachableTimeout) { BufferedReader br = null; ArrayList
result = null; try { result = new ArrayList
(); 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)) { // Basic sanity check 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;}}

 

 

转载地址:http://pawmx.baihongyu.com/

你可能感兴趣的文章
Base64算法的使用
查看>>
MySQL表类型
查看>>
OpenStack collectd的从零安装客户端
查看>>
5.4完成其他模块的xadmin后台注册
查看>>
Nodejs进阶:基于express+multer的文件上传
查看>>
嵌入式软件测试
查看>>
表格的编写,课程表
查看>>
Excel中R1C1引用样式
查看>>
验证正则表达式(纯属转载)
查看>>
php怎么判断网页是电脑访问还是手机访问
查看>>
Node.js的循环与异步问题
查看>>
PE文件详解1——PE文件头部解析
查看>>
kafka配置参数
查看>>
Cocos2d-x PluginX (一)使用手册
查看>>
Flask环境搭建
查看>>
1、FreeRTOS移植
查看>>
java第二次实验报告
查看>>
如何快速熟悉新项目的代码?
查看>>
Oracle PL/SQL入门之慨述
查看>>
Oracle内置函数
查看>>