Created
January 4, 2017 07:31
-
-
Save EXJUSTICE/0c42da764e26ef8a51417c6a9e5f3a76 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.xu.consent; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.net.NetworkInfo; | |
import android.net.wifi.p2p.WifiP2pDevice; | |
import android.net.wifi.p2p.WifiP2pManager; | |
import android.net.wifi.p2p.WifiP2pManager.Channel; | |
import android.net.wifi.p2p.WifiP2pManager.PeerListListener; | |
import android.util.Log; | |
/** | |
* Main hub for connecting info | |
* Has not only client info but also Host capability | |
*/ | |
public class WifiDirectBroadcastReceiver extends BroadcastReceiver { | |
private WifiP2pManager manager; | |
private Channel channel; | |
private WIFIDirectActivity activity; | |
//Constructor called by WIFIDirectActivity, passing self reference, manager, and channel too | |
public WifiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel, WIFIDirectActivity activity){ | |
super(); | |
this.manager = manager; | |
this.channel = channel; | |
this.activity=activity; | |
} | |
//Intent which is broadcasted by DirectActivity, tells us what to do | |
@Override | |
public void onReceive(Context context, Intent intent){ | |
String action = intent.getAction(); | |
if(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)){ | |
//Update UI to show WifiP2p status | |
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE,-1); | |
if(state == WifiP2pManager.WIFI_P2P_STATE_ENABLED){ | |
//Wifi Direct Enabled | |
//we keep the WifiP2pEnabled boolean within the activity, | |
//So it wont change, and can be followed | |
activity.setIsWifiP2pEnabled(true); | |
}else{ | |
activity.setIsWifiP2pEnabled(false); | |
activity.resetData(); | |
} | |
Log.d(WIFIDirectActivity.TAG,"P2P state changed" +state); | |
}else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)){ | |
/*Request actual peers from P2p manager. | |
Asynchronous call, so its independent from calling activity | |
Callback result notified on WifiP2pManager.PeerListListener.onPeersAvailable() | |
*/ | |
if(manager != null) { | |
//We are typeCaasting because ListFragment implements PeerListListener, | |
//so it fulfills argument requirement. | |
//The host where you display peers should have the PeerListListener, unless | |
//You're passing them over one by one | |
manager.requestPeers(channel, (PeerListListener) activity.getFragmentManager() | |
.findFragmentById(R.id.frag_list)); | |
} | |
Log.d(WIFIDirectActivity.TAG,"P2P peers changed"); | |
//Final intent condition, check if we have an actual CONNECTION ATTEMPT | |
}else if(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)){ | |
if(manager != null){ | |
return; | |
} | |
//Fetch networkinfo just to check if connected | |
NetworkInfo networkInfo = (NetworkInfo)intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO); | |
if(networkInfo.isConnected()){ | |
//Connection established, request connection info to find GROUP OWNER IP | |
DeviceDetailFragment fragment = (DeviceDetailFragment)activity | |
.getFragmentManager().findFragmentById(R.id.frag_detail); | |
/*Now request Connection Info like group ownership etc. PASSED TO | |
DEVICEDETAILFRAGMENT ONCONNECTIONAVAILABLE!!!!! | |
requstConnectionInfo takes arguments Channel and ConnectionInfoListener, | |
which in this case is an instance of our DeviceDetailFragment | |
*/ | |
Intent startConsentActivity = new Intent(context, ConsentActivity.class); | |
context.startActivity(startConsentActivity); | |
//Now that we're connected, requestConnectionINfo to get group owner IP | |
manager.requestConnectionInfo(channel,fragment); | |
//WifiDirectActivity.logconnect()? May be unecessary as we w ould be doing it twice | |
}else{ | |
activity.resetData(); | |
} | |
}else if(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)){ | |
DeviceListFragment fragment = (DeviceListFragment) activity.getFragmentManager() | |
.findFragmentById(R.id.frag_list); | |
fragment.updateThisDevice((WifiP2pDevice) intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment