android.net.wifi.p2p.WifiP2pGroup Java Examples

The following examples show how to use android.net.wifi.p2p.WifiP2pGroup. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NetworkMonitorAutoDetect.java    From webrtc_android with MIT License 6 votes vote down vote up
/** Handle a change notification about the wifi p2p group. */
private void onWifiP2pGroupChange(  WifiP2pGroup wifiP2pGroup) {
  if (wifiP2pGroup == null || wifiP2pGroup.getInterface() == null) {
    return;
  }

  NetworkInterface wifiP2pInterface;
  try {
    wifiP2pInterface = NetworkInterface.getByName(wifiP2pGroup.getInterface());
  } catch (SocketException e) {
    Logging.e(TAG, "Unable to get WifiP2p network interface", e);
    return;
  }

  List<InetAddress> interfaceAddresses = Collections.list(wifiP2pInterface.getInetAddresses());
  IPAddress[] ipAddresses = new IPAddress[interfaceAddresses.size()];
  for (int i = 0; i < interfaceAddresses.size(); ++i) {
    ipAddresses[i] = new IPAddress(interfaceAddresses.get(i).getAddress());
  }

  wifiP2pNetworkInfo =
      new NetworkInformation(wifiP2pGroup.getInterface(), ConnectionType.CONNECTION_WIFI,
          ConnectionType.CONNECTION_NONE, WIFI_P2P_NETWORK_HANDLE, ipAddresses);
  observer.onNetworkConnect(wifiP2pNetworkInfo);
}
 
Example #2
Source File: WifiDisplayController.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private WifiDisplaySessionInfo getSessionInfo(WifiP2pGroup info, int session) {
    if (info == null) {
        return null;
    }
    Inet4Address addr = getInterfaceAddress(info);
    WifiDisplaySessionInfo sessionInfo = new WifiDisplaySessionInfo(
            !info.getOwner().deviceAddress.equals(mThisDevice.deviceAddress),
            session,
            info.getOwner().deviceAddress + " " + info.getNetworkName(),
            info.getPassphrase(),
            (addr != null) ? addr.getHostAddress() : "");
    if (DEBUG) {
        Slog.d(TAG, sessionInfo.toString());
    }
    return sessionInfo;
}
 
Example #3
Source File: WifiDisplayController.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private static Inet4Address getInterfaceAddress(WifiP2pGroup info) {
    NetworkInterface iface;
    try {
        iface = NetworkInterface.getByName(info.getInterface());
    } catch (SocketException ex) {
        Slog.w(TAG, "Could not obtain address of network interface "
                + info.getInterface(), ex);
        return null;
    }

    Enumeration<InetAddress> addrs = iface.getInetAddresses();
    while (addrs.hasMoreElements()) {
        InetAddress addr = addrs.nextElement();
        if (addr instanceof Inet4Address) {
            return (Inet4Address)addr;
        }
    }

    Slog.w(TAG, "Could not obtain address of network interface "
            + info.getInterface() + " because it had no IPv4 addresses.");
    return null;
}
 
Example #4
Source File: WifiDirectHandler.java    From WiFi-Buddy with MIT License 6 votes vote down vote up
public String p2pGroupToString(WifiP2pGroup wifiP2pGroup) {
    if (wifiP2pGroup != null) {
        String strWifiP2pGroup = "Network name: " + wifiP2pGroup.getNetworkName();
        strWifiP2pGroup += "\nIs group owner: " + wifiP2pGroup.isGroupOwner();
        if (wifiP2pGroup.getOwner() != null) {
            strWifiP2pGroup += "\nGroup owner: ";
            strWifiP2pGroup += "\n" + p2pDeviceToString(wifiP2pGroup.getOwner());
        }
        if (wifiP2pGroup.getClientList() != null && !wifiP2pGroup.getClientList().isEmpty()) {
            for (WifiP2pDevice client : wifiP2pGroup.getClientList()) {
                strWifiP2pGroup += "\nClient: ";
                strWifiP2pGroup += "\n" + p2pDeviceToString(client);
            }
        }
        return strWifiP2pGroup;
    } else {
        Log.e(TAG, "WifiP2pGroup is null");
        return "";
    }
}
 
