Multicast DNS (MDNS) provides functions such as adding, removing, discovering, and resolving local services on a LAN.
Typical MDNS management scenarios include:
Managing local services on a LAN, such as adding, removing, and resolving local services.
Discover local services and listen to the status changes of local services of the specified type through the DiscoveryService object.
To maximize the app running efficiency, most API calls are asynchronous. For asynchronous APIs, both callback and promise modes are provided. The following examples use the promise mode. For more modes, see @ohos.net.mdns (MDNS Management).
The following describes the development procedure specific to each application scenario.
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.
Connect the device to the Wi-Fi network.
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'; 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)}`);
})
// ... Call resolveLocalService to resolve the IP address of the local network (optional; use it as needed).
// (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)}`);
}) Call removeLocalService to remove a 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)}`);
}) Connect the device to the Wi-Fi network.
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'; Create a DiscoveryService object to discover MDNS services of the specified service 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); 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));
// ...
}); Enable discovery of MDNS services on the LAN.
// Enable discovery of mDNS services on the LAN. discoveryService.startSearchingMDNS();
Stop searching for MDNS services on the LAN.
// Stop searching for mDNS services on the LAN. discoveryService.stopSearchingMDNS();
Unsubscribe from MDNS service discovery status changes.
// Unsubscribe from mDNS service discovery status changes. If no callback is passed, unsubscribe all callbacks for the event.
discoveryService.off('discoveryStart');
discoveryService.off('discoveryStop');
discoveryService.off('serviceFound');
discoveryService.off('serviceLost');