怎么听连接到Android设备热点热点、连接到、设备、Android

2023-09-07 00:36:05 作者:人红马子多

下面是我已经用于创建Android的热点在code现在我想自动检测连接设备的IP

  //切换WiFi热点或关闭公共静态布尔configApState(上下文的背景下){    WifiManager wifimanager =(WifiManager)context.getSystemService(context.WIFI_SERVICE);    WifiConfiguration wificonfiguration = NULL;    尝试{        //如果WiFi是上,将其关闭        如果(isApOn(上下文)){            wifimanager.setWifiEnabled(假);        }        方法方法= wifimanager.getClass()实现getMethod(setWifiApEnabled,WifiConfiguration.class,boolean.class)。        method.invoke(wifimanager,wificonfiguration,isApOn(上下文)!);        返回true;    }    赶上(例外五){        e.printStackTrace();    }    返回false;} 

解决方案 新一代 Android 要来了,这 20 个重要新功能你不能不知

您可以使用以下code得到conencted设备:

 公共无效getListOfConnectedDevice(){线程线程=新主题(新的Runnable(){    @覆盖    公共无效的run(){        BR的BufferedReader = NULL;        布尔isFirstLine = TRUE;        尝试{            BR =新的BufferedReader(新的FileReader(的/ proc /净/ ARP));            串线;            而((行= br.readLine())!= NULL){                如果(isFirstLine){                    isFirstLine = FALSE;                    继续;                }                串[]分裂= line.split(+);                如果(分裂= NULL&放大器;!&安培; splitted.length> = 4){                    串ip地址=分裂[0];                    串MACADDRESS =分裂[3];                    布尔isReachable = InetAddress.getByName(                            分裂[0])isReachable(500)。 //这是网络调用,所以我们不能做,关于UI线程,所以我把后台线程。                    如果(isReachable){                        Log.d(设备信息,ip地址+:                                + MACADDRESS);                    }                }            }        }赶上(例外五){            e.printStackTrace();        } {最后            尝试{                br.close();            }赶上(IOException异常五){                e.printStackTrace();            }        }    }});thread.start();} 

来源:链接

Here is the code that i had used to create hotspot in android now i want to detect the ip of devices connected to it

// toggle wifi hotspot on or off
public static boolean configApState(Context context) {

    WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
    WifiConfiguration wificonfiguration = null;
    try {  

        // if WiFi is on, turn it off
        if(isApOn(context)) {               
            wifimanager.setWifiEnabled(false);
        }               
        Method method = wifimanager.getClass().getMethod("setWifiApEnabled",  WifiConfiguration.class, boolean.class);                   
        method.invoke(wifimanager, wificonfiguration, !isApOn(context));
        return true;
    } 
    catch (Exception e) {

        e.printStackTrace();
    }       
    return false;
}

解决方案

You can get the conencted devices using the following code:

public void getListOfConnectedDevice() {
Thread thread = new Thread(new Runnable() {

    @Override
    public void run() {
        BufferedReader br = null;
        boolean isFirstLine = true;

        try {
            br = new BufferedReader(new FileReader("/proc/net/arp"));
            String line;

            while ((line = br.readLine()) != null) {
                if (isFirstLine) {
                    isFirstLine = false;
                    continue;
                }

                String[] splitted = line.split(" +");

                if (splitted != null && splitted.length >= 4) {

                    String ipAddress = splitted[0];
                    String macAddress = splitted[3];

                    boolean isReachable = InetAddress.getByName(
                            splitted[0]).isReachable(500);  // this is network call so we cant do that on UI thread, so i take background thread.
                    if (isReachable) {
                        Log.d("Device Information", ipAddress + " : "
                                + macAddress);
                    }

                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
});
thread.start();
}

source: link