Example #5
Source File: Salut.java    From Salut with MIT License 6 votes vote down vote up
protected void disconnectFromDevice() {
    manager.requestGroupInfo(channel, new WifiP2pManager.GroupInfoListener() {
        @Override
        public void onGroupInfoAvailable(final WifiP2pGroup group) {
            if (group != null) {
                manager.removeGroup(channel, new WifiP2pManager.ActionListener() {
                    @Override
                    public void onSuccess() {
                        isConnectedToAnotherDevice = false;
                        deleteGroup(manager, channel, group);
                        Log.d(TAG, "Removed WiFi Direct Group.");
                    }

                    @Override
                    public void onFailure(int reason) {
                        Log.e(TAG, "Failed to remove a WiFi Direct Group. Reason: " + reason);
                    }
                });
            }
        }
    });
}
 
Example #6
Source File: NetworkMonitorAutoDetect.java    From webrtc_android with MIT License 5 votes vote down vote up
@Override
@SuppressLint("InlinedApi")
public void onReceive(Context context, Intent intent) {
  if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(intent.getAction())) {
    WifiP2pGroup wifiP2pGroup = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_GROUP);
    onWifiP2pGroupChange(wifiP2pGroup);
  } else if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(intent.getAction())) {
    int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, 0 /* default to unknown */);
    onWifiP2pStateChange(state);
  }
}
 
Example #7
Source File: WifiDirectGroupManager.java    From ShareBox with Apache License 2.0 5 votes vote down vote up
@Override
public void onGroupInfoAvailable(WifiP2pGroup group) {
	if(group!=null){
		mHotSpotName=group.getNetworkName();
		mHotSpotPwd=group.getPassphrase();
		mHandler.obtainMessage(Event.GROUP_INFO_AVAILABLE).sendToTarget();
	}else{
		mWifiP2pManager.requestGroupInfo(mChannel, WifiDirectGroupManager.this);
	}
}
 
Example #8
Source File: WifiDirectHandler.java    From WiFi-Buddy with MIT License 5 votes vote down vote up
/**
 * The state of Wi-Fi P2P connectivity has changed
 * Here is where you can request group info
 * Available extras: EXTRA_WIFI_P2P_INFO, EXTRA_NETWORK_INFO, EXTRA_WIFI_P2P_GROUP
 * @param intent
 */
private void handleConnectionChanged(Intent intent) {
    Log.i(TAG, "Wi-Fi P2P Connection Changed");

    if(wifiP2pManager == null) {
        return;
    }

    // Extra information from EXTRA_NETWORK_INFO
    NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
    if(networkInfo.isConnected()) {
        Log.i(TAG, "Connected to P2P network. Requesting connection info");
        wifiP2pManager.requestConnectionInfo(channel, WifiDirectHandler.this);
    } else {
        Intent disconnected = new Intent(Action.COMMUNICATION_DISCONNECTED);
        localBroadcastManager.sendBroadcast(disconnected);
    }

    // Requests peer-to-peer group information
    wifiP2pManager.requestGroupInfo(channel, new WifiP2pManager.GroupInfoListener() {
        @Override
        public void onGroupInfoAvailable(WifiP2pGroup wifiP2pGroup) {
            if (wifiP2pGroup != null) {
                Log.i(TAG, "Group info available");
                Log.i(TAG, "WifiP2pGroup:");
                Log.i(TAG, p2pGroupToString(wifiP2pGroup));
                WifiDirectHandler.this.wifiP2pGroup = wifiP2pGroup;
            }
        }
    });

}
 
Example #9
Source File: Salut.java    From Salut with MIT License 5 votes vote down vote up
@Override
public void onConnectionInfoAvailable(final WifiP2pInfo info) {
    /* This method is automatically called when we connect to a device.
     * The group owner accepts connections using a server socket and then spawns a
     * client socket for every client. This is handled by the registration jobs.
     * This will automatically handle first time connections.*/

    manager.requestGroupInfo(channel, new WifiP2pManager.GroupInfoListener() {
        @Override
        public void onGroupInfoAvailable(WifiP2pGroup group) {

            if (isRunningAsHost && !registrationIsRunning) {
                if (info.groupFormed && !group.getClientList().isEmpty()) {
                    startHostRegistrationServer();
                }
            } else if (!thisDevice.isRegistered && !info.isGroupOwner) {
                if (serviceRequest == null) {
                    //This means that discoverNetworkServices was never called and we're still connected to an old host for some reason.
                    Log.e(Salut.TAG, "This device is still connected to an old host for some reason. A forced disconnect will be attempted.");
                    forceDisconnect();
                }
                Log.v(Salut.TAG, "Successfully connected to another device.");
                startRegistrationForClient(new InetSocketAddress(info.groupOwnerAddress.getHostAddress(), SALUT_SERVER_PORT));
            }
        }
    });
}
 
