@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) { // Check to see if Wi-Fi is enabled and notify appropriate activity int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1); if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) { activity.setServerWifiStatus("Wifi Direct is enabled"); } else { activity.setServerWifiStatus("Wifi Direct is not enabled"); } } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) { // Call WifiP2pManager.requestPeers() to get a list of current peers } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) { NetworkInfo networkState = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO); if (networkState.isConnected()) { activity.setServerStatus("Connection Status: Connected"); } else { activity.setServerStatus("Connection Status: Disconnected"); manager.cancelConnect(channel, null); } } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) { // Respond to this device's wifi state changing } }
@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) { // Check to see if Wi-Fi is enabled and notify appropriate activity int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1); if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) { // Wi-Fi P2P is enabled activity.setIsWifiP2pEnabled(true); } else { // Wi-Fi P2P is not enabled activity.setIsWifiP2pEnabled(false); activity.resetDeviceList(); } Log.d(TAG, "P2P state changed - " + state); } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) { // The peer list has changed. // Request available peers from the Wi-Fi P2P manager. This is an // asynchronous call and the calling activity is notified with a // callback on PeerListListener.onPeersAvailable() if (manager != null) { manager.requestPeers( channel, (WifiP2pManager.PeerListListener) activity.getFragmentManager().findFragmentById(R.id.frag_speaker_devices)); } Log.d(TAG, "P2P peers changed"); } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) { // Respond to new connection or disconnections if (manager == null) { return; } NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO); if (networkInfo.isConnected()) { // We are connected with the other device. // Request connection info to find group owner IP ClientDeviceListFragment fragment = (ClientDeviceListFragment) activity.getFragmentManager().findFragmentById(R.id.frag_speaker_devices); manager.requestConnectionInfo(channel, fragment); } else { // It's a disconnect activity.resetDeviceList(); activity.disconnect(); } } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) { // Respond to this device's Wi-Fi state changing ClientDeviceListFragment fragment = (ClientDeviceListFragment) activity.getFragmentManager().findFragmentById(R.id.frag_speaker_devices); fragment.updateThisDevice( (WifiP2pDevice) intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE)); } }
/** * Receive Wifi broadcasts * * @param context * @param intent */ @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) { // request available peers from the wifi p2p manager. This is an++ // asynchronous call and the calling activity is notified with a // callback on PeerListListener.onPeersAvailable() if (mWifiManager != null) { mWifiManager.requestPeers(mChannel, this); } } else if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) { // Update ui to show the wifip2p status int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1); if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) { // P2p wifi is enabled... } else { // P2p wifi is disabled... resetViews(); } } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) { if (mWifiManager == null) { return; } NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO); if (networkInfo.isConnected()) { // devices are connected SendImageFragment fragment = (SendImageFragment) getFragmentManager().findFragmentById(R.id.fragment_send_image); mWifiManager.requestConnectionInfo(mChannel, fragment); } else { // devices were disconnected mWifiManager.removeGroup(mChannel, null); resetViews(); } } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) { // Get the information about this device WifiP2pDevice myDevice = (WifiP2pDevice) intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE); } }