public void connect() {
   if (!packetManager.isRunning()) {
     // Start a timer that attempt a connection on a regular basis
     connectionTimer.schedule(
         new TimerTask() {
           @Override
           public void run() {
             try {
               RobotResolver resolver = RobotResolver.getInstance(context);
               InetAddress robotAddress = resolver.resolve(roboRIOName);
               packetManager.start(robotAddress);
               resolver.stop();
               cancel();
             } catch (UnknownHostException e) {
               Log.d(TAG, "Could not resolve robot IP: " + e);
             } catch (IOException ex) {
               Log.e(TAG, "Failed to initialize networking: " + ex);
             }
           }
         },
         0,
         connectionPeriod);
   }
 }
 public void disconnect() {
   if (packetManager.isRunning()) {
     RobotResolver.getInstance(context).stop();
     packetManager.stop();
   }
 }
  public ConnectionManager(
      final Context context,
      final String roboRIOName,
      final int connectionPeriod,
      final ControlDatabase controlDatabase,
      final ConnectionIndicator connectionIndicator,
      final ModeSwitch modeSwitch,
      final EnableButton enableButton) {
    this.context = context;
    this.roboRIOName = roboRIOName;
    this.connectionPeriod = connectionPeriod;

    driverStationPacket = new DriverStationPacket();
    robotPacket = new RobotPacket();
    packetManager = new PacketManager(driverStationPacket, robotPacket);

    controlDatabase.setControlListener(controlListener);
    modeSwitch.setModeChangeListener(
        new ModeSwitch.ModeChangeListener() {
          @Override
          public void onTeleopEnabled() {
            enableButton.setEnabled(false);
            driverStationPacket.setMode(DriverStationPacket.Mode.TELEOPERATED);
          }

          @Override
          public void onAutoEnabled() {
            enableButton.setEnabled(false);
            driverStationPacket.setMode(DriverStationPacket.Mode.AUTONOMOUS);
          }
        });
    enableButton.addEnableListener(
        new EnableButton.EnableListener() {
          @Override
          public void onEnabled() {
            driverStationPacket.setEnabled(true);
          }

          @Override
          public void onDisabled() {
            driverStationPacket.setEnabled(false);
          }
        });
    packetManager.setConnectionListener(
        new PacketManager.ConnectionListener() {
          @Override
          public void onConnect() {
            new Handler(context.getMainLooper())
                .post(
                    new Runnable() {
                      @Override
                      public void run() {
                        connectionIndicator.setConnected(true);
                        Toast.makeText(context, "Connection Established", Toast.LENGTH_SHORT)
                            .show();
                      }
                    });
          }

          @Override
          public void onConnectionLost() {
            new Handler(context.getMainLooper())
                .post(
                    new Runnable() {
                      @Override
                      public void run() {
                        connectionIndicator.setConnected(false);
                        Toast.makeText(context, "Connection Lost", Toast.LENGTH_SHORT).show();
                        enableButton.setEnabled(false);
                      }
                    });
          }

          @Override
          public void onConnectionFailed() {
            new Handler(context.getMainLooper())
                .post(
                    new Runnable() {
                      @Override
                      public void run() {
                        Toast.makeText(context, "Connection Failed", Toast.LENGTH_SHORT).show();
                        enableButton.setEnabled(false);
                      }
                    });
          }
        });
  }