FileDocCategorySizeDatePackage
WifiP2pGroup.javaAPI DocAndroid 5.1 API10608Thu Mar 12 22:22:44 GMT 2015android.net.wifi.p2p

WifiP2pGroup

public class WifiP2pGroup extends Object implements android.os.Parcelable
A class representing a Wi-Fi P2p group. A p2p group consists of a single group owner and one or more clients. In the case of a group with only two devices, one will be the group owner and the other will be a group client. {@see WifiP2pManager}

Fields Summary
public static final int
TEMPORARY_NET_ID
The temporary network id. {@hide}
public static final int
PERSISTENT_NET_ID
The persistent network id. If a matching persistent profile is found, use it. Otherwise, create a new persistent profile. {@hide}
private String
mNetworkName
The network name
private WifiP2pDevice
mOwner
Group owner
private boolean
mIsGroupOwner
Device is group owner
private List
mClients
Group clients
private String
mPassphrase
The passphrase used for WPA2-PSK
private String
mInterface
private int
mNetId
The network id in the wpa_supplicant
private static final Pattern
groupStartedPattern
P2P group started string pattern
public static final Creator
CREATOR
Implement the Parcelable interface
Constructors Summary
public WifiP2pGroup()


      
    
public WifiP2pGroup(String supplicantEvent)

param
supplicantEvent formats supported include P2P-GROUP-STARTED p2p-wlan0-0 [client|GO] ssid="DIRECT-W8" freq=2437 [psk=2182b2e50e53f260d04f3c7b25ef33c965a3291b9b36b455a82d77fd82ca15bc| passphrase="fKG4jMe3"] go_dev_addr=fa:7b:7a:42:02:13 [PERSISTENT] P2P-GROUP-REMOVED p2p-wlan0-0 [client|GO] reason=REQUESTED P2P-INVITATION-RECEIVED sa=fa:7b:7a:42:02:13 go_dev_addr=f8:7b:7a:42:02:13 bssid=fa:7b:7a:42:82:13 unknown-network P2P-INVITATION-RECEIVED sa=b8:f9:34:2a:c7:9d persistent=0 Note: The events formats can be looked up in the wpa_supplicant code
hide


        String[] tokens = supplicantEvent.split(" ");

        if (tokens.length < 3) {
            throw new IllegalArgumentException("Malformed supplicant event");
        }

        if (tokens[0].startsWith("P2P-GROUP")) {
            mInterface = tokens[1];
            mIsGroupOwner = tokens[2].equals("GO");

            Matcher match = groupStartedPattern.matcher(supplicantEvent);
            if (!match.find()) {
                return;
            }

            mNetworkName = match.group(1);
            //freq and psk are unused right now
            //int freq = Integer.parseInt(match.group(2));
            //String psk = match.group(3);
            mPassphrase = match.group(4);
            mOwner = new WifiP2pDevice(match.group(5));
            if (match.group(6) != null) {
                mNetId = PERSISTENT_NET_ID;
            } else {
                mNetId = TEMPORARY_NET_ID;
            }
        } else if (tokens[0].equals("P2P-INVITATION-RECEIVED")) {
            String sa = null;
            mNetId = PERSISTENT_NET_ID;
            for (String token : tokens) {
                String[] nameValue = token.split("=");
                if (nameValue.length != 2) continue;

                if (nameValue[0].equals("sa")) {
                    sa = nameValue[1];

                    // set source address into the client list.
                    WifiP2pDevice dev = new WifiP2pDevice();
                    dev.deviceAddress = nameValue[1];
                    mClients.add(dev);
                    continue;
                }

                if (nameValue[0].equals("go_dev_addr")) {
                    mOwner = new WifiP2pDevice(nameValue[1]);
                    continue;
                }

                if (nameValue[0].equals("persistent")) {
                    mOwner = new WifiP2pDevice(sa);
                    mNetId = Integer.parseInt(nameValue[1]);
                    continue;
                }
            }
        } else {
            throw new IllegalArgumentException("Malformed supplicant event");
        }
    
