# @ohos.distributedHardware.deviceManager (设备管理)
本模块能力已更新至新模块。建议使用新模块的接口进行开发,参见[@ohos.distributedDeviceManager](js-apis-distributedDeviceManager.md)。
本模块提供分布式设备管理能力。
系统应用可调用接口实现如下功能:
- 注册和解除注册设备上下线变化监听
- 发现周边不可信设备
- 认证和取消认证设备
- 查询可信设备列表
- 查询本地设备信息,包括设备名称,设备类型和设备标识
- 发布设备发现
> **说明:**
>
> - 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
> - 本模块接口为系统接口,三方应用不支持调用。
## 导入模块
```ts
import deviceManager from '@ohos.distributedHardware.deviceManager';
```
## deviceManager.createDeviceManager
createDeviceManager(bundleName: string, callback: AsyncCallback<DeviceManager>): void
创建一个设备管理器实例。
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ---------- | ---------------------------------------------------- | ---- | ----------------------------------------------------------- |
| bundleName | string | 是 | 指示应用程序的Bundle名称。 |
| callback | AsyncCallback<[DeviceManager](#devicemanager)> | 是 | DeviceManager实例创建时调用的回调,返回设备管理器对象实例。 |
**示例:**
```ts
import deviceManager from '@ohos.distributedHardware.deviceManager';
import { BusinessError } from '@ohos.base'
let dmInstance: deviceManager.Devicemanager | null = null;
try {
deviceManager.createDeviceManager("ohos.samples.jshelloworld", (err: BusinessError, data: deviceManager.Devicemanager) => {
if (err) {
console.error("createDeviceManager errCode:" + err.code + ",errMessage:" + err.message);
return;
}
console.info("createDeviceManager success");
dmInstance = data;
});
} catch(err) {
let e: BusinessError = err as BusinessError;
console.error("createDeviceManager errCode:" + e.code + ",errMessage:" + e.message);
}
```
## DeviceInfo
设备信息。
**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager
| 名称 | 类型 | 必填 | 说明 |
| ---------------------- | ------------------------- | ---- | -------- |
| deviceId | string | 是 | 设备的唯一标识。 |
| deviceName | string | 是 | 设备名称。 |
| deviceType | [DeviceType](#devicetype) | 是 | 设备类型。 |
| networkId8+ | string | 是 | 设备网络标识。 |
| range9+ | number | 是 | 发现设备的距离。 |
| authForm10+ | [AuthForm](#authform) | 是 | 设备认证类型 |
## DeviceType
表示设备类型的枚举类。
**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager
| 名称 | 值 | 说明 |
| ------------ | ---- | ---- |
| SPEAKER | 0x0A | 智能音箱 |
| PHONE | 0x0E | 手机 |
| TABLET | 0x11 | 平板 |
| WEARABLE | 0x6D | 智能穿戴 |
| TV | 0x9C | 智慧屏 |
| CAR | 0x83 | 车 |
| UNKNOWN_TYPE | 0 | 未知设备 |
## AuthForm10+
表示设备认证类型的枚举类。
**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager
| 名称 | 值 | 说明 |
| ------------------- | ---- | --------------- |
| INVALID_TYPE | -1 | 设备没有认证 |
| PEER_TO_PEER | 0 | 无账号设备点对点认证 |
| IDENTICAL_ACCOUNT | 1 | 设备同账号认证 |
| ACROSS_ACCOUNT | 2 | 设备跨账号认证 |
## DeviceStateChangeAction
表示设备状态变化的枚举。
**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager
| 名称 | 值 | 说明 |
| ------- | ---- | --------------- |
| ONLINE | 0 | 设备物理上线状态。 |
| READY | 1 | 设备可用状态,表示设备间信息已在分布式数据中同步完成, 可以运行分布式业务。 |
| OFFLINE | 2 | 设备物理下线状态。 |
| CHANGE | 3 | 设备信息更改。 |
## SubscribeInfo
发现信息。
**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager
| 名称 | 类型 | 必填 | 说明 |
| ------------- | --------------------------------- | ---- | ----------------- |
| subscribeId | number | 是 | 发现标识,用于标识不同的发现周期。 |
| mode | [DiscoverMode ](#discovermode) | 是 | 发现模式。 |
| medium | [ExchangeMedium](#exchangemedium) | 是 | 发现类型。 |
| freq | [ExchangeFreq](#exchangefreq) | 是 | 发现频率。 |
| isSameAccount | boolean | 否 | 是否同帐号。 |
| isWakeRemote | boolean | 否 | 是否唤醒设备。 |
| capability | [SubscribeCap](#subscribecap) | 是 | 发现能力。 |
## DiscoverMode
表示发现模式的枚举。
**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager
| 名称 | 值 | 说明 |
| --------------------- | ---- | ----- |
| DISCOVER_MODE_PASSIVE | 0x55 | 被动模式。 |
| DISCOVER_MODE_ACTIVE | 0xAA | 主动模式。 |
## ExchangeMedium
表示发现类型的枚举。
**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager
| 名称 | 值 | 说明 |
| ---- | ---- | --------- |
| AUTO | 0 | 自动发现类型。 |
| BLE | 1 | 蓝牙发现类型。 |
| COAP | 2 | WiFi发现类型。 |
| USB | 3 | USB发现类型。 |
## ExchangeFreq
表示发现频率的枚举。
**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager
| 名称 | 值 | 说明 |
| ---------- | ---- | ----- |
| LOW | 0 | 低频率。 |
| MID | 1 | 中频率。 |
| HIGH | 2 | 高频率。 |
| SUPER_HIGH | 3 | 超高频率。 |
## SubscribeCap
表示发现能力的枚举。
**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager
| 名称 | 值 | 说明 |
| ------------------------- | ---- | -------------- |
| SUBSCRIBE_CAPABILITY_DDMP | 0 | DDMP能力,后续会被废弃。 |
| SUBSCRIBE_CAPABILITY_OSD | 1 | OSD能力。 |
## AuthParam
认证参数。
**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager
| 名称 | 类型 | 必填 | 说明 |
| --------- | -------------------- | ---- | ---------- |
| authType | number | 是 | 认证类型。 |
| extraInfo | {[key:string] : any} | 否 | 认证参数可扩展字段。可选,默认为undefined。 |
## AuthInfo
认证信息。
**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager
| 名称 | 类型 | 必填 | 说明 |
| --------- | -------------------- | ---- | ---------- |
| authType | number | 是 | 认证类型。 |
| token | number | 是 | 认证Token。 |
| extraInfo | {[key:string] : any} | 否 | 认证信息可扩展字段。可选,默认为undefined。 |
## PublishInfo9+
发布设备参数
**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager
| 名称 | 类型 | 必填 | 说明 |
| ------------- | --------------------------------- | ---- | ----------------- |
| publishId | number | 是 | 发布设备标识,用于标识不同的发布周期。 |
| mode | [DiscoverMode ](#discovermode) | 是 | 发现模式。 |
| freq | [ExchangeFreq](#exchangefreq) | 是 | 发现频率。 |
| ranging | boolean | 是 | 发布的设备是否支持测距能力。 |
## DeviceManager
设备管理实例,用于获取可信设备和本地设备的相关信息。在调用DeviceManager的方法前,需要先通过createDeviceManager构建一个DeviceManager实例dmInstance。
### release
release(): void
设备管理实例不再使用后,通过该方法释放DeviceManager实例。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**错误码:**
以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
| 错误码ID | 错误信息 |
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function. |
**示例:**
```ts
import { BusinessError } from '@ohos.base'
try {
dmInstance.release();
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("release errCode:" + e.code + ",errMessage:" + e.message);
}
```
### getTrustedDeviceListSync
getTrustedDeviceListSync(): Array<DeviceInfo>
同步获取所有可信设备列表。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**返回值:**
| 名称 | 说明 |
| -------------------------------------- | --------- |
| Array<[DeviceInfo](#deviceinfo)> | 返回可信设备列表。 |
**错误码:**
以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
| 错误码ID | 错误信息 |
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function. |
**示例:**
```ts
import deviceManager from '@ohos.distributedHardware.deviceManager';
import { BusinessError } from '@ohos.base'
try {
let deviceInfoList: Array = dmInstance.getTrustedDeviceListSync();
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("getTrustedDeviceListSync errCode:" + e.code + ",errMessage:" + e.message);
}
```
### getTrustedDeviceListSync10+
getTrustedDeviceListSync(isRefresh: boolean): Array<DeviceInfo>
打开软总线系统端的心跳模式,让周围处于下线状态的可信设备快速上线,同时刷新已上线的可信设备列表。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------------- | --------------------------------- | ---- | ---------------------------------- |
| isRefresh | boolean | 是 | 是否打开心跳模式,刷新可信列表。 |
**返回值:**
| 名称 | 说明 |
| -------------------------------------- | ---------------- |
| Array<[DeviceInfo](#deviceinfo)> | 返回可信设备列表。 |
**错误码:**
以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
| 错误码ID | 错误信息 |
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function. |
**示例:**
```ts
import deviceManager from '@ohos.distributedHardware.deviceManager';
import { BusinessError } from '@ohos.base'
try {
let deviceInfoList: Array = dmInstance.getTrustedDeviceListSync(true);
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("getTrustedDeviceListSync errCode:" + e.code + ",errMessage:" + e.message);
}
```
### getTrustedDeviceList8+
getTrustedDeviceList(callback:AsyncCallback<Array<DeviceInfo>>): void
获取所有可信设备列表。使用callback异步回调。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | --------------------- |
| callback | AsyncCallback<Array<[DeviceInfo](#deviceinfo)>> | 是 | 获取所有可信设备列表的回调,返回设备信息。 |
**示例:**
```ts
import deviceManager from '@ohos.distributedHardware.deviceManager';
import { BusinessError } from '@ohos.base'
try {
dmInstance.getTrustedDeviceList((err: BusinessError, data: deviceManager.DeviceInfo) => {
if (err) {
console.error("getTrustedDeviceList errCode:" + err.code + ",errMessage:" + err.message);
return;
}
console.log('get trusted device info: ' + JSON.stringify(data));
});
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("getTrustedDeviceList errCode:" + e.code + ",errMessage:" + e.message);
}
```
### getTrustedDeviceList8+
getTrustedDeviceList(): Promise<Array<DeviceInfo>>
获取所有可信设备列表。使用Promise异步回调。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**返回值:**
| 类型 | 说明 |
| ---------------------------------------- | --------------------- |
| Promise<Array<[DeviceInfo](#deviceinfo)>> | Promise实例,用于获取异步返回结果。 |
**示例:**
```ts
import deviceManager from '@ohos.distributedHardware.deviceManager';
import { BusinessError } from '@ohos.base'
dmInstance.getTrustedDeviceList().then((data: Array) => {
console.log('get trusted device info: ' + JSON.stringify(data));
}).catch((err: BusinessError) => {
console.error("getTrustedDeviceList errCode:" + err.code + ",errMessage:" + err.message);
});
```
### getLocalDeviceInfoSync8+
getLocalDeviceInfoSync(): [DeviceInfo](#deviceinfo)
同步获取本地设备信息。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**返回值:**
| 名称 | 说明 |
| ------------------------- | ---------------- |
| [DeviceInfo](#deviceinfo) | 返回本地设备列表。 |
**错误码:**
以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
| 错误码ID | 错误信息 |
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function. |
**示例:**
```ts
import deviceManager from '@ohos.distributedHardware.deviceManager';
import { BusinessError } from '@ohos.base'
try {
let deviceInfo: deviceManager.DeviceInfo = dmInstance.getLocalDeviceInfoSync();
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("getLocalDeviceInfoSync errCode:" + e.code + ",errMessage:" + e.message);
}
```
### getLocalDeviceInfo8+
getLocalDeviceInfo(callback:AsyncCallback<DeviceInfo>): void
获取本地设备信息。使用callback异步回调。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | --------- |
| callback | AsyncCallback<[DeviceInfo](#deviceinfo)> | 是 | 获取本地设备信息。 |
**示例:**
```ts
import deviceManager from '@ohos.distributedHardware.deviceManager';
import { BusinessError } from '@ohos.base'
try {
dmInstance.getLocalDeviceInfo((err: BusinessError, data: deviceManager.DeviceInfo) => {
if (err) {
console.error("getLocalDeviceInfo errCode:" + err.code + ",errMessage:" + err.message);
return;
}
console.log('get local device info: ' + JSON.stringify(data));
});
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("getLocalDeviceInfo errCode:" + e.code + ",errMessage:" + e.message);
}
```
### getLocalDeviceInfo8+
getLocalDeviceInfo(): Promise<DeviceInfo>
获取本地设备信息。使用Promise异步回调。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**返回值:**
| 类型 | 说明 |
| ---------------------------------------- | --------------------- |
| Promise<[DeviceInfo](#deviceinfo)> | Promise实例,用于获取异步返回结果。 |
**示例:**
```ts
import deviceManager from '@ohos.distributedHardware.deviceManager';
import { BusinessError } from '@ohos.base'
dmInstance.getLocalDeviceInfo().then((data: deviceManager.DeviceInfo) => {
console.log('get local device info: ' + JSON.stringify(data));
}).catch((err: BusinessError) => {
console.error("getLocalDeviceInfo errCode:" + err.code + ",errMessage:" + err.message);
});
```
### getDeviceInfo10+
getDeviceInfo(networkId: string, callback:AsyncCallback<DeviceInfo>): void
通过指定设备的网络标识获取该设备的信息。使用callback异步回调。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | --------- |
| networkId| string | 是 | 设备的网络标识。 |
| callback | AsyncCallback<[DeviceInfo](#deviceinfo)> | 是 | 获取指定设备信息。 |
**示例:**
```ts
import deviceManager from '@ohos.distributedHardware.deviceManager';
import { BusinessError } from '@ohos.base'
try {
// 设备网络标识,可以从可信设备列表中获取
let networkId = "xxxxxxx"
dmInstance.getDeviceInfo(networkId, (err: BusinessError, data: deviceManager.DeviceInfo) => {
if (err) {
console.error("getDeviceInfo errCode:" + err.code + ",errMessage:" + err.message);
return;
}
console.log('get device info: ' + JSON.stringify(data));
});
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("getDeviceInfo errCode:" + e.code + ",errMessage:" + e.message);
}
```
### getDeviceInfo10+
getDeviceInfo(networkId: string): Promise<DeviceInfo>
通过指定设备的网络标识获取该设备的信息。使用Promise异步回调。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | --------- |
| networkId| string | 是 | 设备的网络标识。 |
**返回值:**
| 类型 | 说明 |
| ---------------------------------------- | --------------------- |
| Promise<[DeviceInfo](#deviceinfo)> | Promise实例,用于获取异步返回结果。 |
**示例:**
```ts
import deviceManager from '@ohos.distributedHardware.deviceManager';
import { BusinessError } from '@ohos.base'
// 设备网络标识,可以从可信设备列表中获取
let networkId = "xxxxxxx"
dmInstance.getDeviceInfo(networkId).then((data: deviceManager.DeviceInfo) => {
console.log('get device info: ' + JSON.stringify(data));
}).catch((err: BusinessError) => {
console.error("getDeviceInfo errCode:" + err.code + ",errMessage:" + err.message);
});
```
### startDeviceDiscovery8+
startDeviceDiscovery(subscribeInfo: SubscribeInfo): void
发现周边设备。发现状态持续两分钟,超过两分钟,会停止发现,最大发现数量99个。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------------- | ------------------------------- | ---- | ----- |
| subscribeInfo | [SubscribeInfo](#subscribeinfo) | 是 | 发现信息。|
**错误码:**
以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
| 错误码ID | 错误信息 |
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function. |
| 11600104 | Discovery invalid. |
**示例:**
```ts
import { BusinessError } from '@ohos.base'
interface SubscribeInfo {
subscribeId: number,
mode: number, // 主动模式
medium: number, // 自动发现类型,同时支持多种发现类型
freq: number, // 高频率
isSameAccount: boolean,
isWakeRemote: boolean,
capability: number
};
// 生成发现标识,随机数确保每次调用发现接口的标识不一致
let subscribeId = Math.floor(Math.random() * 10000 + 1000);
let subscribeInfo: SubscribeInfo = {
subscribeId: subscribeId,
mode: 0xAA, // 主动模式
medium: 0, // 自动发现类型,同时支持多种发现类型
freq: 2, // 高频率
isSameAccount: false,
isWakeRemote: false,
capability: 1
};
try {
dmInstance.startDeviceDiscovery(subscribeInfo); // 当有设备发现时,通过deviceFound回调通知给应用程序
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("startDeviceDiscovery errCode:" + e.code + ",errMessage:" + e.message);
}
```
### startDeviceDiscovery9+
startDeviceDiscovery(subscribeInfo: SubscribeInfo, filterOptions?: string): void
发现周边设备。发现状态持续两分钟,超过两分钟,会停止发现,最大发现数量99个。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------------- | ------------------------------- | ---- | ----- |
| subscribeInfo | [SubscribeInfo](#subscribeinfo) | 是 | 发现信息。 |
| filterOptions | string | 否 | 发现设备过滤信息。可选,默认为undefined,发现未上线设备。|
**错误码:**
以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
| 错误码ID | 错误信息 |
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function. |
| 11600104 | Discovery invalid. |
**示例:**
```ts
import { BusinessError } from '@ohos.base'
interface Filters {
type: string,
value: number
}
interface FilterOptions {
filter_op: string, // 可选, 默认"OR"
filters: Filters[]
}
interface SubscribeInfo {
subscribeId: number,
mode: number, // 主动模式
medium: number, // 自动发现类型,同时支持多种发现类型
freq: number, // 高频率
isSameAccount: boolean,
isWakeRemote: boolean,
capability: number
}
// 生成发现标识,随机数确保每次调用发现接口的标识不一致
let subscribeId = Math.floor(Math.random() * 10000 + 1000);
let subscribeInfo: SubscribeInfo = {
subscribeId: subscribeId,
mode: 0xAA, // 主动模式
medium: 0, // 自动发现类型,同时支持多种发现类型
freq: 2, // 高频率
isSameAccount: false,
isWakeRemote: false,
capability: 1
};
let filters: Filters[] = [
{
type: "range",
value: 50 // 需过滤发现设备的距离,单位(cm)
}
]
let filterOptions: FilterOptions = {
filter_op: "OR", // 可选, 默认"OR"
filters: filters
};
try {
dmInstance.startDeviceDiscovery(subscribeInfo, JSON.stringify(filterOptions)); // 当有设备发现时,通过deviceFound回调通知给应用程序
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("startDeviceDiscovery errCode:" + e.code + ",errMessage:" + e.message);
}
```
### stopDeviceDiscovery
stopDeviceDiscovery(subscribeId: number): void
停止发现周边设备。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----------- | ------ | ---- | ----- |
| subscribeId | number | 是 | 发现标识。 |
**错误码:**
以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
| 错误码ID | 错误信息 |
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function. |
**示例:**
```ts
import { BusinessError } from '@ohos.base'
try {
// stopDeviceDiscovery和startDeviceDiscovery需配对使用,入参需要和startDeviceDiscovery接口传入的subscribeId值相等
let subscribeId = 12345;
dmInstance.stopDeviceDiscovery(subscribeId);
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("stopDeviceDiscovery errCode:" + e.code + ",errMessage:" + e.message);
}
```
### publishDeviceDiscovery9+
publishDeviceDiscovery(publishInfo: PublishInfo): void
发布设备发现。发布状态持续两分钟,超过两分钟会停止发布。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------------- | ------------------------------- | ---- | ----- |
| publishInfo | [PublishInfo](#publishinfo) | 是 | 发布设备发现信息。 |
**错误码:**
以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
| 错误码ID | 错误信息 |
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function. |
| 11600105 | Publish invalid. |
**示例:**
```ts
import { BusinessError } from '@ohos.base'
interface PublishInfo {
publishId: number,
mode: number, // 主动模式
freq: number, // 高频率
ranging: boolean // 支持发现时测距
};
// 生成发布标识,随机数确保每次调用发布接口的标识不一致
let publishId = Math.floor(Math.random() * 10000 + 1000);
let publishInfo: PublishInfo = {
publishId: publishId,
mode: 0xAA, // 主动模式
freq: 2, // 高频率
ranging: true // 支持发现时测距
};
try {
dmInstance.publishDeviceDiscovery(publishInfo); // 当有发布结果时,通过回调通知给应用程序
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("publishDeviceDiscovery errCode:" + e.code + ",errMessage:" + e.message);
}
```
### unPublishDeviceDiscovery9+
unPublishDeviceDiscovery(publishId: number): void
停止发布设备发现。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----------- | -------- | ---- | ----- |
| publishId | number | 是 | 发布标识。 |
**错误码:**
以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
| 错误码ID | 错误信息 |
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function. |
**示例:**
```ts
import { BusinessError } from '@ohos.base'
try {
// unPublishDeviceDiscovery和publishDeviceDiscovery配对使用,入参需要和publishDeviceDiscovery接口传入的publishId值相等
let publishId = 12345;
dmInstance.unPublishDeviceDiscovery(publishId);
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("unPublishDeviceDiscovery errCode:" + e.code + ",errMessage:" + e.message);
}
```
### authenticateDevice
authenticateDevice(deviceInfo: DeviceInfo, authParam: AuthParam, callback: AsyncCallback<{deviceId: string, pinToken ?: number}>): void
认证设备。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ---------- | ---------------------------------------- | ---- | ------- |
| deviceInfo | [DeviceInfo](#deviceinfo) | 是 | 设备信息。 |
| authParam | [AuthParam](#authparam) | 是 | 认证参数。 |
| callback | AsyncCallback<{deviceId: string, pinToken ?: number}> | 是 | 认证结果回调。 |
**示例:**
```ts
import { BusinessError } from '@ohos.base'
class Data {
deviceId: string = ""
pinToken?: number = 0
}
interface DeviceInfo {
deviceId: string,
deviceName: string
deviceType: number,
networkId: string,
range: number
};
interface ExtraInfo {
targetPkgName: string,
appName: string,
appDescription: string,
business: string
}
interface AuthParam {
authType: number,// 认证类型: 1 - 无帐号PIN码认证
extraInfo: ExtraInfo
}
// 认证的设备信息,可以从发现的结果中获取
let deviceInfo: DeviceInfo = {
deviceId: "XXXXXXXX",
deviceName: "",
deviceType: 0x0E,
networkId: "xxxxxxx",
range: 0
};
let extraInfo: ExtraInfo = {
targetPkgName: 'ohos.samples.xxx',
appName: 'xxx',
appDescription: 'xxx',
business: '0'
}
let authParam: AuthParam = {
authType: 1,// 认证类型: 1 - 无帐号PIN码认证
extraInfo: extraInfo
}
try {
dmInstance.authenticateDevice(deviceInfo, authParam, (err: BusinessError, data: Data) => {
if (err) {
console.error("authenticateDevice errCode:" + err.code + ",errMessage:" + err.message);
return;
}
console.info("authenticateDevice result:" + JSON.stringify(data));
let token = data.pinToken;
});
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("authenticateDevice errCode:" + e.code + ",errMessage:" + e.message);
}
```
### unAuthenticateDevice8+
unAuthenticateDevice(deviceInfo: DeviceInfo): void
解除认证设备。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ---------- | ------------------------- | ---- | ----- |
| deviceInfo | [DeviceInfo](#deviceinfo) | 是 | 设备信息。 |
**错误码:**
以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
| 错误码ID | 错误信息 |
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function. |
**示例:**
```ts
import { BusinessError } from '@ohos.base'
interface DeviceInfo {
deviceId: string,
deviceName: string,
deviceType: number,
networkId: string,
range: number
}
try {
let deviceInfo: DeviceInfo = {
deviceId: "XXXXXXXX",
deviceName: "",
deviceType: 0x0E,
networkId: "xxxxxxx",
range: 0
};
dmInstance.unAuthenticateDevice(deviceInfo);
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("unAuthenticateDevice errCode:" + e.code + ",errMessage:" + e.message);
}
```
### verifyAuthInfo
verifyAuthInfo(authInfo: AuthInfo, callback: AsyncCallback<{deviceId: string, level: number}>): void
验证认证信息。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | ------- |
| authInfo | [AuthInfo](#authinfo) | 是 | 认证信息。 |
| callback | AsyncCallback<{deviceId: string, level: number}> | 是 | 验证结果回调。 |
**示例:**
```ts
import { BusinessError } from '@ohos.base'
interface ExtraInfo {
authType: number,
token: number
}
interface AuthInfo {
authType: number,
token: number,
extraInfo: ExtraInfo
}
class Data {
deviceId: string = ""
level: number = 0
}
let extraInfo: ExtraInfo = {
authType: 0,
token: 0
}
let authInfo: AuthInfo = {
authType: 1,
token: 123456,
extraInfo: extraInfo
}
try {
dmInstance.verifyAuthInfo(authInfo, (err: BusinessError, data: Data) => {
if (err) {
console.error("verifyAuthInfo errCode:" + err.code + ",errMessage:" + err.message);
return;
}
console.info("verifyAuthInfo result:" + JSON.stringify(data));
});
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("verifyAuthInfo errCode:" + e.code + ",errMessage:" + e.message);
}
```
### setUserOperation9+
setUserOperation(operateAction: number, params: string): void;
设置用户ui操作行为。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------------- | --------------- | ---- | ------------------- |
| operateAction | number | 是 | 用户操作动作。 |
| params | string | 是 | 表示用户的输入参数。 |
**示例:**
```ts
import { BusinessError } from '@ohos.base'
try {
/*
operateAction = 0 - 允许授权
operateAction = 1 - 取消授权
operateAction = 2 - 授权框用户操作超时
operateAction = 3 - 取消pin码框展示
operateAction = 4 - 取消pin码输入框展示
operateAction = 5 - pin码输入框确定操作
*/
let operation = 0;
dmInstance.setUserOperation(operation, "extra")
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("setUserOperation errCode:" + e.code + ",errMessage:" + e.message);
}
```
### requestCredentialRegisterInfo10+
requestCredentialRegisterInfo(requestInfo: string, callback: AsyncCallback<{registerInfo: string}>): void;
获取凭据的注册信息。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------------- | --------------- | ---- | ------------------- |
| requestInfo | string | 是 | 请求凭据信息。 |
| callback | AsyncCallback<{registerInfo: string}> | 是 | 凭据的注册信息回调。 |
**示例:**
```ts
import { BusinessError } from '@ohos.base'
interface CredentialInfo {
version: string,
userId: string
}
class Data {
registerInfo: string = ""
}
let credentialInfo: CredentialInfo = {
version: "1.2.3",
userId: "123"
}
try {
dmInstance.requestCredentialRegisterInfo(credentialInfo, (data:Data) => {
if (data) {
console.info("requestCredentialRegisterInfo result:" + JSON.stringify(data));
} else {
console.info("requestCredentialRegisterInfo result: data is null");
}
});
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("requestCredentialRegisterInfo err:" + e.code + "," + e.message);
}
```
### importCredential10+
importCredential(credentialInfo: string, callback: AsyncCallback<{resultInfo: string}>): void;
导入凭据信息。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------------- | --------------- | ---- | ------------------- |
| credentialInfo| string | 是 | 导入凭据信息。 |
| callback | AsyncCallback<{resultInfo: string}> | 是 | 导入凭据结果回调。 |
**示例:**
```ts
import { BusinessError } from '@ohos.base'
class Data {
resultInfo: string = ""
}
interface CredentialData {
credentialType: number,
credentialId: string,
serverPk: string,
pkInfoSignature : string,
pkInfo: string,
authCode: string,
peerDeviceId: string
}
interface CredentialInfo {
processType: number,
authType: number,
userId: string,
deviceId: string,
version: string,
devicePk : string,
credentialData : CredentialData
}
let credentialData: CredentialData = {
credentialType: 2,
credentialId: "102",
serverPk: "3059301306072A8648CE3D020106082A8648CE3D03",
pkInfoSignature : "30440220490BCB4F822004C9A76AB8D97F80041FC0E",
pkInfo: "",
authCode: "",
peerDeviceId: ""
}
let credentialInfo: CredentialInfo = {
processType: 1,
authType: 1,
userId: "123",
deviceId: "aaa",
version: "1.2.3",
devicePk : "0000",
credentialData : credentialData
}
try {
dmInstance.importCredential(credentialInfo, (data: Data) => {
if (data) {
console.info("importCredential result:" + JSON.stringify(data));
} else {
console.info("importCredential result: data is null");
}
});
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("importCredential err:" + e.code + "," + e.message);
}
```
### deleteCredential10+
deleteCredential(queryInfo: string, callback: AsyncCallback<{resultInfo: string}>): void;
删除凭据信息。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------------- | --------------- | ---- | ------------------- |
| queryInfo | string | 是 | 删除凭据信息。 |
| callback | AsyncCallback<{resultInfo: string}> | 是 | 删除凭据结果回调。 |
**示例:**
```ts
import { BusinessError } from '@ohos.base'
class Data {
resultInfo: string = ""
}
interface QueryInfo {
processType: number,
authType: number,
userId: string
}
let queryInfo: QueryInfo = {
processType: 1,
authType: 1,
userId: "123"
}
try {
dmInstance.deleteCredential(queryInfo, (data: Data) => {
if (data) {
console.info("deleteCredential result:" + JSON.stringify(data));
} else {
console.info("deleteCredential result: data is null");
}
});
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("deleteCredential err:" + e.code + "," + e.message);
}
```
### on('uiStateChange')9+
on(type: 'uiStateChange', callback: Callback<{ param: string}>): void;
ui状态变更回调。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------ | ---- | ------------------------------ |
| type | string | 是 | 注册的设备管理器 ui 状态回调,以便在状态改变时通知应用。 |
| callback | Callback<{ param: string}> | 是 | 指示要注册的设备管理器 ui 状态回调,返回ui状态。 |
**示例:**
```ts
import { BusinessError } from '@ohos.base'
class Data {
param: string = ""
}
interface TmpStr {
verifyFailed: boolean
}
try {
dmInstance.on('uiStateChange', (data: Data) => {
console.log("uiStateChange executed, dialog closed" + JSON.stringify(data))
let tmpStr: TmpStr = JSON.parse(data.param)
let isShow = tmpStr.verifyFailed
console.log("uiStateChange executed, dialog closed" + isShow)
});
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("uiStateChange errCode:" + e.code + ",errMessage:" + e.message);
}
```
### off('uiStateChange')9+
off(type: 'uiStateChange', callback?: Callback<{ param: string}>): void;
取消ui状态变更回调。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------- | ---- | ------------------------------ |
| type | string | 是 | 取消注册的设备管理器 ui 状态回调。 |
| callback | Callback<{ param: string}> | 否 | 指示要取消注册的设备管理器 ui 状态,返回UI状态。 |
**示例:**
```ts
import { BusinessError } from '@ohos.base'
try {
dmInstance.off('uiStateChange');
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("uiStateChange errCode:" + e.code + ",errMessage:" + e.message);
}
```
### on('deviceStateChange')
on(type: 'deviceStateChange', callback: Callback<{ action: DeviceStateChangeAction, device: DeviceInfo }>): void
注册设备状态回调。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | ------------------------------ |
| type | string | 是 | 注册设备状态回调,固定为deviceStateChange。 |
| callback | Callback<{ action: [DeviceStateChangeAction](#devicestatechangeaction), device: [DeviceInfo](#deviceinfo) }> | 是 | 指示要注册的设备状态回调,返回设备状态和设备信息。 |
**示例:**
```ts
import deviceManager from '@ohos.distributedHardware.deviceManager';
import { BusinessError } from '@ohos.base'
class Data {
action: deviceManager.DeviceStateChangeAction = 0
device: deviceManager.DeviceInfo = {
deviceId: "",
deviceName: "",
deviceType: 0,
networkId: "",
range: 0,
authForm:0,
}
}
try {
dmInstance.on('deviceStateChange', (data: Data) => {
console.info("deviceStateChange on:" + JSON.stringify(data));
});
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("deviceStateChange errCode:" + e.code + ",errMessage:" + e.message);
}
```
### off('deviceStateChange')
off(type: 'deviceStateChange', callback?: Callback<{ action: DeviceStateChangeAction, device: DeviceInfo }>): void
取消注册设备状态回调。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | --------------------------- |
| type | string | 是 | 根据应用程序的包名取消注册设备状态回调。 |
| callback | Callback<{ action: [DeviceStateChangeAction](#devicestatechangeaction), device: [DeviceInfo](#deviceinfo) }> | 否 | 指示要取消注册的设备状态回调,返回设备状态和设备信息。 |
**示例:**
```ts
import deviceManager from '@ohos.distributedHardware.deviceManager';
import { BusinessError } from '@ohos.base'
class Data {
action: deviceManager.DeviceStateChangeAction = 0
device: deviceManager.DeviceInfo = {
deviceId: "",
deviceName: "",
deviceType: 0,
networkId: "",
range: 0,
authForm:0,
}
}
try {
dmInstance.off('deviceStateChange', (data: Data) => {
console.info('deviceStateChange' + JSON.stringify(data));
});
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("deviceStateChange errCode:" + e.code + ",errMessage:" + e.message);
}
```
### on('deviceFound')
on(type: 'deviceFound', callback: Callback<{ subscribeId: number, device: DeviceInfo }>): void
注册发现设备回调监听。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | -------------------------- |
| type | string | 是 | 注册设备发现回调,以便在发现周边设备时通知应用程序。 |
| callback | Callback<{ subscribeId: number, device: [DeviceInfo](#deviceinfo) }> | 是 | 注册设备发现的回调方法。 |
**示例:**
```ts
import deviceManager from '@ohos.distributedHardware.deviceManager';
import { BusinessError } from '@ohos.base'
class Data {
subscribeId: number = 0
device: deviceManager.DeviceInfo = {}
}
try {
dmInstance.on('deviceFound', (data: Data) => {
console.info("deviceFound:" + JSON.stringify(data));
});
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("deviceFound errCode:" + e.code + ",errMessage:" + e.message);
}
```
### off('deviceFound')
off(type: 'deviceFound', callback?: Callback<{ subscribeId: number, device: DeviceInfo }>): void
取消注册设备发现回调。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | --------------------------- |
| type | string | 是 | 取消注册设备发现回调。 |
| callback | Callback<{ subscribeId: number, device: [DeviceInfo](#deviceinfo) }> | 否 | 指示要取消注册的设备发现回调,返回设备状态和设备信息。 |
**示例:**
```ts
import deviceManager from '@ohos.distributedHardware.deviceManager';
import { BusinessError } from '@ohos.base'
class Data {
subscribeId: number = 0
device: deviceManager.DeviceInfo = {
deviceId: "",
deviceName: "",
deviceType: 0,
networkId: "",
range: 0,
authForm:0,
}
}
try {
dmInstance.off('deviceFound', (data: Data) => {
console.info('deviceFound' + JSON.stringify(data));
});
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("deviceFound errCode:" + e.code + ",errMessage:" + e.message);
}
```
### on('discoverFail')
on(type: 'discoverFail', callback: Callback<{ subscribeId: number, reason: number }>): void
注册设备发现失败回调监听。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | ------------------------------ |
| type | string | 是 | 注册设备发现失败回调,以便在发现周边设备失败时通知应用程序。 |
| callback | Callback<{ subscribeId: number, reason: number }> | 是 | 注册设备发现失败的回调方法。 |
**示例:**
```ts
import { BusinessError } from '@ohos.base'
class Data {
subscribeId: number = 0
reason: number = 0
}
try {
dmInstance.on('discoverFail', (data: Data) => {
console.info("discoverFail on:" + JSON.stringify(data));
});
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("discoverFail errCode:" + e.code + ",errMessage:" + e.message);
}
```
### off('discoverFail')
off(type: 'discoverFail', callback?: Callback<{ subscribeId: number, reason: number }>): void
取消注册设备发现失败回调。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | ----------------- |
| type | string | 是 | 取消注册设备发现失败回调。 |
| callback | Callback<{ subscribeId: number, reason: number }> | 否 | 指示要取消注册的设备发现失败回调。 |
**示例:**
```ts
import { BusinessError } from '@ohos.base'
class Data {
subscribeId: number = 0
reason: number = 0
}
try {
dmInstance.off('discoverFail', (data: Data) => {
console.info('discoverFail' + JSON.stringify(data));
});
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("discoverFail errCode:" + e.code + ",errMessage:" + e.message);
}
```
### on('publishSuccess')9+
on(type: 'publishSuccess', callback: Callback<{ publishId: number }>): void
注册发布设备发现回调监听。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | -------------------------- |
| type | string | 是 | 注册发布设备成功回调,以便将发布成功时通知应用程序。 |
| callback | Callback<{ publishId: number }> | 是 | 注册设备发布成功的回调方法。 |
**示例:**
```ts
import { BusinessError } from '@ohos.base'
class Data {
publishId: number = 0
}
try {
dmInstance.on('publishSuccess', (data: Data) => {
console.info("publishSuccess:" + JSON.stringify(data));
});
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("publishSuccess errCode:" + e.code + ",errMessage:" + e.message);
}
```
### off('publishSuccess')9+
off(type: 'publishSuccess', callback?: Callback<{ publishId: number }>): void
取消注册设备发布成功回调。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | --------------------------- |
| type | string | 是 | 取消注册设备发布成功回调。 |
| callback | Callback<{ publishId: number }> | 否 | 指示要取消注册的设备发布成功回调。 |
**示例:**
```ts
import { BusinessError } from '@ohos.base'
class Data {
publishId: number = 0
}
try {
dmInstance.off('publishSuccess', (data: Data) => {
console.info('publishSuccess' + JSON.stringify(data));
});
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("publishSuccess errCode:" + e.code + ",errMessage:" + e.message);
}
```
### on('publishFail')9+
on(type: 'publishFail', callback: Callback<{ publishId: number, reason: number }>): void
注册设备发布失败回调监听。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------------------------------------- | ---- | ------------------------------ |
| type | string | 是 | 注册设备发布失败回调,以便在发布设备失败时通知应用程序。 |
| callback | Callback<{ publishId: number, reason: number }> | 是 | 注册设备发布失败的回调方法。 |
**示例:**
```ts
import { BusinessError } from '@ohos.base'
class Data {
publishId: number = 0
reason: number = 0
}
try {
dmInstance.on('publishFail', (data: Data) => {
console.info("publishFail on:" + JSON.stringify(data));
});
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("publishFail errCode:" + e.code + ",errMessage:" + e.message);
}
```
### off('publishFail')9+
off(type: 'publishFail', callback?: Callback<{ publishId: number, reason: number }>): void
取消注册设备发布失败回调。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------------------------------------- | ---- | ----------------- |
| type | string | 是 | 取消注册设备发布失败回调。 |
| callback | Callback<{ publishId: number, reason: number }> | 否 | 指示要取消注册设备发布失败回调。 |
**示例:**
```ts
import { BusinessError } from '@ohos.base'
class Data {
publishId: number = 0
reason: number = 0
}
try {
dmInstance.off('publishFail', (data: Data) => {
console.info('publishFail' + JSON.stringify(data));
});
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("publishFail errCode:" + e.code + ",errMessage:" + e.message);
}
```
### on('serviceDie')
on(type: 'serviceDie', callback: () => void): void
注册设备管理服务死亡监听。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | ---------------------------------------- |
| type | string | 是 | 注册serviceDie回调,以便在devicemanager服务异常终止时通知应用程序。 |
| callback | () => void | 是 | 注册serviceDie的回调方法。 |
**示例:**
```ts
import { BusinessError } from '@ohos.base'
try {
dmInstance.on("serviceDie", () => {
console.info("serviceDie on");
});
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("serviceDie errCode:" + e.code + ",errMessage:" + e.message);
}
```
### off('serviceDie')
off(type: 'serviceDie', callback?: () => void): void
取消注册设备管理服务死亡监听。
**需要权限**:ohos.permission.ACCESS_SERVICE_DM
**系统能力**:SystemCapability.DistributedHardware.DeviceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | ---------------------------------------- |
| type | string | 是 | 取消注册serviceDie回调,以便在devicemanager服务异常终止时通知应用程序。 |
| callback | () => void | 否 | 取消注册serviceDie的回调方法。 |
**示例:**
```ts
import { BusinessError } from '@ohos.base'
try {
dmInstance.off("serviceDie", () => {
console.info("serviceDie off");
});
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error("serviceDie errCode:" + e.code + ",errMessage:" + e.message);
}
```