Android application Wi-Fi device - AP connectivity
I am building an application which can transfer data between a mobile and a Wi-Fi device... The mobile has got the AP enabled (through code) and another device connects to this specific network... How can I detect through code to see the details of the devices connected to the network(AP)?** Is there a solution for this?
I have seen an application called Wifi Hot spot in HTC Desire that does this functionality of showing the IP addresses of the devices connected to the network. How can this be achieved?
Check out Review: Sprint Mobile Hotspot on HTC EVO 4G.
It shows an application that can actually display the connected users. How can we do that programmatically? Is there an API for that?
For creating an access point:
private void createWifiAccessPoint() {
if (wifiManager.isWifiEnabled())
{
wifiManager.setWifiEnabled(false);
}
Method[] wmMethods = wifiManager.getClass().getDeclaredMethods(); //Get all declared methods in WifiManager class
boolean methodFound = false;
for (Method method: wmMethods){
if (method.getName().equals("setWifiApEnabled")){
methodFound = true;
WifiConfiguration netConfig = new WifiConfiguration();
netConfig.SSID = "\""+ssid+"\"";
netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
//netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
//netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
//netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
//netConfig.preSharedKey = password;
//netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
//netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
//netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
//netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
try {
boolean apstatus = (Boolean) method.invoke(wifiManager, netConfig,true);
//statusView.setText("Creating a Wi-Fi Network \""+netConfig.SSID+"\"");
for (Method isWifiApEnabledmethod: wmMethods)
{
if (isWifiApEnabledmethod.getName().equals("isWifiApEnabled")){
while (!(Boolean)isWifiApEnabledmethod.invoke(wifiManager)){
};
for (Method method1: wmMethods){
开发者_如何学JAVA if(method1.getName().equals("getWifiApState")){
int apstate;
apstate = (Integer)method1.invoke(wifiManager);
// netConfig = (WifiConfiguration)method1.invoke(wifi);
//statusView.append("\nSSID:"+netConfig.SSID+"\nPassword:"+netConfig.preSharedKey+"\n");
}
}
}
}
if(apstatus)
{
System.out.println("SUCCESSdddd");
//statusView.append("\nAccess Point Created!");
//finish();
//Intent searchSensorsIntent = new Intent(this,SearchSensors.class);
//startActivity(searchSensorsIntent);
}
else
{
System.out.println("FAILED");
//statusView.append("\nAccess Point Creation failed!");
}
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
if (!methodFound){
//statusView.setText("Your phone's API does not contain setWifiApEnabled method to configure an access point");
}
}
You could read the /proc/net/arp
file to read all the ARP entries. See the example in the blog post Android: Howto find the hardware MAC address of a remote host. In the ARP table, search for all the hosts that belong to your Wi-Fi network based on the IP address.
Here is example code, which counts the number of hosts connected to the AP. This code assumes that one ARP entry is for the phone connected to the network and the remaining ones are from hosts connected to the AP.
private int countNumMac()
{
int macCount = 0;
BufferedReader br = null;
try {
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("..:..:..:..:..:..")) {
macCount++;
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
br.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
if (macCount == 0)
return 0;
else
return macCount-1; //One MAC address entry will be for the host.
}
You could ping the device if you know its host-name or its IP address.
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("ping -c 1 " + hostname);
proc.waitFor();
You could do an IP address scan, trying every IP address on the network for a response using a ping like above or trying to connect using TCP or UDP.
If you know the MAC address, you could use the ARP table.
If you got some own software running on the devices, you could send out UDP packets on every device and listen for them on your Android device. See Sending and receiving UDP broadcast packets in Android on how to do this.
You can use accesspoint:
WifiApControl apControl = WifiApControl.getInstance(context);
// These are cached and may no longer be connected, see
// WifiApControl.getReachableClients(int, ReachableClientListener)
List<WifiApControl.Client> clients = apControl.getClients()
精彩评论