public WifiP2pGroup(WifiP2pGroup source)
copy constructor

        if (source != null) {
            mNetworkName = source.getNetworkName();
            mOwner = new WifiP2pDevice(source.getOwner());
            mIsGroupOwner = source.mIsGroupOwner;
            for (WifiP2pDevice d : source.getClientList()) mClients.add(d);
            mPassphrase = source.getPassphrase();
            mInterface = source.getInterface();
            mNetId = source.getNetworkId();
        }
    
Methods Summary
public voidaddClient(WifiP2pDevice device)

hide

        for (WifiP2pDevice client : mClients) {
            if (client.equals(device)) return;
        }
        mClients.add(device);
    
public voidaddClient(java.lang.String address)

hide

        addClient(new WifiP2pDevice(address));
    
public booleancontains(WifiP2pDevice device)

hide
Returns {@code true} if the device is part of the group

        if (mOwner.equals(device) || mClients.contains(device)) return true;
        return false;
    
public intdescribeContents()
Implement the Parcelable interface

        return 0;
    
public java.util.CollectiongetClientList()
Get the list of clients currently part of the p2p group

        return Collections.unmodifiableCollection(mClients);
    
public java.lang.StringgetInterface()
Get the interface name on which the group is created

        return mInterface;
    
public intgetNetworkId()

hide

        return mNetId;
    
public java.lang.StringgetNetworkName()
Get the network name (SSID) of the group. Legacy Wi-Fi clients will discover the p2p group using the network name.

        return mNetworkName;
    
public WifiP2pDevicegetOwner()
Get the details of the group owner as a {@link WifiP2pDevice} object

        return mOwner;
    
public java.lang.StringgetPassphrase()
Get the passphrase of the group. This function will return a valid passphrase only at the group owner. Legacy Wi-Fi clients will need this passphrase alongside network name obtained from {@link #getNetworkName()} to join the group

        return mPassphrase;
    
public booleanisClientListEmpty()

hide

        return mClients.size() == 0;
    
public booleanisGroupOwner()
Check whether this device is the group owner of the created p2p group

        return mIsGroupOwner;
    
public booleanremoveClient(java.lang.String address)

hide

        return mClients.remove(new WifiP2pDevice(address));
    
public booleanremoveClient(WifiP2pDevice device)

hide

        return mClients.remove(device);
    
public voidsetInterface(java.lang.String intf)

hide

        mInterface = intf;
    
public voidsetIsGroupOwner(boolean isGo)

hide

        mIsGroupOwner = isGo;
    
public voidsetNetworkId(int netId)

hide

        this.mNetId = netId;
    
public voidsetNetworkName(java.lang.String networkName)

hide

        mNetworkName = networkName;
    
public voidsetOwner(WifiP2pDevice device)

hide

        mOwner = device;
    
public voidsetPassphrase(java.lang.String passphrase)

hide

        mPassphrase = passphrase;
    
public java.lang.StringtoString()

        StringBuffer sbuf = new StringBuffer();
        sbuf.append("network: ").append(mNetworkName);
        sbuf.append("\n isGO: ").append(mIsGroupOwner);
        sbuf.append("\n GO: ").append(mOwner);
        for (WifiP2pDevice client : mClients) {
            sbuf.append("\n Client: ").append(client);
        }
        sbuf.append("\n interface: ").append(mInterface);
        sbuf.append("\n networkId: ").append(mNetId);
        return sbuf.toString();
    
public voidwriteToParcel(android.os.Parcel dest, int flags)
Implement the Parcelable interface

        dest.writeString(mNetworkName);
        dest.writeParcelable(mOwner, flags);
        dest.writeByte(mIsGroupOwner ? (byte) 1: (byte) 0);
        dest.writeInt(mClients.size());
        for (WifiP2pDevice client : mClients) {
            dest.writeParcelable(client, flags);
        }
        dest.writeString(mPassphrase);
        dest.writeString(mInterface);
        dest.writeInt(mNetId);