예제 #1
0
 public WifiP2pConfig createFromParcel(Parcel in) {
   WifiP2pConfig config = new WifiP2pConfig();
   config.deviceAddress = in.readString();
   config.wps = (WpsInfo) in.readParcelable(null);
   config.groupOwnerIntent = in.readInt();
   config.persist = Persist.valueOf(in.readString());
   return config;
 }
예제 #2
0
 /**
  * Try to connect to a wifi direct device
  *
  * @param device the wifi direct devie from api android
  */
 public void connectTo(WifiP2pDevice device) {
   WifiP2pConfig config = new WifiP2pConfig();
   config.deviceAddress = device.deviceAddress;
   config.groupOwnerIntent = WifiDirectProperty.CLIENT_GO;
   wifiP2pManager.connect(this.wifiP2PChannel, config, actionListener);
   new Timer()
       .schedule(
           new TimerTask() {
             @Override
             public void run() {
               if (status == WifiDirectStatus.DISCONNECTED) {
                 cancelConnectionTo();
               }
             }
           },
           new Date((new Date()).getTime() + WifiDirectProperty.TIMER));
 }
예제 #3
0
  /**
   * Manages connection to the given device
   *
   * @param device
   */
  public void connect(WifiP2pDevice device) {
    WifiP2pConfig config = new WifiP2pConfig();
    config.deviceAddress = device.deviceAddress;
    config.wps.setup = WpsInfo.PBC;
    config.groupOwnerIntent = 15;
    manager.connect(
        channel,
        config,
        new ActionListener() {

          public void onSuccess() {
            showToast("Richiesta di connessione effettuata");
          }

          public void onFailure(int reason) {
            showToast("Impossibile Connettersi reason = " + reason);
          }
        });
  }
