WifiP2pDnsSdServiceInfopublic 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_1Bonjour version 1. | public static final int | DNS_TYPE_PTRPointer record. | public static final int | DNS_TYPE_TXTText record. | private static final Map | sVmPacketvirtual 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().
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.String | compressDnsName(java.lang.String dnsName)Compress DNS data.
see E.3 of the Wi-Fi Direct technical specification for the detail.
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.String | createPtrServiceQuery(java.lang.String instanceName, java.lang.String serviceType)Create wpa_supplicant service query for PTR record.
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.String | createRequest(java.lang.String dnsName, int dnsType, int version)Create bonjour service discovery request.
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.String | createTxtServiceQuery(java.lang.String instanceName, java.lang.String serviceType, android.net.nsd.DnsSdTxtRecord txtRecord)Create wpa_supplicant service query for TXT record.
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.WifiP2pDnsSdServiceInfo | newInstance(java.lang.String instanceName, java.lang.String serviceType, java.util.Map txtMap)Create a 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);
|
|