Created
April 26, 2013 08:37
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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