Search in sources :

Example 1 with WifiP2pConfig

use of android.net.wifi.p2p.WifiP2pConfig in project android_frameworks_base by ResurrectionRemix.

the class WifiDisplayController method updateConnection.

/**
     * 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() {
    if (DEBUGV) {
        //new Throwable("WFD_DBG").printStackTrace();
        StackTraceElement[] st = Thread.currentThread().getStackTrace();
        for (int i = 2; i < st.length && i < 5; i++) {
            Slog.i(TAG, st[i].toString());
        }
        dump();
    }
    // Step 0. Stop scans if necessary to prevent interference while connected.
    // Resume scans later when no longer attempting to connect.
    updateScanState();
    // have disconnected from the old one.
    if ((mRemoteDisplay != null || mExtRemoteDisplay != null) && (mConnectedDevice != mDesiredDevice) || (mRemoteDisplayInterface != null && mConnectedDevice == null)) {
        Slog.i(TAG, "Stopped listening for RTSP connection on " + mRemoteDisplayInterface);
        if (mRemoteDisplay != null) {
            mRemoteDisplay.dispose();
        } else if (mExtRemoteDisplay != null) {
            ExtendedRemoteDisplayHelper.dispose(mExtRemoteDisplay);
        }
        mExtRemoteDisplay = null;
        mRemoteDisplay = null;
        mRemoteDisplayInterface = null;
        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 (mRemoteDisplayConnected || mDisconnectingDevice != null) {
        // wait for asynchronous callback
        return;
    }
    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();
                }
            }
        });
        // wait for asynchronous callback
        return;
    }
    // to the old one.
    if (mCancelingDevice != null) {
        // wait for asynchronous callback
        return;
    }
    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();
                }
            }
        });
        // wait for asynchronous callback
        return;
    }
    // autonomous GO, then mission accomplished.
    if (mDesiredDevice == null) {
        if (mWifiDisplayCertMode) {
            mListener.onDisplaySessionInfo(getSessionInfo(mConnectedDeviceGroupInfo, 0));
        }
        unadvertiseDisplay();
        // done
        return;
    }
    //Before we connect, we need to set the oldDevice to the desiredDevice to check
    //the device on receiving callbacks from the Remote display modules
    final WifiP2pDevice oldDevice = mDesiredDevice;
    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);
                mRemoteDisplayConnected = false;
                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);
            }
        }
    };
    // 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);
        if (ExtendedRemoteDisplayHelper.isAvailable() && mExtRemoteDisplay == null) {
            final int port = getPortNumber(mDesiredDevice);
            //IP is superfluous for WFD source, and we don't have one at this stage anyway since
            //P2P connection hasn't been established yet
            final String iface = "255.255.255.255:" + port;
            mRemoteDisplayInterface = iface;
            Slog.i(TAG, "Listening for RTSP connection on " + iface + " from Wifi display: " + mDesiredDevice.deviceName);
            mExtRemoteDisplay = ExtendedRemoteDisplayHelper.listen(iface, listener, mHandler, mContext);
        }
        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);
                }
            }
        });
        // wait for asynchronous callback
        return;
    }
    // Step 6. Listen for incoming RTSP connection.
    if (mConnectedDevice != null && mRemoteDisplay == 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);
            // done
            return;
        }
        mWifiP2pManager.setMiracastMode(WifiP2pManager.MIRACAST_SOURCE);
        final int port = getPortNumber(mConnectedDevice);
        final String iface = addr.getHostAddress() + ":" + port;
        mRemoteDisplayInterface = iface;
        if (!ExtendedRemoteDisplayHelper.isAvailable()) {
            Slog.i(TAG, "Listening for RTSP connection on " + iface + " from Wifi display: " + mConnectedDevice.deviceName);
            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);
    }
}
Also used : RemoteDisplay(android.media.RemoteDisplay) Inet4Address(java.net.Inet4Address) PeerListListener(android.net.wifi.p2p.WifiP2pManager.PeerListListener) GroupInfoListener(android.net.wifi.p2p.WifiP2pManager.GroupInfoListener) ActionListener(android.net.wifi.p2p.WifiP2pManager.ActionListener) WifiP2pDevice(android.net.wifi.p2p.WifiP2pDevice) WifiP2pConfig(android.net.wifi.p2p.WifiP2pConfig) StackTraceElement(java.lang.StackTraceElement) Surface(android.view.Surface) ActionListener(android.net.wifi.p2p.WifiP2pManager.ActionListener) WifiDisplay(android.hardware.display.WifiDisplay) WpsInfo(android.net.wifi.WpsInfo)

Example 2 with WifiP2pConfig

use of android.net.wifi.p2p.WifiP2pConfig in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class WifiP2pSettings method onPreferenceTreeClick.

@Override
public boolean onPreferenceTreeClick(Preference preference) {
    if (preference instanceof WifiP2pPeer) {
        mSelectedWifiPeer = (WifiP2pPeer) preference;
        if (mSelectedWifiPeer.device.status == WifiP2pDevice.CONNECTED) {
            showDialog(DIALOG_DISCONNECT);
        } else if (mSelectedWifiPeer.device.status == WifiP2pDevice.INVITED) {
            showDialog(DIALOG_CANCEL_CONNECT);
        } else {
            WifiP2pConfig config = new WifiP2pConfig();
            config.deviceAddress = mSelectedWifiPeer.device.deviceAddress;
            int forceWps = SystemProperties.getInt("wifidirect.wps", -1);
            if (forceWps != -1) {
                config.wps.setup = forceWps;
            } else {
                if (mSelectedWifiPeer.device.wpsPbcSupported()) {
                    config.wps.setup = WpsInfo.PBC;
                } else if (mSelectedWifiPeer.device.wpsKeypadSupported()) {
                    config.wps.setup = WpsInfo.KEYPAD;
                } else {
                    config.wps.setup = WpsInfo.DISPLAY;
                }
            }
            mWifiP2pManager.connect(mChannel, config, new WifiP2pManager.ActionListener() {

                public void onSuccess() {
                    if (DBG)
                        Log.d(TAG, " connect success");
                }

                public void onFailure(int reason) {
                    Log.e(TAG, " connect fail " + reason);
                    Toast.makeText(getActivity(), R.string.wifi_p2p_failed_connect_message, Toast.LENGTH_SHORT).show();
                }
            });
        }
    } else if (preference instanceof WifiP2pPersistentGroup) {
        mSelectedGroup = (WifiP2pPersistentGroup) preference;
        showDialog(DIALOG_DELETE_GROUP);
    }
    return super.onPreferenceTreeClick(preference);
}
Also used : WifiP2pConfig(android.net.wifi.p2p.WifiP2pConfig)

Example 3 with WifiP2pConfig

use of android.net.wifi.p2p.WifiP2pConfig in project platform_frameworks_base by android.

the class WifiDisplayController method updateConnection.

/**
     * 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();
    // have disconnected from the old one.
    if (mRemoteDisplay != null && mConnectedDevice != mDesiredDevice) {
        Slog.i(TAG, "Stopped listening for RTSP connection on " + mRemoteDisplayInterface + " from Wifi display: " + mConnectedDevice.deviceName);
        mRemoteDisplay.dispose();
        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) {
        // wait for asynchronous callback
        return;
    }
    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();
                }
            }
        });
        // wait for asynchronous callback
        return;
    }
    // to the old one.
    if (mCancelingDevice != null) {
        // wait for asynchronous callback
        return;
    }
    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();
                }
            }
        });
        // wait for asynchronous callback
        return;
    }
    // autonomous GO, then mission accomplished.
    if (mDesiredDevice == null) {
        if (mWifiDisplayCertMode) {
            mListener.onDisplaySessionInfo(getSessionInfo(mConnectedDeviceGroupInfo, 0));
        }
        unadvertiseDisplay();
        // done
        return;
    }
    // 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);
                }
            }
        });
        // wait for asynchronous callback
        return;
    }
    // Step 6. Listen for incoming RTSP connection.
    if (mConnectedDevice != null && mRemoteDisplay == 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);
            // done
            return;
        }
        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);
        mRemoteDisplay = RemoteDisplay.listen(iface, 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);
                }
            }
        }, 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);
    }
}
Also used : Inet4Address(java.net.Inet4Address) PeerListListener(android.net.wifi.p2p.WifiP2pManager.PeerListListener) GroupInfoListener(android.net.wifi.p2p.WifiP2pManager.GroupInfoListener) ActionListener(android.net.wifi.p2p.WifiP2pManager.ActionListener) ActionListener(android.net.wifi.p2p.WifiP2pManager.ActionListener) WifiP2pDevice(android.net.wifi.p2p.WifiP2pDevice) WifiP2pConfig(android.net.wifi.p2p.WifiP2pConfig) WifiDisplay(android.hardware.display.WifiDisplay) WpsInfo(android.net.wifi.WpsInfo) Surface(android.view.Surface)

Example 4 with WifiP2pConfig

use of android.net.wifi.p2p.WifiP2pConfig in project physical-web by google.

the class WifiDirectConnect method connect.

public void connect(UrlDevice urlDevice, String title) {
    String progressTitle = mContext.getString(R.string.page_loading_title) + " " + title;
    mProgress = new ProgressDialog(mContext);
    mProgress.setCancelable(true);
    mProgress.setOnCancelListener(new OnCancelListener() {

        @Override
        public void onCancel(DialogInterface dialogInterface) {
            Log.i(TAG, "Dialog box canceled");
            mDevice = null;
            mManager.cancelConnect(mChannel, new ActionListener() {

                @Override
                public void onSuccess() {
                    Log.d(TAG, "cancel connect call success");
                }

                @Override
                public void onFailure(int reason) {
                    Log.d(TAG, "cancel connect call fail " + reason);
                }
            });
        }
    });
    mProgress.setTitle(progressTitle);
    mProgress.setMessage(mContext.getString(R.string.page_loading_message));
    mProgress.show();
    mDevice = urlDevice;
    WifiP2pConfig config = new WifiP2pConfig();
    config.deviceAddress = Utils.getWifiAddress(mDevice);
    config.groupOwnerIntent = 0;
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
    mContext.registerReceiver(mReceiver, intentFilter);
    mManager.requestGroupInfo(mChannel, new WifiP2pManager.GroupInfoListener() {

        public void onGroupInfoAvailable(final WifiP2pGroup group) {
            if (group != null) {
                Log.d(TAG, "group not null");
                if (group.getOwner().deviceAddress.equals(Utils.getWifiAddress(mDevice))) {
                    Log.i(TAG, "Already connected");
                    mManager.requestConnectionInfo(mChannel, new WifiP2pManager.ConnectionInfoListener() {

                        public void onConnectionInfoAvailable(final WifiP2pInfo info) {
                            if (mDevice != null && info.groupOwnerAddress != null) {
                                Intent intent = new Intent(Intent.ACTION_VIEW);
                                intent.setData(Uri.parse("http:/" + info.groupOwnerAddress + ":" + Integer.toString(Utils.getWifiPort(mDevice))));
                                mContext.startActivity(intent);
                            }
                        }
                    });
                } else {
                    mManager.removeGroup(mChannel, new ActionListener() {

                        @Override
                        public void onSuccess() {
                            Log.d(TAG, "remove call success");
                            connectHelper(true, config);
                        }

                        @Override
                        public void onFailure(int reason) {
                            Log.d(TAG, "remove call fail " + reason);
                        }
                    });
                }
            } else {
                Log.d(TAG, "group null");
                connectHelper(true, config);
            }
        }
    });
}
Also used : IntentFilter(android.content.IntentFilter) DialogInterface(android.content.DialogInterface) WifiP2pConfig(android.net.wifi.p2p.WifiP2pConfig) Intent(android.content.Intent) ProgressDialog(android.app.ProgressDialog) WifiP2pInfo(android.net.wifi.p2p.WifiP2pInfo) ActionListener(android.net.wifi.p2p.WifiP2pManager.ActionListener) WifiP2pGroup(android.net.wifi.p2p.WifiP2pGroup) WifiP2pManager(android.net.wifi.p2p.WifiP2pManager) OnCancelListener(android.content.DialogInterface.OnCancelListener)

Example 5 with WifiP2pConfig

use of android.net.wifi.p2p.WifiP2pConfig in project android_frameworks_base by ParanoidAndroid.

the class WifiDisplayController method updateConnection.

/**
     * 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() {
    // have disconnected from the old one.
    if (mRemoteDisplay != null && mConnectedDevice != mDesiredDevice) {
        Slog.i(TAG, "Stopped listening for RTSP connection on " + mRemoteDisplayInterface + " from Wifi display: " + mConnectedDevice.deviceName);
        mRemoteDisplay.dispose();
        mRemoteDisplay = null;
        mRemoteDisplayInterface = null;
        mRemoteDisplayConnected = false;
        mHandler.removeCallbacks(mRtspTimeout);
        mWifiP2pManager.setMiracastMode(WifiP2pManager.MIRACAST_DISABLED);
        setRemoteSubmixOn(false);
        unadvertiseDisplay();
    // continue to next step
    }
    // Step 2. Before we try to connect to a new device, disconnect from the old one.
    if (mDisconnectingDevice != null) {
        // wait for asynchronous callback
        return;
    }
    if (mConnectedDevice != null && mConnectedDevice != mDesiredDevice) {
        Slog.i(TAG, "Disconnecting from Wifi display: " + mConnectedDevice.deviceName);
        mDisconnectingDevice = mConnectedDevice;
        mConnectedDevice = 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();
                }
            }
        });
        // wait for asynchronous callback
        return;
    }
    // to the old one.
    if (mCancelingDevice != null) {
        // wait for asynchronous callback
        return;
    }
    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();
                }
            }
        });
        // wait for asynchronous callback
        return;
    }
    // Step 4. If we wanted to disconnect, then mission accomplished.
    if (mDesiredDevice == null) {
        unadvertiseDisplay();
        // done
        return;
    }
    // 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 (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);
                }
            }
        });
        // wait for asynchronous callback
        return;
    }
    // Step 6. Listen for incoming connections.
    if (mConnectedDevice != null && mRemoteDisplay == 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);
            // done
            return;
        }
        setRemoteSubmixOn(true);
        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);
        mRemoteDisplay = RemoteDisplay.listen(iface, new RemoteDisplay.Listener() {

            @Override
            public void onDisplayConnected(Surface surface, int width, int height, int flags) {
                if (mConnectedDevice == oldDevice && !mRemoteDisplayConnected) {
                    Slog.i(TAG, "Opened RTSP connection with Wifi display: " + mConnectedDevice.deviceName);
                    mRemoteDisplayConnected = true;
                    mHandler.removeCallbacks(mRtspTimeout);
                    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);
                }
            }
        }, mHandler);
        mHandler.postDelayed(mRtspTimeout, RTSP_TIMEOUT_SECONDS * 1000);
    }
}
Also used : Inet4Address(java.net.Inet4Address) PeerListListener(android.net.wifi.p2p.WifiP2pManager.PeerListListener) GroupInfoListener(android.net.wifi.p2p.WifiP2pManager.GroupInfoListener) ActionListener(android.net.wifi.p2p.WifiP2pManager.ActionListener) ActionListener(android.net.wifi.p2p.WifiP2pManager.ActionListener) WifiP2pDevice(android.net.wifi.p2p.WifiP2pDevice) WifiP2pConfig(android.net.wifi.p2p.WifiP2pConfig) WifiDisplay(android.hardware.display.WifiDisplay) WpsInfo(android.net.wifi.WpsInfo) Surface(android.view.Surface)

Aggregations

WifiP2pConfig (android.net.wifi.p2p.WifiP2pConfig)9 ActionListener (android.net.wifi.p2p.WifiP2pManager.ActionListener)7 WifiDisplay (android.hardware.display.WifiDisplay)6 WpsInfo (android.net.wifi.WpsInfo)6 WifiP2pDevice (android.net.wifi.p2p.WifiP2pDevice)6 GroupInfoListener (android.net.wifi.p2p.WifiP2pManager.GroupInfoListener)6 PeerListListener (android.net.wifi.p2p.WifiP2pManager.PeerListListener)6 Surface (android.view.Surface)6 Inet4Address (java.net.Inet4Address)6 RemoteDisplay (android.media.RemoteDisplay)3 StackTraceElement (java.lang.StackTraceElement)3 ProgressDialog (android.app.ProgressDialog)1 DialogInterface (android.content.DialogInterface)1 OnCancelListener (android.content.DialogInterface.OnCancelListener)1 Intent (android.content.Intent)1 IntentFilter (android.content.IntentFilter)1 WifiP2pGroup (android.net.wifi.p2p.WifiP2pGroup)1 WifiP2pInfo (android.net.wifi.p2p.WifiP2pInfo)1 WifiP2pManager (android.net.wifi.p2p.WifiP2pManager)1