我在我的Android应用程序中以编程方式连接不同设备和wifi热点AP,如何以编程方式检测连接和断开连接的客户端以及wifi热点AP? Android API中是否有任何回调事件可以提供有关各个设备的连接或断开连接事件的信息?提前致谢 .
不幸的是,没有公共API提供有关此信息...但您可以读取/ proc / net / arp文件并查看连接到您的接入点的客户端 .
/ proc / net / arp文件有6个字段: IP address , HW type , Flags , HW address , Mask 和 Device
问题是当客户端断开连接时,因为它不会从文件中消失 . 一个解决方案可能是对每个客户端执行ping操作并等待响应,但对我来说这不是对ping的响应 . 如果你喜欢这个解决方案,请在GitHub上检查这个项目 - > https://github.com/nickrussler/Android-Wifi-Hotspot-Manager-Class/tree/master/src/com/whitebyte
我所做的是:读取/ proc / net / arp并检查FLAGS字段,当值为0x2时,工作站已连接且0x0已断开连接,但要刷新此字段,我需要不时清除ARP缓存,并且我用这个命令做到了: ip neigh flush all
我希望我能帮助你
这个方法适合我,但这只检测版本4.0及更高版本;它无法找到与热点连接的2.2或2.3版本的设备 .
public void getClientList() { 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 ) { // Basic sanity check String mac = splitted[3]; System.out.println("Mac : Outside If "+ mac ); if (mac.matches("..:..:..:..:..:..")) { macCount++; /* ClientList.add("Client(" + macCount + ")"); IpAddr.add(splitted[0]); HWAddr.add(splitted[3]); Device.add(splitted[5]);*/ System.out.println("Mac : "+ mac + " IP Address : "+splitted[0] ); System.out.println("Mac_Count " + macCount + " MAC_ADDRESS "+ mac); Toast.makeText( getApplicationContext(), "Mac_Count " + macCount + " MAC_ADDRESS " + mac, Toast.LENGTH_SHORT).show(); } /* for (int i = 0; i < splitted.length; i++) System.out.println("Addressssssss "+ splitted[i]);*/ } } } catch(Exception e) { } }
@SuppressWarnings("ConstantConditions") public static String getClientMacByIP(String ip) { String res = ""; if (ip == null) return res; String flushCmd = "sh ip -s -s neigh flush all"; Runtime runtime = Runtime.getRuntime(); try { runtime.exec(flushCmd,null,new File("/proc/net")); } BufferedReader br; try { br = new BufferedReader(new FileReader("/proc/net/arp")); String line; while ((line = br.readLine()) != null) { String[] sp = line.split(" +"); if (sp.length >= 4 && ip.equals(sp[0])) {Assistance.Log(sp[0]+sp[2]+sp[3],ALERT_STATES.ALERT_STATE_LOG); String mac = sp[3]; if (mac.matches("..:..:..:..:..:..") && sp[2].equals("0x2")) { res = mac; break; } } } br.close(); } catch (Exception e) {} return res; }
// ------------------------------------------------ --------
@SuppressWarnings("ConstantConditions") public static String getClientIPByMac(String mac) { String res = ""; if (mac == null) return res; String flushCmd = "sh ip -s -s neigh flush all"; Runtime runtime = Runtime.getRuntime(); try { runtime.exec(flushCmd,null,new File("/proc/net")); } BufferedReader br; try { br = new BufferedReader(new FileReader("/proc/net/arp")); String line; while ((line = br.readLine()) != null) { String[] sp = line.split(" +"); if (sp.length >= 4 && mac.equals(sp[3])) { String ip = sp[0]; if (ip.matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}") && sp[2].equals("0x2")) { res = ip; break; } } } br.close(); } catch (Exception e) {} return res; }
您可以使用BroadcastReciever“android.net.wifi.WIFI_HOTSPOT_CLIENTS_CHANGED”来检测客户端连接 . 在你的AndroidManifest中:
<receiver android:name=".WiFiConnectionReciever" android:enabled="true" android:exported="true" > <intent-filter> <action android:name="android.net.wifi.WIFI_HOTSPOT_CLIENTS_CHANGED" /> </intent-filter> </receiver>
在你的活动中
IntentFilter mIntentFilter = new IntentFilter(); mIntentFilter.addAction("android.net.wifi.WIFI_HOTSPOT_CLIENTS_CHANGED"); rcv = new WiFiConnectionReciever(); registerReceiver(rcv, mIntentFilter);
4 回答
不幸的是,没有公共API提供有关此信息...但您可以读取/ proc / net / arp文件并查看连接到您的接入点的客户端 .
/ proc / net / arp文件有6个字段: IP address , HW type , Flags , HW address , Mask 和 Device
问题是当客户端断开连接时,因为它不会从文件中消失 . 一个解决方案可能是对每个客户端执行ping操作并等待响应,但对我来说这不是对ping的响应 . 如果你喜欢这个解决方案,请在GitHub上检查这个项目 - > https://github.com/nickrussler/Android-Wifi-Hotspot-Manager-Class/tree/master/src/com/whitebyte
我所做的是:读取/ proc / net / arp并检查FLAGS字段,当值为0x2时,工作站已连接且0x0已断开连接,但要刷新此字段,我需要不时清除ARP缓存,并且我用这个命令做到了: ip neigh flush all
我希望我能帮助你
这个方法适合我,但这只检测版本4.0及更高版本;它无法找到与热点连接的2.2或2.3版本的设备 .
// ------------------------------------------------ --------
您可以使用BroadcastReciever“android.net.wifi.WIFI_HOTSPOT_CLIENTS_CHANGED”来检测客户端连接 . 在你的AndroidManifest中:
在你的活动中