How to make Android device visible to Bonjour / Zeroconf with .local name?


Question

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?


Answer

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.




Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes