当设备连接到你的android wifi网络共享APP时如何获得通知?

时间:2014-03-14 08:15:33

标签: android wifi tethering

我在我的Android设备中启用了我的应用程序的wi-fi网络共享。当有人连接到我的应用程序所连接的Wi-Fi网络时,如何在我的应用程序中收到通知?我需要注册一些特定的广播接收器吗?

我已粘贴在应用程序源代码

下面
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((ToggleButton)   findViewById(R.id.toggle_tethering)).setOnCheckedChangeListener(this);
}

@Override

public void onCheckedChanged(CompoundButton button, boolean isChecked) {

   WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
   Boolean result = false;
   WifiConfiguration config = new WifiConfiguration();
   config.SSID = "Tab3OpenWifi";
   config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
   String setWifiApConfigurationMethodName = "setWifiApConfiguration";
   Method setWifiApConfigurationMethod = wifiManager.getClass().getMethod(setWifiApConfigurationMethodName, WifiConfiguration.class);
   result = (Boolean) setWifiApConfigurationMethod.invoke(wifiManager, config);

   if (result) {
      String setWifiApEnableMethodName = "setWifiApEnabled";
      Method setWifiApEnableMethod = wifiManager.getClass().getMethod(setWifiApEnableMethodName, WifiConfiguration.class, boolean.class);
      String message;
      if (isChecked) {
         result = (Boolean) setWifiApEnableMethod.invoke(wifiManager, null, true);
         if (result) {
             message = "Enabling tethering successfully";
         } else {
             message = "Enabling tethering failed";
         }
     } else {
         result = (Boolean) setWifiApEnableMethod.invoke(wifiManager, null, false);
         if (result) {
            message = "Disabling tethering successfully";
         } else {
            message = "Disabling tethering failed";
         }
     }
     Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
   } else {
     Toast.makeText(this, "Failed to update Wifi Tethering config.", Toast.LENGTH_SHORT).show();
   }
}

我使用下面的广播接收器来获得AP状态。

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

    @Override

    public void onReceive(Context context, Intent intent) {

        Log.d(TAG,"mReceiver.onReceive>>");
        String action = intent.getAction();
        if (WIFI_AP_STATE_CHANGED_ACTION.equals(action)) {
            Log.d(TAG,"WIFI_AP_STATE_CHANGED_ACTION");
        } else if (ACTION_TETHER_STATE_CHANGED.equals(action)) {
            ArrayList<String> available = intent.getStringArrayListExtra(
                    EXTRA_AVAILABLE_TETHER);
            ArrayList<String> active = intent.getStringArrayListExtra(
                    EXTRA_ACTIVE_TETHER);
            ArrayList<String> errored = intent.getStringArrayListExtra(
                    EXTRA_ERRORED_TETHER);

            Log.d(TAG,"==Available==");
            for(String str : available)
                Log.d(TAG, ""+str);

            Log.d(TAG,"==Active==");
            for(String str : active)
                Log.d(TAG, ""+active);

            Log.d(TAG,"==Error==");
            for(String str : errored)
                Log.d(TAG, ""+str);

        } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {

        }
        Log.d(TAG,"mReceiver.onReceive<<");
    }
};

1 个答案:

答案 0 :(得分:1)

我认为没有相关的API,也没有任何可以收听的广播事件。

我能想到的唯一选择是在较低级别(即内核级别)运行。我不是专家,但我曾经监视 / proc / net / tcp 文件以查找打开的连接。这可能是你的起点。

如果你想了解我是如何做到的,我还写了一个简单的库。您可以在此处找到它:https://github.com/dextorer/AndroidTCPSourceApp