IP address of device using phone as access point
2013-03-22 08:22
369 查看
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
ClientScanResult.java
[b]WIFI_AP_STATE.java[/b]
WifiApManager.java
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. */ @Override public 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<ClientScanResult> 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; }
[b]WIFI_AP_STATE.java[/b]
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<ClientScanResult> 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<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)) { // 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; } }
相关文章推荐
- Get the IP address of a network interface in C using SIOCGIFADDR
- Android: get IP address of device
- 当Navicat for Mysql出现 1045 -Access denied for user'root'@'ipAddress'(using password :yes)
- Using PowerCLI to get the IP address of a VM
- IP Address change and Symbian Access Point Reconnection Issues
- How to quikly find AS number of a IP address
- 电脑弹出&quot;Access violation at address ****. Read of address ****&quot;什么意思?
- A complete guide to using Keras as part of a TensorFlow workflow: tutorial
- Change language of UI using custom control in SharePoint 2010
- UserName showing up as DomainName\UserName instead of Full Name in SharePoint 2010
- Genymotion模拟器常见问题——Unable to start the virtual device,The virtual device got no IP address
- 解决the virtual device got no ip address情况之一
- Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hos
- oracle 错误 access violation at address in module 'ora805.dll'. write of address
- Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is rec
- Class org.apache.struts2.json.JSONWriter can not access a member of class org.springframework.aop.TruePointcut with modifiers "public"
- Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable解决方法
- proftpd启动失败提示unable to determine IP address of “xxx.com”
- proftpd启动失败提示unable to determine IP address of “xxx.com”
- Installing SharePoint 2007 by using different versions of .net framework