我正在尝试实现一种在智能手机热点上侦听客户端连接事件的方法。我看到,android.net.wifi.WIFI_HOTSPOT_CLIENTS_CHANGED不再是无价之宝。我该怎么做?我认为这是可能的,因为当我的客户端连接到智能手机热点时,智能手机会通知我。
发布于 2017-05-02 15:07:01
您不能使用意图Action...You必须使用自定义方法,我建议您创建一个后台线程,不断检查/读取I.P表(/proc/net/arp)并更新您.下面是我使用的一个片段。
阅读i.p列表表
public ArrayList<String> getConnectedDevices() {
ArrayList<String> arrayList = new ArrayList();
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader("/proc/net/arp"));
while (true) {
String readLine = bufferedReader.readLine();
if (readLine == null) {
break;
}
String[] split = readLine.split(" +");
if (split != null && split.length >= 4) {
arrayList.add(split[0]);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return arrayList;
}创建可运行的检查
class CheckHotSpotConnection implements Runnable {
private CheckHotSpotConnection() {
}
public void run() {
int i = 0;
while (discoverClient()) {
i = getConnectedDevices().size();
if (i > 1) {
//client discovered
//disable client discovery to end thread
} else {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}启动线程
new Thread(new CheckHotSpotConnection()).start();https://stackoverflow.com/questions/43537451
复制相似问题