Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Need help for a native binding

softlionsoftlion FRBeta ✭✭✭

I looked at samples from https://developer.xamarin.com/guides/android/advanced_topics/java_integration_overview/working_with_jni/

But can't understand how to do it properly.

Android definition from android source code:

package android.net;

public class NetworkUtils {
public native static boolean runDhcp(String interfaceName, DhcpInfoInternal ipInfo);
}

Posts

  • BerayBentesenBerayBentesen TRUniversity ✭✭✭✭

    @Softlion which library is that ?

  • softlionsoftlion FRBeta ✭✭✭

    From android standard java lib

  • BerayBentesenBerayBentesen TRUniversity ✭✭✭✭

    Xamarin has standart Android libraries, why you're trying to bind additionally ? If it's a library that Xamarin hasn't published and you're sure that it's an Android library, you better share name of it and I can try binding that library.

  • softlionsoftlion FRBeta ✭✭✭

    Can't you just read the question ? Did you see runDhcp in the Xamarin lib ? No. So ?

  • BerayBentesenBerayBentesen TRUniversity ✭✭✭✭

    @softlion You're giving link that has many topics which is not specific and not about your question since you don't share any error or anything about binding.

    package android.net;

    public class NetworkUtils {
    public native static boolean runDhcp(String interfaceName, DhcpInfoInternal ipInfo);
    }

    This is nothing when you're talking about binding a library since Xamarin support android.net by default so I think You can't just search Google to check whether it supports runDhcp or not.

    Anyway, you could check documentation instead of writing

    Can't you just read the question ? Did you see runDhcp in the Xamarin lib ? No. So ?

    Being rude won't solve your problem. Good luck.

  • softlionsoftlion FRBeta ✭✭✭

    I know i'm rude, but you are asking questions that does not lead to an answer to the original question.

    Basically you're writing that I'm dumb and Xamarin provides all what's needed. Obviously not as i'm asking a taugh question.

    I've included all informations needed:

    • class android.net.NetworkUtils is not available in Xamarin but is available in Android.
    • runDhcp is not available with Xamarin but is available in Android.
    • The full .NET network management stack does not provide this platform feature on Android, not nuget packages, not components

    This code is required to get the list of dns servers. The .NET methods in framework or nuget packages just don't return the value.

  • BerayBentesenBerayBentesen TRUniversity ✭✭✭✭

    @softlion check here

  • softlionsoftlion FRBeta ✭✭✭
    edited November 2016

    I will thank Patrick Gilhuijs on link for his solution on how to query linux system properties and this stackoverflow thread for the right way to get the dns ips on Android.

    I posted a new issue here (dotnet corefx) as it is a bug in the System.Net.NetworkInformation nuget when targeting a linux environment.

    And finally my solution to query dns on android, not using any bindings as i still don't know how to write them:

        public static IEnumerable<IPEndPoint> GetDnsServers()
        {
            var dnsEntries = new List<string>();
            try
            {
                var systemPropertiesClass = Java.Lang.Class.ForName("android.os.SystemProperties");
                var getMethod = systemPropertiesClass.GetMethod("get", Java.Lang.Class.FromType(typeof(Java.Lang.String)));
                for (var i = 1; i <8; i++)
                {
                    var value = ((Java.Lang.String) getMethod.Invoke(null, $"net.dns{i}"))?.ToString();
                    if (!String.IsNullOrEmpty(value) && !dnsEntries.Contains(value))
                        dnsEntries.Add(value);
                }
            }
            catch (Java.Lang.Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.Message);
            }
    
            return dnsEntries.Select(entry => new IPEndPoint(IPAddress.Parse(entry), 53)).ToList();
    
            //This code uses the nuget package System.Net.NetworkInformation v4.3.0, but it does not work on Android
            //var nets = NetworkInterface.GetAllNetworkInterfaces().ToList();
            //foreach (var adapter in nets)
            //{
            //    if (adapter.OperationalStatus == OperationalStatus.Up)
            //    {
            //        var ipProps = adapter.GetIPProperties();
            //        if (ipProps != null)
            //        {
            //            foreach (var ipAddr in ipProps.DnsAddresses) //DnsAddresses is always empty, bug in the nuget package
            //            {
            //                if (ipAddr.AddressFamily == AddressFamily.InterNetwork
            //                    || (ipAddr.AddressFamily == AddressFamily.InterNetworkV6 && (ipAddr.IsIPv4MappedToIPv6 || ipAddr.IsIPv6Multicast || ipAddr.IsIPv6Teredo)))
            //                {
            //                    yield return new IPEndPoint(ipAddr, 53);
            //                }
            //            }
            //        }
            //    }
            //}
        }
    
Sign In or Register to comment.