2016-04-21 91 views
1

当任何对等或客户端连接到android热点时,android中是否有任何广播。Android HotSpot连接的客户端

就像我已经创建了热点,并希望等到任何对等或客户端连接到它。那么,我将如何知道任何对等点已连接。

回答

0

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) { 

}    
} 
+0

此广播不工作(android.net.wifi.WIFI_HOTSPOT_CLIENTS_CHANGED)和我已经在使用第二个方法使用的TimerTask但我期待这样的广播(android.net.wifi.WIFI_HOTSPOT_CLIENTS_CHANGED),但它不工作 –

+0

您是否请求了权限:ACCESS_WIFI_STATE,CHANGE_WIFI_STATE,CHANGE_WIFI_MULTICAST_STATE? – Fiil

+0

是的,我已经使用了所有三种权限,并且正在使用Android M 6.0进行测试。但没有奏效。 –