Android12 监听热点已连接的设备信息

0 阅读1分钟

使用系统API,可获取到ip与连接的设备名称

TetheringManager tm = mContext.getSystemService(TetheringManager.class);

tm.registerTetheringEventCallback(
        Executors.newSingleThreadExecutor(),
        new TetheringManager.TetheringEventCallback() {
            @Override
            public void onClientsChanged(Collection<TetheredClient> clients) {
                Log.i(TAG, "onClientsChanged size:" + clients);
                for (TetheredClient c : clients) {
                    MacAddress mac = c.getMacAddress();
                    List<TetheredClient.AddressInfo> addrs = c.getAddresses();
                    Log.d(TAG, "client mac=" + mac + ", addrs " + addrs);
                    if (addrs != null) {
                        for (TetheredClient.AddressInfo la : addrs) {
                            Log.d(TAG, "ip=" + la.getAddress().getAddress().getHostAddress()
                                    + ", hostname=" + la.getHostname()
                            );
                        }
                    }
                }
            }
        });