【发布时间】:2016-04-21 15:29:27
【问题描述】:
当任何对等方或客户端连接到android热点时,android中是否有任何广播。
就像我创建了热点并想等到任何对等方或客户端连接到它。那么,我将如何了解任何已连接的对等方。
【问题讨论】:
标签: android
当任何对等方或客户端连接到android热点时,android中是否有任何广播。
就像我创建了热点并想等到任何对等方或客户端连接到它。那么,我将如何了解任何已连接的对等方。
【问题讨论】:
标签: android
1) 你可以使用 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);
2) 或者,您可以扫描已连接设备的列表,并在有变化时不时进行比较。
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("Address "+ splitted[i]);*/
}
}
} catch(Exception e) {
}
}
【讨论】: