FileDocCategorySizeDatePackage
WifiP2pServiceResponse.javaAPI DocAndroid 5.1 API12348Thu Mar 12 22:22:44 GMT 2015android.net.wifi.p2p.nsd

WifiP2pServiceResponse

public class WifiP2pServiceResponse extends Object implements android.os.Parcelable
The class for a response of service discovery.
hide

Fields Summary
private static int
MAX_BUF_SIZE
protected int
mServiceType
Service type. It's defined in table63 in Wi-Fi Direct specification.
protected int
mStatus
Status code of service discovery response. It's defined in table65 in Wi-Fi Direct specification.
protected int
mTransId
Service transaction ID. This is a nonzero value used to match the service request/response TLVs.
protected android.net.wifi.p2p.WifiP2pDevice
mDevice
Source device.
protected byte[]
mData
Service discovery response data based on the requested on the service protocol type. The protocol format depends on the service type.
public static final Creator
CREATOR
Implement the Parcelable interface {@hide}
Constructors Summary
protected WifiP2pServiceResponse(int serviceType, int status, int transId, android.net.wifi.p2p.WifiP2pDevice device, byte[] data)
Hidden constructor. This is only used in framework.

param
serviceType service discovery type.
param
status status code.
param
transId transaction id.
param
device source device.
param
data query data.

        mServiceType = serviceType;
        mStatus = status;
        mTransId = transId;
        mDevice = device;
        mData = data;
    
Methods Summary
public intdescribeContents()
Implement the Parcelable interface {@hide}

        return 0;
    
public booleanequals(java.lang.Object o)

        if (o == this) {
            return true;
        }
        if (!(o instanceof WifiP2pServiceResponse)) {
            return false;
        }

        WifiP2pServiceResponse req = (WifiP2pServiceResponse)o;

        return (req.mServiceType == mServiceType) &&
            (req.mStatus == mStatus) &&
                equals(req.mDevice.deviceAddress, mDevice.deviceAddress) &&
                Arrays.equals(req.mData, mData);
    
private booleanequals(java.lang.Object a, java.lang.Object b)

        if (a == null && b == null) {
            return true;
        } else if (a != null) {
            return a.equals(b);
        }
        return false;
    
public byte[]getRawData()
Return response data.
Data format depends on service type

return
a query or response data.

        return mData;
    
public intgetServiceType()
Return the service type of service discovery response.

return
service discovery type.
e.g) {@link WifiP2pServiceInfo#SERVICE_TYPE_BONJOUR}

        return mServiceType;
    
public android.net.wifi.p2p.WifiP2pDevicegetSrcDevice()
Returns the source device of service discovery response.
This is valid only when service discovery response.

return
the source device of service discovery response.

        return mDevice;
    
public intgetStatus()
Return the status code of service discovery response.

return
status code.
see
Status

        return mStatus;
    
public intgetTransactionId()
Return the transaction id of service discovery response.

return
transaction id.
hide

        return mTransId;
    
public inthashCode()

        int result = 17;
        result = 31 * result + mServiceType;
        result = 31 * result + mStatus;
        result = 31 * result + mTransId;
        result = 31 * result + (mDevice.deviceAddress == null ?
                0 : mDevice.deviceAddress.hashCode());
        result = 31 * result + (mData == null ? 0 : mData.hashCode());
        return result;
    
private static byte[]hexStr2Bin(java.lang.String hex)
Converts hex string to byte array.

param
hex hex string. if invalid, return null.
return
binary data.

        int sz = hex.length()/2;
        byte[] b = new byte[hex.length()/2];

        for (int i=0;i<sz;i++) {
            try {
                b[i] = (byte)Integer.parseInt(hex.substring(i*2, i*2+2), 16);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return b;
    
public static java.util.ListnewInstance(java.lang.String supplicantEvent)
Create the list of WifiP2pServiceResponse instance from supplicant event.
The format is as follows.
P2P-SERV-DISC-RESP <address> <update indicator> <response data>
e.g) P2P-SERV-DISC-RESP 02:03:7f:11:62:da 1 0300000101

param
supplicantEvent wpa_supplicant event string.
return
if parse failed, return null
hide


        List<WifiP2pServiceResponse> respList = new ArrayList<WifiP2pServiceResponse>();
        String[] args = supplicantEvent.split(" ");
        if (args.length != 4) {
            return null;
        }
        WifiP2pDevice dev = new WifiP2pDevice();
        String srcAddr = args[1];
        dev.deviceAddress = srcAddr;
        //String updateIndicator = args[2];//not used.
        byte[] bin = hexStr2Bin(args[3]);
        if (bin == null) {
            return null;
        }

        DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bin));
        try {
            while (dis.available() > 0) {
                /*
                 * Service discovery header is as follows.
                 * ______________________________________________________________
                 * |           Length(2byte)     | Type(1byte) | TransId(1byte)}|
                 * ______________________________________________________________
                 * | status(1byte)  |            vendor specific(variable)      |
                 */
                // The length equals to 3 plus the number of octets in the vendor
                // specific content field. And this is little endian.
                int length = (dis.readUnsignedByte() +
                        (dis.readUnsignedByte() << 8)) - 3;
                int type = dis.readUnsignedByte();
                int transId = dis.readUnsignedByte();
                int status = dis.readUnsignedByte();
                if (length < 0) {
                    return null;
                }
                if (length == 0) {
                    if (status == Status.SUCCESS) {
                        respList.add(new WifiP2pServiceResponse(type, status,
                            transId, dev, null));
                    }
                    continue;
                }
                if (length > MAX_BUF_SIZE) {
                    dis.skip(length);
                    continue;
                }
                byte[] data = new byte[length];
                dis.readFully(data);

                WifiP2pServiceResponse resp;
                if (type ==  WifiP2pServiceInfo.SERVICE_TYPE_BONJOUR) {
                    resp = WifiP2pDnsSdServiceResponse.newInstance(status,
                            transId, dev, data);
                } else if (type == WifiP2pServiceInfo.SERVICE_TYPE_UPNP) {
                    resp = WifiP2pUpnpServiceResponse.newInstance(status,
                            transId, dev, data);
                } else {
                    resp = new WifiP2pServiceResponse(type, status, transId, dev, data);
                }
                if (resp != null && resp.getStatus() == Status.SUCCESS) {
                    respList.add(resp);
                }
            }
            return respList;
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (respList.size() > 0) {
            return respList;
        }
        return null;
    
public voidsetSrcDevice(android.net.wifi.p2p.WifiP2pDevice dev)

hide

        if (dev == null) return;
        this.mDevice = dev;
    
public java.lang.StringtoString()

        StringBuffer sbuf = new StringBuffer();
        sbuf.append("serviceType:").append(mServiceType);
        sbuf.append(" status:").append(Status.toString(mStatus));
        sbuf.append(" srcAddr:").append(mDevice.deviceAddress);
        sbuf.append(" data:").append(mData);
        return sbuf.toString();
    
public voidwriteToParcel(android.os.Parcel dest, int flags)
Implement the Parcelable interface {@hide}

        dest.writeInt(mServiceType);
        dest.writeInt(mStatus);
        dest.writeInt(mTransId);
        dest.writeParcelable(mDevice, flags);
        if (mData == null || mData.length == 0) {
            dest.writeInt(0);
        } else {
            dest.writeInt(mData.length);
            dest.writeByteArray(mData);
        }