Example #10
Source File: Salut.java    From Salut with MIT License 5 votes vote down vote up
private void deleteGroup(WifiP2pManager manager, WifiP2pManager.Channel channel, WifiP2pGroup wifiP2pGroup) {
    try {
        Method getNetworkId = WifiP2pGroup.class.getMethod("getNetworkId");
        Integer networkId = (Integer) getNetworkId.invoke(wifiP2pGroup);
        Method deletePersistentGroup = WifiP2pManager.class.getMethod("deletePersistentGroup",
                WifiP2pManager.Channel.class, Integer.class, WifiP2pManager.ActionListener.class);
        deletePersistentGroup.invoke(manager, channel, networkId, null);
    } catch (Exception ex) {
        Log.v(Salut.TAG, "Failed to delete persistent group.");
    }
}
 
Example #11
Source File: WifiDisplayController.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private void handleConnectionChanged(NetworkInfo networkInfo) {
    mNetworkInfo = networkInfo;
    if (mWfdEnabled && networkInfo.isConnected()) {
        if (mDesiredDevice != null || mWifiDisplayCertMode) {
            mWifiP2pManager.requestGroupInfo(mWifiP2pChannel, new GroupInfoListener() {
                @Override
                public void onGroupInfoAvailable(WifiP2pGroup info) {
                    if (DEBUG) {
                        Slog.d(TAG, "Received group info: " + describeWifiP2pGroup(info));
                    }

                    if (mConnectingDevice != null && !info.contains(mConnectingDevice)) {
                        Slog.i(TAG, "Aborting connection to Wifi display because "
                                + "the current P2P group does not contain the device "
                                + "we expected to find: " + mConnectingDevice.deviceName
                                + ", group info was: " + describeWifiP2pGroup(info));
                        handleConnectionFailure(false);
                        return;
                    }

                    if (mDesiredDevice != null && !info.contains(mDesiredDevice)) {
                        disconnect();
                        return;
                    }

                    if (mWifiDisplayCertMode) {
                        boolean owner = info.getOwner().deviceAddress
                                .equals(mThisDevice.deviceAddress);
                        if (owner && info.getClientList().isEmpty()) {
                            // this is the case when we started Autonomous GO,
                            // and no client has connected, save group info
                            // and updateConnection()
                            mConnectingDevice = mDesiredDevice = null;
                            mConnectedDeviceGroupInfo = info;
                            updateConnection();
                        } else if (mConnectingDevice == null && mDesiredDevice == null) {
                            // this is the case when we received an incoming connection
                            // from the sink, update both mConnectingDevice and mDesiredDevice
                            // then proceed to updateConnection() below
                            mConnectingDevice = mDesiredDevice = owner ?
                                    info.getClientList().iterator().next() : info.getOwner();
                        }
                    }

                    if (mConnectingDevice != null && mConnectingDevice == mDesiredDevice) {
                        Slog.i(TAG, "Connected to Wifi display: "
                                + mConnectingDevice.deviceName);

                        mHandler.removeCallbacks(mConnectionTimeout);
                        mConnectedDeviceGroupInfo = info;
                        mConnectedDevice = mConnectingDevice;
                        mConnectingDevice = null;
                        updateConnection();
                    }
                }
            });
        }
    } else {
        mConnectedDeviceGroupInfo = null;

        // Disconnect if we lost the network while connecting or connected to a display.
        if (mConnectingDevice != null || mConnectedDevice != null) {
            disconnect();
        }

        // After disconnection for a group, for some reason we have a tendency
        // to get a peer change notification with an empty list of peers.
        // Perform a fresh scan.
        if (mWfdEnabled) {
            requestPeers();
        }
    }
}
 
Example #12
Source File: WifiDisplayController.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private static String describeWifiP2pGroup(WifiP2pGroup group) {
    return group != null ? group.toString().replace('\n', ',') : "null";
}