예제 #4
0
        @Override
        public void onReceive(final Context context, final Intent intent) {
          Log.d("nico", "REception de l'intent");
          WifiP2pConfig config = new WifiP2pConfig();
          WifiP2pDevice device = intent.getParcelableExtra("Device");
          config.deviceAddress = device.deviceAddress;
          config.wps.setup = WpsInfo.PBC;
          config.groupOwnerIntent = 0;

          manager.connect(
              channel,
              config,
              new WifiP2pManager.ActionListener() {
                @Override
                public void onSuccess() {
                  Log.d("clem", "Je suis connecté");
                }

                @Override
                public void onFailure(int reason) {}
              });
        }
  /**
   * This function is called repeatedly after each asynchronous operation until all preconditions
   * for the connection have been satisfied and the connection is established (or not).
   */
  private void updateConnection() {
    // Step 0. Stop scans if necessary to prevent interference while connected.
    // Resume scans later when no longer attempting to connect.
    updateScanState();

    // Step 1. Before we try to connect to a new device, tell the system we
    // have disconnected from the old one.
    if ((mRemoteDisplay != null || mExtRemoteDisplay != null)
        && mConnectedDevice != mDesiredDevice) {
      Slog.i(
          TAG,
          "Stopped listening for RTSP connection on "
              + mRemoteDisplayInterface
              + " from Wifi display: "
              + mConnectedDevice.deviceName);

      if (mRemoteDisplay != null) {
        mRemoteDisplay.dispose();
      } else if (mExtRemoteDisplay != null) {
        ExtendedRemoteDisplayHelper.dispose(mExtRemoteDisplay);
      }

      mExtRemoteDisplay = null;
      mRemoteDisplay = null;
      mRemoteDisplayInterface = null;
      mRemoteDisplayConnected = false;
      mHandler.removeCallbacks(mRtspTimeout);

      mWifiP2pManager.setMiracastMode(WifiP2pManager.MIRACAST_DISABLED);
      unadvertiseDisplay();

      // continue to next step
    }

    // Step 2. Before we try to connect to a new device, disconnect from the old one.
    if (mDisconnectingDevice != null) {
      return; // wait for asynchronous callback
    }
    if (mConnectedDevice != null && mConnectedDevice != mDesiredDevice) {
      Slog.i(TAG, "Disconnecting from Wifi display: " + mConnectedDevice.deviceName);
      mDisconnectingDevice = mConnectedDevice;
      mConnectedDevice = null;
      mConnectedDeviceGroupInfo = null;

      unadvertiseDisplay();

      final WifiP2pDevice oldDevice = mDisconnectingDevice;
      mWifiP2pManager.removeGroup(
          mWifiP2pChannel,
          new ActionListener() {
            @Override
            public void onSuccess() {
              Slog.i(TAG, "Disconnected from Wifi display: " + oldDevice.deviceName);
              next();
            }

            @Override
            public void onFailure(int reason) {
              Slog.i(
                  TAG,
                  "Failed to disconnect from Wifi display: "
                      + oldDevice.deviceName
                      + ", reason="
                      + reason);
              next();
            }

            private void next() {
              if (mDisconnectingDevice == oldDevice) {
                mDisconnectingDevice = null;
                updateConnection();
              }
            }
          });
      return; // wait for asynchronous callback
    }

    // Step 3. Before we try to connect to a new device, stop trying to connect
    // to the old one.
    if (mCancelingDevice != null) {
      return; // wait for asynchronous callback
    }
    if (mConnectingDevice != null && mConnectingDevice != mDesiredDevice) {
      Slog.i(TAG, "Canceling connection to Wifi display: " + mConnectingDevice.deviceName);
      mCancelingDevice = mConnectingDevice;
      mConnectingDevice = null;

      unadvertiseDisplay();
      mHandler.removeCallbacks(mConnectionTimeout);

      final WifiP2pDevice oldDevice = mCancelingDevice;
      mWifiP2pManager.cancelConnect(
          mWifiP2pChannel,
          new ActionListener() {
            @Override
            public void onSuccess() {
              Slog.i(TAG, "Canceled connection to Wifi display: " + oldDevice.deviceName);
              next();
            }

            @Override
            public void onFailure(int reason) {
              Slog.i(
                  TAG,
                  "Failed to cancel connection to Wifi display: "
                      + oldDevice.deviceName
                      + ", reason="
                      + reason);
              next();
            }

            private void next() {
              if (mCancelingDevice == oldDevice) {
                mCancelingDevice = null;
                updateConnection();
              }
            }
          });
      return; // wait for asynchronous callback
    }

    // Step 4. If we wanted to disconnect, or we're updating after starting an
    // autonomous GO, then mission accomplished.
    if (mDesiredDevice == null) {
      if (mWifiDisplayCertMode) {
        mListener.onDisplaySessionInfo(getSessionInfo(mConnectedDeviceGroupInfo, 0));
      }
      unadvertiseDisplay();
      return; // done
    }

    // Step 5. Try to connect.
    if (mConnectedDevice == null && mConnectingDevice == null) {
      Slog.i(TAG, "Connecting to Wifi display: " + mDesiredDevice.deviceName);

      mConnectingDevice = mDesiredDevice;
      WifiP2pConfig config = new WifiP2pConfig();
      WpsInfo wps = new WpsInfo();
      if (mWifiDisplayWpsConfig != WpsInfo.INVALID) {
        wps.setup = mWifiDisplayWpsConfig;
      } else if (mConnectingDevice.wpsPbcSupported()) {
        wps.setup = WpsInfo.PBC;
      } else if (mConnectingDevice.wpsDisplaySupported()) {
        // We do keypad if peer does display
        wps.setup = WpsInfo.KEYPAD;
      } else {
        wps.setup = WpsInfo.DISPLAY;
      }
      config.wps = wps;
      config.deviceAddress = mConnectingDevice.deviceAddress;
      // Helps with STA & P2P concurrency
      config.groupOwnerIntent = WifiP2pConfig.MIN_GROUP_OWNER_INTENT;

      WifiDisplay display = createWifiDisplay(mConnectingDevice);
      advertiseDisplay(display, null, 0, 0, 0);

      final WifiP2pDevice newDevice = mDesiredDevice;
      mWifiP2pManager.connect(
          mWifiP2pChannel,
          config,
          new ActionListener() {
            @Override
            public void onSuccess() {
              // The connection may not yet be established.  We still need to wait
              // for WIFI_P2P_CONNECTION_CHANGED_ACTION.  However, we might never
              // get that broadcast, so we register a timeout.
              Slog.i(TAG, "Initiated connection to Wifi display: " + newDevice.deviceName);

              mHandler.postDelayed(mConnectionTimeout, CONNECTION_TIMEOUT_SECONDS * 1000);
            }

            @Override
            public void onFailure(int reason) {
              if (mConnectingDevice == newDevice) {
                Slog.i(
                    TAG,
                    "Failed to initiate connection to Wifi display: "
                        + newDevice.deviceName
                        + ", reason="
                        + reason);
                mConnectingDevice = null;
                handleConnectionFailure(false);
              }
            }
          });
      return; // wait for asynchronous callback
    }

    // Step 6. Listen for incoming RTSP connection.
    if (mConnectedDevice != null && mRemoteDisplay == null && mExtRemoteDisplay == null) {
      Inet4Address addr = getInterfaceAddress(mConnectedDeviceGroupInfo);
      if (addr == null) {
        Slog.i(
            TAG,
            "Failed to get local interface address for communicating "
                + "with Wifi display: "
                + mConnectedDevice.deviceName);
        handleConnectionFailure(false);
        return; // done
      }

      mWifiP2pManager.setMiracastMode(WifiP2pManager.MIRACAST_SOURCE);

      final WifiP2pDevice oldDevice = mConnectedDevice;
      final int port = getPortNumber(mConnectedDevice);
      final String iface = addr.getHostAddress() + ":" + port;
      mRemoteDisplayInterface = iface;

      Slog.i(
          TAG,
          "Listening for RTSP connection on "
              + iface
              + " from Wifi display: "
              + mConnectedDevice.deviceName);

      RemoteDisplay.Listener listener =
          new RemoteDisplay.Listener() {
            @Override
            public void onDisplayConnected(
                Surface surface, int width, int height, int flags, int session) {
              if (mConnectedDevice == oldDevice && !mRemoteDisplayConnected) {
                Slog.i(
                    TAG,
                    "Opened RTSP connection with Wifi display: " + mConnectedDevice.deviceName);
                mRemoteDisplayConnected = true;
                mHandler.removeCallbacks(mRtspTimeout);

                if (mWifiDisplayCertMode) {
                  mListener.onDisplaySessionInfo(
                      getSessionInfo(mConnectedDeviceGroupInfo, session));
                }

                final WifiDisplay display = createWifiDisplay(mConnectedDevice);
                advertiseDisplay(display, surface, width, height, flags);
              }
            }

            @Override
            public void onDisplayDisconnected() {
              if (mConnectedDevice == oldDevice) {
                Slog.i(
                    TAG,
                    "Closed RTSP connection with Wifi display: " + mConnectedDevice.deviceName);
                mHandler.removeCallbacks(mRtspTimeout);
                disconnect();
              }
            }

            @Override
            public void onDisplayError(int error) {
              if (mConnectedDevice == oldDevice) {
                Slog.i(
                    TAG,
                    "Lost RTSP connection with Wifi display due to error "
                        + error
                        + ": "
                        + mConnectedDevice.deviceName);
                mHandler.removeCallbacks(mRtspTimeout);
                handleConnectionFailure(false);
              }
            }
          };
      if (ExtendedRemoteDisplayHelper.isAvailable()) {
        mExtRemoteDisplay = ExtendedRemoteDisplayHelper.listen(iface, listener, mHandler, mContext);
      } else {
        mRemoteDisplay =
            RemoteDisplay.listen(iface, listener, mHandler, mContext.getOpPackageName());
      }

      // Use extended timeout value for certification, as some tests require user inputs
      int rtspTimeout =
          mWifiDisplayCertMode ? RTSP_TIMEOUT_SECONDS_CERT_MODE : RTSP_TIMEOUT_SECONDS;

      mHandler.postDelayed(mRtspTimeout, rtspTimeout * 1000);
    }
  }