gt:             if (fragment.getDevice() == null
context: package com.example.maicol.wifidirect;
  
          import android.annotation.TargetApi;
          import android.app.Activity;
          import android.app.AlertDialog;
          import android.content.BroadcastReceiver;
          import android.content.Context;
          import android.content.Intent;
          import android.content.IntentFilter;
          import android.net.wifi.p2p.WifiP2pConfig;
          import android.net.wifi.p2p.WifiP2pDevice;
          import android.net.wifi.p2p.WifiP2pManager;
          import android.os.Build;
          import android.os.Bundle;
          import android.os.Handler;
          import android.os.Message;
          import android.provider.Settings;
          import android.support.v7.app.AppCompatActivity;
          import android.util.Log;
          import android.view.Menu;
          import android.view.MenuInflater;
          import android.view.MenuItem;
          import android.view.View;
          import android.widget.TextView;
          import android.widget.Toast;
  
          import java.net.DatagramPacket;
          import java.net.DatagramSocket;
  
  @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
  
  public class WiFiDirectActivity extends AppCompatActivity implements WifiP2pManager.ChannelListener, DeviceListFragment.DeviceActionListener {
  
      public static final String TAG = "wifidirectdemo";
      private WifiP2pManager manager;
      private boolean isWifiP2pEnabled = false;
      private boolean retryChannel = false;
  
      private final IntentFilter intentFilter = new IntentFilter();
      private WifiP2pManager.Channel channel;
      private BroadcastReceiver receiver = null;
  
      public static double cont;
      public static String arre[];
      public static DatagramSocket serverSocket;
      public static DatagramPacket receivePacket;
      public static int i=0; //conta il numero di pacchetti
      public static Handler handler;
  
      public long start; //TEMPO di inizio
      public long time_discovery;  //tempo discovery
      public long start2; //tempo dopo la discovery
      public long time_groupformation; //tempo group formation
      public AlertDialog.Builder miaAlert;
      /**
       * @param isWifiP2pEnabled the isWifiP2pEnabled to set
       */
      public void setIsWifiP2pEnabled(boolean isWifiP2pEnabled) {
          this.isWifiP2pEnabled = isWifiP2pEnabled;
          Toast.makeText(WiFiDirectActivity.this, "WIFI ENABLE",Toast.LENGTH_SHORT).show();
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          //getActionBar().show();
  
  
          handler = new MyHandler();
          start=0;
          // add necessary intent values to be matched.
          intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
          intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
          intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
          intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
          manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
          channel = manager.initialize(this, getMainLooper(), null);
  
      }
  
  
      public void discov(String end){  //calcola il tempo necessario ai due dispositivi per trovarsi
          time_discovery = ((Long.valueOf(end))-start)/1000; //secondi
          Toast.makeText(WiFiDirectActivity.this, "SEC = "+time_discovery,Toast.LENGTH_SHORT).show();
      }
  
      public void formation(String role, String format){  //calcola il tempo necessario ai due dispositivi per trovarsi
          time_groupformation = ((Long.valueOf(format))-start2)/1000; //secondi
  
          miaAlert = new AlertDialog.Builder(WiFiDirectActivity.this);
          miaAlert.setTitle("Ruolo: "+role);
          long total = time_groupformation+ time_discovery;
          miaAlert.setMessage("Tempo di discovery: "+time_discovery+"secondi\n"+"Tempo di formazion: "+time_groupformation+"secondi\nTempo totale: "+total);
          AlertDialog alert = miaAlert.create();
          alert.setIcon(R.drawable.search);
          if(!alert.isShowing()){
              alert.show();
          }
      }
  
  
      /** register the BroadcastReceiver with the intent values to be matched */
      @Override
      public void onResume() {
          super.onResume();
          receiver = new WiFiDirectBroadcastReceiver(manager, channel, this);
          registerReceiver(receiver, intentFilter);
      }
  
      @Override
      public void onPause() {
          super.onPause();
          unregisterReceiver(receiver);
      }
  
      public class MyHandler extends Handler {
          @Override
          public void handleMessage(Message msg) {
              Bundle bundle = msg.getData();
              if(bundle.containsKey("innuendo")) {
                  String value = bundle.getString("innuendo");
                  TextView mytext=(TextView)findViewById(R.id.viliri);
                  String ai = (String) mytext.getText();
                  mytext.setText(ai+value);
              }
          }
      }
  
      /**
       * Remove all peers and clear all fields. This is called on
       * BroadcastReceiver receiving a state change event.
       */
      public void resetData() {
          DeviceListFragment fragmentList = (DeviceListFragment) getFragmentManager()
                  .findFragmentById(R.id.frag_list);
          DeviceDetailFragment fragmentDetails = (DeviceDetailFragment) getFragmentManager()
                  .findFragmentById(R.id.frag_detail);
          if (fragmentList != null) {
              fragmentList.clearPeers();
          }
          if (fragmentDetails != null) {
              fragmentDetails.resetViews();
          }
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.action_items, menu);
          return true;
      }
  
  
  
      /*public void wuaglio(){
          DeviceDetailFragment fragment = (DeviceDetailFragment) this.getFragmentManager().findFragmentById(R.id.frag_list);
          manager.requestGroupInfo(channel, fragment);
      } */
      /*
       * (non-Javadoc)
       * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
       */
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
              case R.id.atn_direct_enable:
                  if (manager != null && channel != null) {
  
                      // Since this is the system wireless settings activity, it's
                      // not going to send us a result. We will be notified by
                      // WiFiDeviceBroadcastReceiver instead.
  
                      startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
                  } else {
                      Log.e(TAG, "channel or manager is null");
                  }
                  return true;
  
              case R.id.atn_direct_discover:
                  if (!isWifiP2pEnabled) {
                      Toast.makeText(WiFiDirectActivity.this, R.string.p2p_off_warning,
                              Toast.LENGTH_SHORT).show();
                      return true;
                  }
                  final DeviceListFragment fragment = (DeviceListFragment) getFragmentManager()
                          .findFragmentById(R.id.frag_list);
                  fragment.onInitiateDiscovery();
                  manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
  
                      @Override
                      public void onSuccess() {
                          start =(System.currentTimeMillis()); //Tempo di inizio della discovery
                          Toast.makeText(WiFiDirectActivity.this, "Discovery Initiated",Toast.LENGTH_SHORT).show();
                      }
  
                      @Override
                      public void onFailure(int reasonCode) {
                          Toast.makeText(WiFiDirectActivity.this, "Discovery Failed : " + reasonCode,Toast.LENGTH_SHORT).show();
                      }
                  });
                  return true;
              default:
                  return super.onOptionsItemSelected(item);
          }
      }
  
      public void crea_gruppo(){
  
          manager.createGroup(channel, new WifiP2pManager.ActionListener() {
  
              @Override
              public void onSuccess() {
                  Toast.makeText(WiFiDirectActivity.this, "Success!",
                          Toast.LENGTH_SHORT).show();
              }
  
              @Override
              public void onFailure(int reasonCode) {
                  Toast.makeText(WiFiDirectActivity.this, "Failure!" + reasonCode,
                          Toast.LENGTH_SHORT).show();
              }
          });
      }
  
      @Override
      public void showDetails(WifiP2pDevice device) {
          DeviceDetailFragment fragment = (DeviceDetailFragment) getFragmentManager()
                  .findFragmentById(R.id.frag_detail);
          fragment.showDetails(device);
  
      }
  
      @Override
      public void connect(WifiP2pConfig config) {
          manager.connect(channel, config, new WifiP2pManager.ActionListener() {
  
              @Override
              public void onSuccess() {
                  start2 =(System.currentTimeMillis()); //Tempo di inizio della formazione del gruppo
                  Toast.makeText(WiFiDirectActivity.this, "Group Formation start...",Toast.LENGTH_SHORT).show();
                  // WiFiDirectBroadcastReceiver will notify us. Ignore for now.
              }
  
              @Override
              public void onFailure(int reason) {
                  Toast.makeText(WiFiDirectActivity.this, "Connect failed. Retry.",
                          Toast.LENGTH_SHORT).show();
              }
          });
      }
  
  
      @Override
      public void disconnect() {
          final DeviceDetailFragment fragment = (DeviceDetailFragment) getFragmentManager()
                  .findFragmentById(R.id.frag_detail);
          fragment.resetViews();
          manager.removeGroup(channel, new WifiP2pManager.ActionListener() {
  
              @Override
              public void onFailure(int reasonCode) {
                  Log.d(TAG, "Disconnect failed. Reason :" + reasonCode);
              }
  
              @Override
              public void onSuccess() {
                  fragment.getView().setVisibility(View.GONE);
              }
  
          });
      }
  
      @Override
      public void onChannelDisconnected() {
          // we will try once more
          if (manager != null && !retryChannel) {
              Toast.makeText(this, "Channel lost. Trying again", Toast.LENGTH_LONG).show();
              resetData();
              retryChannel = true;
              manager.initialize(this, getMainLooper(), this);
          } else {
              Toast.makeText(this,
                      "Severe! Channel is probably lost premanently. Try Disable/Re-Enable P2P.",
                      Toast.LENGTH_LONG).show();
          }
      }
  
      @Override
      public void cancelDisconnect() {
  
          /*
           * A cancel abort request by user. Disconnect i.e. removeGroup if
           * already connected. Else, request WifiP2pManager to abort the ongoing
           * request
           */
          if (manager != null) {
              final DeviceListFragment fragment = (DeviceListFragment) getFragmentManager()
                      .findFragmentById(R.id.frag_list);
  