harmony 鸿蒙@ohos.driver.deviceManager (Peripheral Management)
@ohos.driver.deviceManager (Peripheral Management)
The deviceManager module provides APIs for managing peripheral devices, including querying the peripheral device list and binding or unbinding a peripheral device.
NOTE
The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.
Modules to Import
import deviceManager from "@ohos.driver.deviceManager";
deviceManager.queryDevices
queryDevices(busType?: number): Array<Readonly<Device>>
Queries the list of peripheral devices. If the device has no peripheral device connected, an empty list is returned.
Required permissions: ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER
System capability: SystemCapability.Driver.ExternalDevice
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
busType | number | No | Bus type of the peripheral device. If this parameter is left blank, all types of peripheral devices are queried. |
Return value
Type | Description |
---|---|
Array<Readonly<Device>> | List of peripheral devices obtained. |
Error codes
ID | Error Message |
---|---|
401 | The parameter check failed. |
22900001 | ExternalDeviceManager service exception. |
Example
import deviceManager from "@ohos.driver.deviceManager";
try {
let devices : Array<deviceManager.Device> = deviceManager.queryDevices(deviceManager.BusType.USB);
for (let item of devices) {
let device : deviceManager.USBDevice = item as deviceManager.USBDevice;
console.info(`Device id is ${device.deviceId}`)
}
} catch (error) {
console.error(`Failed to query device. Code is ${error.code}, message is ${error.message}`);
}
deviceManager.bindDevice
bindDevice(deviceId: number, onDisconnect: AsyncCallback<number>, callback: AsyncCallback<{deviceId: number, remote: rpc.IRemoteObject}>): void;
Binds a peripheral device based on the device information returned by queryDevices(). This API uses an asynchronous callback to return the result.
You need to use deviceManager.queryDevices to obtain the peripheral device information first.
Required permissions: ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER
System capability: SystemCapability.Driver.ExternalDevice
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
deviceId | number | Yes | ID of the device to bind. It can be obtained by queryDevices(). |
onDisconnect | AsyncCallback<number> | Yes | Callback to be invoked when the bound peripheral device is disconnected. |
callback | AsyncCallback<{deviceId: number, remote: rpc.IRemoteObject}> | Yes | Callback invoked to return the communication object of the peripheral device bound. |
Error codes
ID | Error Message |
---|---|
401 | The parameter check failed. |
22900001 | ExternalDeviceManager service exception. |
Example
import deviceManager from "@ohos.driver.deviceManager";
import { BusinessError } from '@ohos.base';
try {
// For example, deviceId is 12345678. You can use queryDevices() to obtain the deviceId.
deviceManager.bindDevice(12345678, (error : BusinessError, data : number) => {
console.error(`Device is disconnected`);
}, (error : BusinessError, data: {
deviceId : number;
remote : rpc.IRemoteObject;
}) => {
if (error) {
console.error(`bindDevice async fail. Code is ${error.code}, message is ${error.message}`);
return;
}
console.info(`bindDevice success`);
});
} catch (error) {
console.error(`bindDevice fail. Code is ${error.code}, message is ${error.message}`);
}
deviceManager.bindDevice
bindDevice(deviceId: number, onDisconnect: AsyncCallback<number>): Promise<{deviceId: number, remote: rpc.IRemoteObject}>;
Binds a peripheral device based on the device information returned by queryDevices(). This API uses a promise to return the result.
You need to use deviceManager.queryDevices to obtain the peripheral device information first.
Required permissions: ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER
System capability: SystemCapability.Driver.ExternalDevice
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
deviceId | number | Yes | ID of the device to bind. It can be obtained by queryDevices(). |
onDisconnect | AsyncCallback<number> | Yes | Callback to be invoked when the bound peripheral device is disconnected. |
Return value
Type | Description |
---|---|
Promise<{deviceId: number, remote: rpc.IRemoteObject}> | Promise used to return the device ID and IRemoteObject object. |
Error codes
ID | Error Message |
---|---|
401 | The parameter check failed. |
22900001 | ExternalDeviceManager service exception. |
Example
import deviceManager from "@ohos.driver.deviceManager";
import { BusinessError } from '@ohos.base';
try {
// For example, deviceId is 12345678. You can use queryDevices() to obtain the deviceId.
deviceManager.bindDevice(12345678, (error : BusinessError, data : number) => {
console.error(`Device is disconnected`);
}).then(data => {
console.info(`bindDevice success, Device_Id is ${data.deviceId}.
remote is ${data.remote != null ? data.remote.getDescriptor() : "null"}`);
}, (error: BusinessError) => {
console.error(`bindDevice async fail. Code is ${error.code}, message is ${error.message}`);
});
} catch (error) {
console.error(`bindDevice fail. Code is ${error.code}, message is ${error.message}`);
}
deviceManager.unbindDevice
unbindDevice(deviceId: number, callback: AsyncCallback<number>): void
Unbinds a peripheral device. This API uses an asynchronous callback to return the result.
Required permissions: ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER
System capability: SystemCapability.Driver.ExternalDevice
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
deviceId | number | Yes | ID of the device to unbind. It can be obtained by queryDevices(). |
callback | AsyncCallback<number> | Yes | Callback invoked to return the result. |
Error codes
ID | Error Message |
---|---|
401 | The parameter check failed. |
22900001 | ExternalDeviceManager service exception. |
Example
import deviceManager from "@ohos.driver.deviceManager";
try {
// For example, deviceId is 12345678. You can use queryDevices() to obtain the deviceId.
deviceManager.unbindDevice(12345678, (error : BusinessError, data : number) => {
if (error) {
console.error(`unbindDevice async fail. Code is ${error.code}, message is ${error.message}`);
return;
}
console.info(`unbindDevice success`);
});
} catch (error) {
console.error(`unbindDevice fail. Code is ${error.code}, message is ${error.message}`);
}
deviceManager.unbindDevice
unbindDevice(deviceId: number): Promise<number>
Unbinds a peripheral device. This API uses a promise to return the result.
Required permissions: ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER
System capability: SystemCapability.Driver.ExternalDevice
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
deviceId | number | Yes | ID of the device to unbind. It can be obtained by queryDevices(). |
Error codes
ID | Error Message |
---|---|
401 | The parameter check failed. |
22900001 | ExternalDeviceManager service exception. |
Return value
Type | Description |
---|---|
Promise<number> | Promise used to return the device ID. |
Example
import deviceManager from "@ohos.driver.deviceManager";
import { BusinessError } from '@ohos.base';
try {
// For example, deviceId is 12345678. You can use queryDevices() to obtain the deviceId.
deviceManager.unbindDevice(12345678).then((data : number) => {
console.info(`unbindDevice success, Device_Id is ${data}.`);
}, (error : BusinessError) => {
console.error(`unbindDevice async fail. Code is ${error.code}, message is ${error.message}`);
});
} catch (error) {
console.error(`unbindDevice fail. Code is ${error.code}, message is ${error.message}`);
}
Device
Represents the peripheral device information.
System capability: SystemCapability.Driver.ExternalDevice
Name | Type | Mandatory | Description |
---|---|---|---|
busType | BusType | Yes | Bus type. |
deviceId | number | Yes | ID of the peripheral device. |
description | string | Yes | Description of the peripheral device. |
USBDevice
Represents the USB device information.
System capability: SystemCapability.Driver.ExternalDevice
Name | Type | Mandatory | Description |
---|---|---|---|
vendorId | number | Yes | Vendor ID of the USB device. |
productId | number | Yes | Product ID of the USB device. |
BusType
Enumerates the device bus types.
System capability: SystemCapability.Driver.ExternalDevice
Name | Value | Description |
---|---|---|
USB | 1 | USB bus. |
你可能感兴趣的鸿蒙文章
harmony 鸿蒙System Common Events (To Be Deprecated Soon)
harmony 鸿蒙System Common Events
harmony 鸿蒙API Reference Document Description
harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)
harmony 鸿蒙BundleStatusCallback
harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)
harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)
harmony 鸿蒙@ohos.bundle (Bundle)
harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