Android Open Source - expo-connect Wi Fi Direct Broadcast Receiver






From Project

Back to project page expo-connect.

License

The source code is released under:

GNU General Public License

If you think the Android project expo-connect listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package it.polimi.camparollo.expoconnect.wifi;
/* w w  w  .j a  va  2s .  c  o  m*/
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.util.Log;
import android.widget.Toast;
import it.polimi.camparollo.expoconnect.uilisteners.ConnectionInfo;
import it.polimi.camparollo.expoconnect.uilisteners.PeersListUpdater;

public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
  
  private StartActivity activity;
  private Channel channel;
  private WifiP2pManager manager;
  
  private InfoActionListener infoListener;
  private PeersListUpdater peersUpdater;
  private ConnectionInfo connInfo;

  public WiFiDirectBroadcastReceiver(WifiP2pManager manager, WifiP2pManager.Channel channel, StartActivity activity) {
    super();
    this.manager = manager;
    this.channel = channel;
    this.activity = activity;
    this.infoListener = new InfoActionListener("Discovery Initiated", "Discovery Failed", activity);
    this.peersUpdater = new PeersListUpdater(activity);
    this.connInfo = new ConnectionInfo(activity);
  }

  public void onReceive(Context context, Intent intent) {
    
      String action = intent.getAction();
      if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
        int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
        Log.d("ExpoConnect", "P2P state changed - " + state);
        Toast.makeText(activity, "P2P state changed - " + state, Toast.LENGTH_SHORT).show();
        if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
          activity.setWifiP2pEnabled(true);
          manager.discoverPeers(channel, infoListener);
        } else {
            activity.setWifiP2pEnabled(false);
            activity.setIsDeviceConnected(false);
            activity.restartWifiP2p();
        }
      } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
  
          if (manager != null && !activity.isDeviceConnected())
              manager.requestPeers(channel, peersUpdater);
          Log.d("ExpoConnect", "P2P peers changed");
        
      } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
  
          if (manager != null) {
  
            NetworkInfo info = (NetworkInfo) intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
    
            if (info.isConnected()) {
              manager.requestConnectionInfo(channel, connInfo);
                Log.d("ExpoConnect", "P2P connection changed");
            } else {
              activity.setIsDeviceConnected(false);
            }
          }
      } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
        Log.d("ExpoConnect", "P2P device details changed");
      }
  }
}




Java Source Code List

it.polimi.camparollo.expoconnect.datatransfer.IPAddressSender.java
it.polimi.camparollo.expoconnect.datatransfer.RESTClient.java
it.polimi.camparollo.expoconnect.datatransfer.Restaurant.java
it.polimi.camparollo.expoconnect.datatransfer.ScreenDataReceiver.java
it.polimi.camparollo.expoconnect.exceptions.CannotEnableWifiP2pException.java
it.polimi.camparollo.expoconnect.recommendations.RestaurantListActivity.java
it.polimi.camparollo.expoconnect.recommendations.RestaurantListAdapter.java
it.polimi.camparollo.expoconnect.recommendations.SingleRestaurant.java
it.polimi.camparollo.expoconnect.uilisteners.ConnectionInfo.java
it.polimi.camparollo.expoconnect.uilisteners.PeersListUpdater.java
it.polimi.camparollo.expoconnect.utils.ScalingUtilities.java
it.polimi.camparollo.expoconnect.wifi.CustomWifiP2pManager.java
it.polimi.camparollo.expoconnect.wifi.InfoActionListener.java
it.polimi.camparollo.expoconnect.wifi.StartActivity.java
it.polimi.camparollo.expoconnect.wifi.WiFiDirectBroadcastReceiver.java
it.polimi.camparollo.expoconnectserver.CommandReceiver.java
it.polimi.camparollo.expoconnectserver.InfoActionListener.java
it.polimi.camparollo.expoconnectserver.ServiceStarterActivity.java
it.polimi.camparollo.expoconnectserver.uilisteners.ConnectionInfo.java
it.polimi.camparollo.expoconnectserver.uilisteners.PeersListUpdater.java
it.polimi.camparollo.expoconnectserver.wifi.ConnectService.java
it.polimi.camparollo.expoconnectserver.wifi.IPAddressReceiver.java
it.polimi.camparollo.expoconnectserver.wifi.ScreenDataSender.java
it.polimi.camparollo.expoconnectserver.wifi.WiFiDirectBroadcastReceiver.java