2017-04-21 252 views
1

我试图实现一种方法来侦听智能手机热点上的客户端连接事件。我发现android.net.wifi.WIFI_HOTSPOT_CLIENTS_CHANGED不再可用。我怎样才能做到这一点?我认为这是可能的,因为智能手机通知我当我的客户端连接到智能手机热点。android.net.wifi.WIFI_HOTSPOT_CLIENTS_CHANGED替代方案

回答

0

您不能使用意图操作...您必须使用自定义方法,我建议您创建一个后台线程,不断检查/读取IP表(/ proc/net/arp)并更新你......这是我用过的一个片段。

读腹腔给药名单表

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();