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

WifiP2pDnsSdServiceInfo

public class WifiP2pDnsSdServiceInfo extends WifiP2pServiceInfo
A class for storing Bonjour service information that is advertised over a Wi-Fi peer-to-peer setup. {@see android.net.wifi.p2p.WifiP2pManager#addLocalService} {@see android.net.wifi.p2p.WifiP2pManager#removeLocalService} {@see WifiP2pServiceInfo} {@see WifiP2pUpnpServiceInfo}

Fields Summary
public static final int
VERSION_1
Bonjour version 1.
public static final int
DNS_TYPE_PTR
Pointer record.
public static final int
DNS_TYPE_TXT
Text record.
private static final Map
sVmPacket
virtual memory packet. see E.3 of the Wi-Fi Direct technical specification for the detail.
Key: domain name Value: pointer address.
Constructors Summary
private WifiP2pDnsSdServiceInfo(List queryList)
This constructor is only used in newInstance().

param
queryList


     
        sVmPacket = new HashMap<String, String>();
        sVmPacket.put("_tcp.local.", "c00c");
        sVmPacket.put("local.", "c011");
        sVmPacket.put("_udp.local.", "c01c");
    
        super(queryList);
    
Methods Summary
private static java.lang.StringcompressDnsName(java.lang.String dnsName)
Compress DNS data. see E.3 of the Wi-Fi Direct technical specification for the detail.

param
dnsName dns name
return
compressed dns name

        StringBuffer sb = new StringBuffer();

        // The domain name is replaced with a pointer to a prior
        // occurrence of the same name in virtual memory packet.
        while (true) {
            String data = sVmPacket.get(dnsName);
            if (data != null) {
                sb.append(data);
                break;
            }
            int i = dnsName.indexOf('.");
            if (i == -1) {
                if (dnsName.length() > 0) {
                    sb.append(String.format(Locale.US, "%02x", dnsName.length()));
                    sb.append(WifiP2pServiceInfo.bin2HexStr(dnsName.getBytes()));
                }
                // for a sequence of labels ending in a zero octet
                sb.append("00");
                break;
            }

            String name = dnsName.substring(0, i);
            dnsName = dnsName.substring(i + 1);
            sb.append(String.format(Locale.US, "%02x", name.length()));
            sb.append(WifiP2pServiceInfo.bin2HexStr(name.getBytes()));
        }
        return sb.toString();
    
private static java.lang.StringcreatePtrServiceQuery(java.lang.String instanceName, java.lang.String serviceType)
Create wpa_supplicant service query for PTR record.

param
instanceName instance name.
e.g) "MyPrinter"
param
serviceType service type.
e.g) "_ipp._tcp"
return
wpa_supplicant service query.


        StringBuffer sb = new StringBuffer();
        sb.append("bonjour ");
        sb.append(createRequest(serviceType + ".local.", DNS_TYPE_PTR, VERSION_1));
        sb.append(" ");

        byte[] data = instanceName.getBytes();
        sb.append(String.format(Locale.US, "%02x", data.length));
        sb.append(WifiP2pServiceInfo.bin2HexStr(data));
        // This is the start point of this response.
        // Therefore, it indicates the request domain name.
        sb.append("c027");
        return sb.toString();
    
static java.lang.StringcreateRequest(java.lang.String dnsName, int dnsType, int version)
Create bonjour service discovery request.

param
dnsName dns name
param
dnsType dns type
param
version version number
hide

        StringBuffer sb = new StringBuffer();

        /*
         * The request format is as follows.
         * ________________________________________________
         * |  Encoded and Compressed dns name (variable)  |
         * ________________________________________________
         * |   Type (2)           | Version (1) |
         */
        if (dnsType == WifiP2pDnsSdServiceInfo.DNS_TYPE_TXT) {
            dnsName = dnsName.toLowerCase(Locale.ROOT); // TODO: is this right?
        }
        sb.append(compressDnsName(dnsName));
        sb.append(String.format(Locale.US, "%04x", dnsType));
        sb.append(String.format(Locale.US, "%02x", version));

        return sb.toString();
    
private static java.lang.StringcreateTxtServiceQuery(java.lang.String instanceName, java.lang.String serviceType, android.net.nsd.DnsSdTxtRecord txtRecord)
Create wpa_supplicant service query for TXT record.

param
instanceName instance name.
e.g) "MyPrinter"
param
serviceType service type.
e.g) "_ipp._tcp"
param
txtRecord TXT record.
return
wpa_supplicant service query.



        StringBuffer sb = new StringBuffer();
        sb.append("bonjour ");

        sb.append(createRequest((instanceName + "." + serviceType + ".local."),
                DNS_TYPE_TXT, VERSION_1));
        sb.append(" ");
        byte[] rawData = txtRecord.getRawData();
        if (rawData.length == 0) {
            sb.append("00");
        } else {
            sb.append(bin2HexStr(rawData));
        }
        return sb.toString();
    
public static android.net.wifi.p2p.nsd.WifiP2pDnsSdServiceInfonewInstance(java.lang.String instanceName, java.lang.String serviceType, java.util.Map txtMap)
Create a Bonjour service information object.

param
instanceName instance name.
e.g) "MyPrinter"
param
serviceType service type.
e.g) "_ipp._tcp"
param
txtMap TXT record with key/value pair in a map confirming to format defined at http://files.dns-sd.org/draft-cheshire-dnsext-dns-sd.txt
return
Bonjour service information object

        if (TextUtils.isEmpty(instanceName) || TextUtils.isEmpty(serviceType)) {
            throw new IllegalArgumentException(
                    "instance name or service type cannot be empty");
        }

        DnsSdTxtRecord txtRecord = new DnsSdTxtRecord();
        if (txtMap != null) {
            for (String key : txtMap.keySet()) {
                txtRecord.set(key, txtMap.get(key));
            }
        }

        ArrayList<String> queries = new ArrayList<String>();
        queries.add(createPtrServiceQuery(instanceName, serviceType));
        queries.add(createTxtServiceQuery(instanceName, serviceType, txtRecord));

        return new WifiP2pDnsSdServiceInfo(queries);