Search in sources :

Example 41 with WifiP2pDevice

use of android.net.wifi.p2p.WifiP2pDevice in project nfcspy by sinpolib.

the class ActivityManageP2P method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_manage_p2p);
    peerdevs = new ArrayList<WifiP2pDevice>();
    peers = new ArrayAdapter<CharSequence>(this, R.layout.listitem_peer);
    eventHelper = new EventHelper();
    ListView lv = ((ListView) findViewById(R.id.list));
    lv.setAdapter(peers);
    lv.setOnItemClickListener(eventHelper);
    thisDevice = (TextView) findViewById(R.id.txtThisdevice);
}
Also used : ListView(android.widget.ListView) WifiP2pDevice(android.net.wifi.p2p.WifiP2pDevice)

Example 42 with WifiP2pDevice

use of android.net.wifi.p2p.WifiP2pDevice in project nfcspy by sinpolib.

the class ActivityManageP2P method handleMessage.

@SuppressWarnings("unchecked")
boolean handleMessage(int type, int status, Object obj) {
    if (type == WiFiP2PCommand.CMD_RequestPeers) {
        resetPeers();
        if (obj != null) {
            for (WifiP2pDevice dev : (Collection<WifiP2pDevice>) obj) {
                peerdevs.add(dev);
                peers.add(getWifiP2pDeviceInfo(dev));
            }
        }
        return true;
    }
    if (type == WiFiP2PCommand.CMD_CancelConnect) {
        new Wifip2pRemoveGroup(eventHelper).execute(p2p);
        return true;
    }
    if (type == WiFiP2PCommand.CMD_RemoveGroup) {
        new Wifip2pDiscoverPeers(eventHelper).execute(p2p);
        return true;
    }
    return true;
}
Also used : WifiP2pDevice(android.net.wifi.p2p.WifiP2pDevice) Collection(java.util.Collection)

Example 43 with WifiP2pDevice

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

the class WifiP2pSettings method handlePeersChanged.

private void handlePeersChanged() {
    mPeerCategoryController.removeAllChildren();
    mConnectedDevices = 0;
    if (DBG)
        Log.d(TAG, "List of available peers");
    for (WifiP2pDevice peer : mPeers.getDeviceList()) {
        if (DBG)
            Log.d(TAG, "-> " + peer);
        mPeerCategoryController.addChild(new WifiP2pPeer(getPrefContext(), peer));
        if (peer.status == WifiP2pDevice.CONNECTED)
            mConnectedDevices++;
    }
    if (DBG)
        Log.d(TAG, " mConnectedDevices " + mConnectedDevices);
}
Also used : WifiP2pDevice(android.net.wifi.p2p.WifiP2pDevice)

Example 44 with WifiP2pDevice

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

