2017-04-21 226 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();