Skip to content

Instantly share code, notes, and snippets.

@curioustechizen
Created April 26, 2013 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save curioustechizen/5465780 to your computer and use it in GitHub Desktop.
Save curioustechizen/5465780 to your computer and use it in GitHub Desktop.
Using reflection to try and get the DnsSdTxtRecord object from NdsServiceInfo while using [android.net.nsd](http://developer.android.com/reference/android/net/nsd/NsdManager.html) for performing mDNS discovery. This sample uses [fest-reflect](https://github.com/alexruiz/fest-reflect) for reflection.
import static org.fest.reflect.core.Reflection.*;
import android.net.nsd.NsdServiceInfo;
public class ReflectionUtils {
public static int extractTxtRecordFromServiceInfo(NsdServiceInfo serviceInfo) {
Class<?> dnsSdTxtRecordType = type("android.net.nsd.DnsSdTxtRecord")
.load();
Object txtRecord =
method("getTxtRecord")
.withReturnType(dnsSdTxtRecordType)
.in(serviceInfo)
.invoke();
int keycount = 0;
if (txtRecord != null) {
method("getKeyCount")
.withReturnType(int.class)
.in(txtRecord)
.invoke();
}
return keycount;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment