title: How does HomeKit find nearby devices?
body: <p>First of all I am sorry if this is not the most appropriate forum to post this question. I looked at AskDifferent which seems not technical enough for this and I looked at Super User which seems too technical for it. Hence why I am asking it here as it is also related to programming.</p>
  
  <h2>Description</h2>
  
  <p>I am using the Arduino IDE to set up an HAP (HomeKit Accessory Protocol) server on an ESP8266 (-01). I have successfully set up an mDNS server on it for discovery using the <a href="https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266mDNS" rel="nofollow noreferrer">ESP8266mDNS</a> library. However, I am facing one slight problem with this:</p>
  
  <p>After the mDNS server has been started and an _hap._tcp service been added, the device initially does not show up in the Home app. It is only after I "probe" the _hap._tcp service using <code>$ dns-sd -B _hap._tcp</code> from a Terminal that the ESP8266 shows up in the Home app.</p>
  
  <p>It seems that this "probing" does something on the network which is not initially done by the mDNS server but is required by HomeKit to be discoverable.</p>
  
  <p>I have tested this with other services such as "_http._tcp" and using an App on my iPhone called Radar to search for Bonjour services. It finds any other service but not hap (until I "probe" it).</p>
  
  <p>Using WireShark shows me that the Home app does not send any mDNS requests until an (already discovered) device is clicked on. This makes me think that the initial discovery (to show devices to click on) is not done by mDNS but by some other method. Perhaps some kind of registry on the network which is then filled in after I probe using dns-sd.</p>
  
  <p>Using the Radar app I can see mDNS packets on the network for any service that I select there except for hap. This is strange as if the iPhone is blocking the mDNS requests for the hap service.</p>
  
  <p>I have spent 3 days now trying to get this to work and I really don't know where to go from here. I would like to know what I am missing to make the ESP8266 instantly discoverable by the Home app without having to use dsn-sd to "activate" this. If you want to reproduce the issue I have put a simplified version of my code which still has the same problem below. You can upload this to an ESP8266 using the Arduino IDE after installing the appropriate boards.</p>
  
  <p>After installation, try to see if the device shows up in HomeKit. If it does not, use a Mac on the same network and enter <code>$ dns-sd -B _hap._tcp</code> into a Terminal. While this is running go back to the Home app and you should be able to see the device there.</p>
  
  <h2>Code</h2>
  
  <pre><code>#include &lt;ESP8266WiFi.h&gt;
  #include &lt;WiFiClient.h&gt;
  #include &lt;ESP8266WebServer.h&gt;
  #include &lt;ESP8266mDNS.h&gt;
  
  
  const char* ssid = "WiFi"; //Change to your SSID
  const char* password = "xxxxx"; //Change to your password
  
  ESP8266WebServer server(80);
  
  void handleNotFound(){
    server.send(404, "text/plain", "Not Found!");
  }
  
  void setup() {
    Serial.begin(115200);
    WiFi.mode(WIFI_STA);
    WiFi.hostname("ESP8266");
    WiFi.begin(ssid, password);
  
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
  
    if (MDNS.begin("ESP8266")) {
      Serial.println("MDNS Started.");
    }
    server.onNotFound(handleNotFound);
  
    server.begin();
  
    MDNS.addService("hap", "tcp", 80);
    MDNS.addServiceTxt("hap", "tcp", "md", "ESP8266");
    MDNS.addServiceTxt("hap", "tcp", "pv", "1.0");
    MDNS.addServiceTxt("hap", "tcp", "id", "A0:20:A6:15:46:44");
    MDNS.addServiceTxt("hap", "tcp", "c#", "3");
    MDNS.addServiceTxt("hap", "tcp", "s#", "1");
    MDNS.addServiceTxt("hap", "tcp", "ff", "0");
    MDNS.addServiceTxt("hap", "tcp", "ci", "5");
    MDNS.addServiceTxt("hap", "tcp", "sf", "1");
  }
  
  void loop() {
    server.handleClient();
  }
  </code></pre>