the class WifiP2pSettings method onActivityCreated.

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    final Activity activity = getActivity();
    mWifiP2pManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
    if (mWifiP2pManager != null) {
        mChannel = mWifiP2pManager.initialize(activity.getApplicationContext(), getActivity().getMainLooper(), null);
        if (mChannel == null) {
            // Failure to set up connection
            Log.e(TAG, "Failed to set up connection with wifi p2p service");
            mWifiP2pManager = null;
        }
    } else {
        Log.e(TAG, "mWifiP2pManager is null !");
    }
    if (savedInstanceState != null && savedInstanceState.containsKey(SAVE_DIALOG_PEER)) {
        WifiP2pDevice device = savedInstanceState.getParcelable(SAVE_DIALOG_PEER);
        mSelectedWifiPeer = new WifiP2pPeer(getPrefContext(), device);
    }
    if (savedInstanceState != null && savedInstanceState.containsKey(SAVE_DEVICE_NAME)) {
        mSavedDeviceName = savedInstanceState.getString(SAVE_DEVICE_NAME);
    }
    if (savedInstanceState != null && savedInstanceState.containsKey(SAVE_SELECTED_GROUP)) {
        mSelectedGroupName = savedInstanceState.getString(SAVE_SELECTED_GROUP);
    }
    mRenameListener = new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (which == DialogInterface.BUTTON_POSITIVE) {
                if (mWifiP2pManager != null) {
                    String name = mDeviceNameText.getText().toString();
                    if (name != null) {
                        for (int i = 0; i < name.length(); i++) {
                            char cur = name.charAt(i);
                            if (!Character.isDigit(cur) && !Character.isLetter(cur) && cur != '-' && cur != '_' && cur != ' ') {
                                Toast.makeText(getActivity(), R.string.wifi_p2p_failed_rename_message, Toast.LENGTH_LONG).show();
                                return;
                            }
                        }
                    }
                    mWifiP2pManager.setDeviceName(mChannel, mDeviceNameText.getText().toString(), new WifiP2pManager.ActionListener() {

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

                        public void onFailure(int reason) {
                            Toast.makeText(getActivity(), R.string.wifi_p2p_failed_rename_message, Toast.LENGTH_LONG).show();
                        }
                    });
                }
            }
        }
    };
    // disconnect dialog listener
    mDisconnectListener = new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (which == DialogInterface.BUTTON_POSITIVE) {
                if (mWifiP2pManager != null) {
                    mWifiP2pManager.removeGroup(mChannel, new WifiP2pManager.ActionListener() {

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

                        public void onFailure(int reason) {
                            if (DBG)
                                Log.d(TAG, " remove group fail " + reason);
                        }
                    });
                }
            }
        }
    };
    // cancel connect dialog listener
    mCancelConnectListener = new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (which == DialogInterface.BUTTON_POSITIVE) {
                if (mWifiP2pManager != null) {
                    mWifiP2pManager.cancelConnect(mChannel, new WifiP2pManager.ActionListener() {

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

                        public void onFailure(int reason) {
                            if (DBG)
                                Log.d(TAG, " cancel connect fail " + reason);
                        }
                    });
                }
            }
        }
    };
    // delete persistent group dialog listener
    mDeleteGroupListener = new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (which == DialogInterface.BUTTON_POSITIVE) {
                if (mWifiP2pManager != null) {
                    if (mSelectedGroup != null) {
                        if (DBG)
                            Log.d(TAG, " deleting group " + mSelectedGroup.getGroupName());
                        mWifiP2pManager.deletePersistentGroup(mChannel, mSelectedGroup.getNetworkId(), new WifiP2pManager.ActionListener() {

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

                            public void onFailure(int reason) {
                                if (DBG)
                                    Log.d(TAG, " delete group fail " + reason);
                            }
                        });
                        mSelectedGroup = null;
                    } else {
                        if (DBG)
                            Log.w(TAG, " No selected group to delete!");
                    }
                }
            } else if (which == DialogInterface.BUTTON_NEGATIVE) {
                if (DBG) {
                    Log.d(TAG, " forgetting selected group " + mSelectedGroup.getGroupName());
                }
                mSelectedGroup = null;
            }
        }
    };
    super.onActivityCreated(savedInstanceState);
}
Also used : WifiP2pDevice(android.net.wifi.p2p.WifiP2pDevice) DialogInterface(android.content.DialogInterface) Activity(android.app.Activity) OnClickListener(android.content.DialogInterface.OnClickListener)

Example 45 with WifiP2pDevice

use of android.net.wifi.p2p.WifiP2pDevice 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

WifiP2pDevice (android.net.wifi.p2p.WifiP2pDevice)95 Test (org.junit.Test)14 WifiDisplay (android.hardware.display.WifiDisplay)12 Activity (android.app.Activity)7 DialogInterface (android.content.DialogInterface)7 OnClickListener (android.content.DialogInterface.OnClickListener)7 WpsInfo (android.net.wifi.WpsInfo)6 WifiP2pConfig (android.net.wifi.p2p.WifiP2pConfig)6 ActionListener (android.net.wifi.p2p.WifiP2pManager.ActionListener)6 GroupInfoListener (android.net.wifi.p2p.WifiP2pManager.GroupInfoListener)6 PeerListListener (android.net.wifi.p2p.WifiP2pManager.PeerListListener)6 Surface (android.view.Surface)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 DataInputStream (java.io.DataInputStream)6 IOException (java.io.IOException)6 Inet4Address (java.net.Inet4Address)6 ArrayList (java.util.ArrayList)6 RemoteDisplay (android.media.RemoteDisplay)3 StackTraceElement (java.lang.StackTraceElement)3 ListView (android.widget.ListView)1