4

I would like to access my device as myandroidthing.local, as I can do it with my (both Linux and Windows) laptops. Is it somehow possible?

Most ideally, it could be done by some app what I can install from an app store.

Responses to the answer:

  • Android.local works. But it finds only one of my Android devices. Is there a way to replace the Android mdsnd with a better configurable one which allows changing the name it advertises?
  • Does running a patched mdnsd require a rooted device?
4
  • Related: Why is "mdnsd" draining my battery and how to stop it? Commented Oct 5, 2021 at 22:59
  • @IrfanLatif Thanks, it helped a lot. I see a lot of apps to scan for mdns / dns-sd, but none which could make my phone visible. That I find quite surprising - if it can send udp packets to 224.0.0.251:5353, why it can't respond to others?
    – peterh
    Commented Oct 6, 2021 at 0:23
  • I haven't used MDNS much except for learning, so can't recommend an app. But as far the Android's official API is documented, it's very simple to broadcast your service as well as to discover other services. There are also third part libraries available like jmdns. This sample app demonstrates the possibilities in very detail. Commented Oct 6, 2021 at 10:26
  • @IrfanLatif That is dns-sd (service discovery). This question is about mdns (multicast dns). But the first probably includes the last. If it is correct, then any app which can advertise itself on dns-sd, will be also make my phone referable as myandroidthing.local .
    – peterh
    Commented Oct 6, 2021 at 12:10

3 Answers 3

6

My answer to Why is "mdnsd" draining my battery and how to stop it? explains the role of Android's mDNS Daemon which is an init service, started on demand. It provides Multicast DNS (host/domain name resolution) and DNS-SD (DNS-Based Service Discovery); both part of Zero Configuration Networking.

How to resolve Android's mDNS hostname to IP address?

Technically it's possible for DNS-SD to use the old unicast DNS to resolve the domain name found in the SRV record (of a broadcasted service) to an IP address. But mostly mDNS and DNS-SD are bound to each other. And this is the case with Android.

Android doesn't expose any setting or API to do only the name resolution (as mDNS Responder or Querier) and not the service discovery. Neither the name resolution mechanism uses mDNS. Android's mdnsd serves as mDNS broadcaster / responder only to DNS-SD service. You cannot use either of the both independently (as to my knowledge).

But there are a few options:

  • Run an app on your Android device with a discoverable service (even a dummy), which uses Android's official API (which in turn interacts with mdnsd underneath) to broadcast and discover services. So any other host on the local network with mDNS/DNS-SD capability can discover the service. For instance use avahi-browse command on a Linux system. Resolving the service provides the IP address of the Android device.

  • When mdnsd is running, Android device is already broadcasting itself with a hard-coded name: Android.local. If you don't want to broadcast a service, there's another option.

    ADB relies on mDNS to discover devices on local network by registering a service of type _adb._tcp (see references here, here and here). So enable "USB Debugging" in Developer Options and you'll see mdnsd running on your device.

    Or on a rooted device start mdnsd alone, without starting ADB, by executing setprop ctl.start mdnsd or simply start mdnsd.

In both above cases, when mdnsd is running, executing avahi-resolve-host-name Android.local on a Linux PC should return IP address of your Android device.

What if two Android devices have the same hostname?

In case of name conflict, as the name Android.local is hard-coded, the Android device which joins later automatically renames itself to Android-2.lcoal, Android-3.local and so on (see reference here). So you can resolve the names accordingly.

Any option to change the mDNS hostname?

Coming to the next problem, what if your OEM/ROM developer removed or modified the mdnsd service, or if you want to run with a name of your choice, not the hard-coded one?

  • Build and run your own more configurable mDNS responder like Avahi on Android device.

  • Even simpler, patch and rebuild Android's mdnsd to receive a commandline argument for name, or read from a config file if exists.

    No, it doesn't need a rooted device. The patched mdnsd binary should run fine from adb shell, or even on a terminal emulator app like Termux. As of this writing, there's only one syscall in mdnsd source code which the seccomp blocks for apps. But it can be removed easily. Neither the DAC or MAC block the execution.

7
  • 1
    Hehh, very funny! Android.local works. Problem is, it finds only one of my androids... somehow I need to change the name it advertises.
    – peterh
    Commented Oct 7, 2021 at 16:37
  • Thanks! Unfortunately, the source clearly shows that there is no way to change it. Next question, is there a way to replace the android mdsnd with a better configurable one.
    – peterh
    Commented Oct 7, 2021 at 16:47
  • 1
    The simplest way is to patch and rebuild Android's mdnsd to receive a commandline argument for name, or read from a config file if exists. Commented Oct 7, 2021 at 21:34
  • That requires root and rooting a phone means that I risk bricking it. Furthermore, there is really free (as in freedom) android image only for a few phones. Sad, but I still can not have a rooted phone on this reason. Furthermore, I can not root the phones what are not mine, but I can talk to their owner to install X app.
    – peterh
    Commented Oct 7, 2021 at 23:34
  • 2
2

I could not make anything work: mdnsd, avahi etc. It works between my Ubuntu PCs but for Android I could not easily make it work.

So then my solution was to just set a static IP for the device in my wifi: Wifi settings -> select the wifi -> edit -> set ip address and gateway as needed by your router (you can look at IP of existing devices to figure out the IP you need, and replace the last number with 1 to usually get your routers IP), and set the 2 DNS addresses e.g. to opendns 208.67.222.222 and 208.67.220.220

Now I just access this same IP and stop worrying about broadcasting a domain locally.

0
0

If you happen to already have an app handy to which you want to add this feature of advertising a custom device name, adding JmDNS as a dependency works quite nicely in order to configure a custom .local name:

package org.example.util;

import android.util.Log;

import androidx.annotation.Nullable;

import java.io.IOException;
import java.net.InetAddress;
import java.util.Objects;

import javax.jmdns.JmDNS;

/**
 * Advertises a custom .local hostname via mDNS
 */
public class HostnameBroadcaster {
    private static final String LOGTAG = "mDNS";

    private static JmDNS sJmDNS;
    private static String sHostname;

    private HostnameBroadcaster() {}

    /**
     * Set the hostname to be advertised via mDNS.
     * <p>
     * Note: JmDNS does some network accesses on the calling thread on startup, so don't call this
     * method on the UI thread.
     *
     * @param addr The IP address to bind to. If <code>null</code>, JmDNS's auto-binding will be
     *             used.
     * @param hostname The hostname to advertise via mDNS. If <code>null</code>, the broadcaster is
     *                 shut down.
     */
    public static void setHostname(@Nullable InetAddress addr, @Nullable String hostname) {
        if (Objects.equals(sHostname, hostname)) {
            return;
        }

        if (sJmDNS != null) {
            try {
                sJmDNS.close();
            } catch (IOException e) {
                Log.d(LOGTAG, "Error stopping mDNS hostname broadcaster", e);
            }
            sJmDNS = null;
        }

        if (hostname != null) {
            try {
                sJmDNS = JmDNS.create(addr, hostname);
            } catch (IOException e) {
                Log.d(LOGTAG, "Error starting mDNS hostname broadcaster", e);
            }
        }

        sHostname = hostname;
    }
}
1
  • 2
    Hi, we appreciate your contribution, but considering Android Enthusiasts focuses on Android end-users (who are not necessarily app developers), I'm afraid this answer cannot be applied to the majority of end-users and out-of-scope on this site. If the wording can be revised for end-users, then consider editing it. Thanks.
    – Andrew T.
    Commented Dec 8 at 14:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .