Using MDNS for LAN Access

Overview

Multicast DNS (MDNS) provides functions such as adding, removing, discovering, and resolving local services on a LAN.

  • Local service: a service provider on a LAN, for example, a printer or scanner.

Typical MDNS management scenarios include:

  • Managing local services on a LAN, such as adding, removing, and resolving local services.
  • Discovering local services and listening to the status changes of local services of the specified type through the DiscoveryService object.

NOTE To maximize the application running efficiency, most API calls are called asynchronously in callback or promise mode. The following code examples use the promise mode. For details about the APIs, see MDNS Management.

The following describes the development procedure specific to each application scenario.

NOTE

In the sample code provided in this topic, this.context is used to obtain the UIAbilityContext, where this indicates a UIAbility instance inherited from UIAbility. To use UIAbilityContext APIs on pages, see Obtaining the Context of UIAbility.

Managing Local Services

  1. Connect the device to the Wi-Fi network.

  2. Import the mdns, BusinessError, and common namespaces from @kit.NetworkKit.

    // Import the mdns namespace from @kit.NetworkKit.
    import { mdns } from '@kit.NetworkKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    import { common } from '@kit.AbilityKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    
  3. Call addLocalService to add a local service.

    // Create a LocalService object.
    private localServiceInfo: mdns.LocalServiceInfo = {
      serviceType: '_print._tcp',
      serviceName: 'servicename',
      port: 5555,
      host: {
        address: '127.0.0.1'
      },
      serviceAttribute: [{ key: '111', value: [1] }]
    };
    // ...
      let context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext;
      // Call addLocalService to add a local service.
      mdns.addLocalService(context, this.localServiceInfo).then((data) => {
        // ...
        hilog.info(0x0000, 'testTag', `Local Service Added: ${JSON.stringify(data)}`);
      })
      // ...
    
  4. (Optional) Call resolveLocalService to resolve the local service for the IP address of the local network.

    // (Optional) Call resolveLocalService to resolve the local service.
    mdns.resolveLocalService(context, this.localServiceInfo).then((data: mdns.LocalServiceInfo) => {
      // ...
      hilog.info(0x0000, 'testTag', `Resolved Local Service: ${JSON.stringify(data)}`);
    })
    
  5. Call removeLocalService to remove the local service.

    // Call removeLocalService to remove the local service.
    mdns.removeLocalService(context, this.localServiceInfo).then((data: mdns.LocalServiceInfo) => {
      // ...
      hilog.info(0x0000, 'testTag', `Local Service Removed: ${JSON.stringify(data)}`);
    })
    

Discovering Local Services

  1. Connect the device to the Wi-Fi network.

  2. Import the mdns namespace from @kit.NetworkKit.

    // Import the mdns namespace from @kit.NetworkKit.
    import { mdns } from '@kit.NetworkKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    import { common } from '@kit.AbilityKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    
  3. Create a DiscoveryService object, which is used to discover MDNS services of the specified type.

    let context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext;
        
    // ...
    // Create a DiscoveryService object, which is used to discover mDNS services of the specified type.
    let serviceType = '_print._tcp';
    let discoveryService = mdns.createDiscoveryService(context, serviceType);
    
  4. Subscribe to MDNS service discovery status changes.

    // Subscribe to mDNS service discovery status changes.
    discoveryService.on('discoveryStart', (data: mdns.DiscoveryEventInfo) => {
      hilog.info(0x0000, 'testTag', JSON.stringify(data));
    });
    discoveryService.on('discoveryStop', (data: mdns.DiscoveryEventInfo) => {
      hilog.info(0x0000, 'testTag', JSON.stringify(data));
    });
    discoveryService.on('serviceFound', (data: mdns.LocalServiceInfo) => {
      hilog.info(0x0000, 'testTag', JSON.stringify(data));
      // ...
    });
    discoveryService.on('serviceLost', (data: mdns.LocalServiceInfo) => {
      hilog.info(0x0000, 'testTag', JSON.stringify(data));
      // ...
    });
    
  5. Enable discovery of MDNS services on the LAN.

    // Enable discovery of mDNS services on the LAN.
    discoveryService.startSearchingMDNS();
    
  6. Stop searching for MDNS services on the LAN.

    // Stop searching for mDNS services on the LAN.
    discoveryService.stopSearchingMDNS();
    
  7. Unsubscribe from MDNS service discovery status changes.

    // Unsubscribe from mDNS service discovery status changes.
    discoveryService.off('discoveryStart', (data: mdns.DiscoveryEventInfo) => {
      hilog.info(0x0000, 'testTag', JSON.stringify(data));
    });
    discoveryService.off('discoveryStop', (data: mdns.DiscoveryEventInfo) => {
      hilog.info(0x0000, 'testTag', JSON.stringify(data));
    });
    discoveryService.off('serviceFound', (data: mdns.LocalServiceInfo) => {
      hilog.info(0x0000, 'testTag', JSON.stringify(data));
      // ...
    });
    discoveryService.off('serviceLost', (data: mdns.LocalServiceInfo) => {
      hilog.info(0x0000, 'testTag', JSON.stringify(data));
      // ...
    });
    

Samples

The following sample is provided to help you better understand MDNS management: