android 得到連接熱點的ip的方法


WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
if (!wifiManager.isWifiEnabled()) {
System.out.println("=================");
wifiManager.setWifiEnabled(true);
}
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String IPAddress = intToIp(wifiInfo.getIpAddress());
System.out.println("IPAddress-->>" + IPAddress);

DhcpInfo dhcpinfo = wifiManager.getDhcpInfo();
String serverAddress = intToIp(dhcpinfo.serverAddress);
System.out.println("serverAddress-->>" + serverAddress);
其中IPAddress 是本機的IP地址,serverAddress 是你所連接的wifi熱點對應的IP地址
 
 

private String intToIp(int paramInt)
{
return (paramInt & 0xFF) + "." + (0xFF & paramInt >> 8) + "." + (0xFF & paramInt >> 16) + "."
+ (0xFF & paramInt >> 24);
}

 

 

當在Android設備終端上使用Wifi熱點的時候,需要獲知Wifi熱點的運行狀態,熱點是否打開,連接到該WIFI熱點的設備數量,以及連接設備的具體IP和MAC地址。

使用re文件管理器去"/proc/net/arp",打開,發現連接上熱點的設備信息都在這里了,包括mac ip等。

鑒於此,我們可以在代碼中打開該文件,並獲取WIFI熱點的信息。

 

獲取WIFI熱點狀態的方法getWifiApState()和判斷熱點是否可用的方法isApEnabled(),在Android源碼WifiManager.Java中已經實現,但是它們是Hide方法,在SDK層面是不能訪問的,如要訪問需要用到java反射的機制。具體代碼實現如下:

 

其中定義WIFI AP的幾個狀態

 

[java]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. public static final int WIFI_AP_STATE_DISABLING = 10;    
  2. public static final int WIFI_AP_STATE_DISABLED = 11;    
  3. public static final int WIFI_AP_STATE_ENABLING = 12;    
  4. public static final int WIFI_AP_STATE_ENABLED = 13;    
  5. public static final int WIFI_AP_STATE_FAILED = 14;   

對應於WifiMangaer.java中對這幾個狀態的定義。

 

 

獲取WIFI熱點的狀態:

 

[java]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. public int getWifiApState(Context mContext) {    
  2.     WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);   
  3.        try {    
  4.            Method method = wifiManager.getClass().getMethod("getWifiApState");    
  5.            int i = (Integer) method.invoke(wifiManager);    
  6.            Log.i(TAG,"wifi state:  " + i);    
  7.            return i;    
  8.        } catch (Exception e) {    
  9.            Log.e(TAG,"Cannot get WiFi AP state" + e);    
  10.            return WIFI_AP_STATE_FAILED;    
  11.        }    
  12.    }    

判斷Wifi熱點是否可用:

 

 

[java]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. public boolean isApEnabled(Context mContext) {    
  2.        int state = getWifiApState(mContext);    
  3.        return WIFI_AP_STATE_ENABLING == state || WIFI_AP_STATE_ENABLED == state;    
  4.    }   


 

獲取鏈接到當前熱點的設備IP:

[java]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. private ArrayList<String> getConnectedHotIP() {  
  2.     ArrayList<String> connectedIP = new ArrayList<String>();  
  3.     try {  
  4.         BufferedReader br = new BufferedReader(new FileReader(  
  5.                 "/proc/net/arp"));  
  6.         String line;  
  7.         while ((line = br.readLine()) != null) {  
  8.             String[] splitted = line.split(" +");  
  9.             if (splitted != null && splitted.length >= 4) {  
  10.                 String ip = splitted[0];  
  11.                 connectedIP.add(ip);  
  12.             }  
  13.         }  
  14.     } catch (Exception e) {  
  15.         e.printStackTrace();  
  16.     }  
  17.     return connectedIP;  
  18. }  
[java]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. //輸出鏈接到當前設備的IP地址  
  2. public void printHotIp() {  
  3.   
  4.     ArrayList<String> connectedIP = getConnectedHotIP();  
  5.     StringBuilder resultList = new StringBuilder();  
  6.     for (String ip : connectedIP) {  
  7.         resultList.append(ip);  
  8.         resultList.append("\n");  
  9.     }  
  10.     System.out.print(resultList);  
  11.     Log.d(TAG,"---->>heww resultList="+resultList);  
  12. }  

 

當然在應用中要添加訪問WIFI設備的權限:

 

[html]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  

否則將會提示如下錯誤:

 

Cannot get WiFi AP state

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM