提交 deb928ad 编写于 作者: A Annie_wang

update docs

Signed-off-by: NAnnie_wang <annie.wangli@huawei.com>
上级 8542a39c
# DriverExtensionAbility
[DriverExtensionAbility](../reference/apis/js-apis-app-ability-driverExtensionAbility.md) is an ExtensionAbility of the driver type that provides driver-related extension framework. If the capabilities of a device can be expanded by inserting an external hardware module, you can install the driver of the hardware module through an application. DriverExtensionAbility can be used to develop such applications.
The [DriverExtensionAbility](../reference/apis/js-apis-app-ability-driverExtensionAbility.md) can be bound to an application through the DriverExtensionManager and process related transactions in the background based on the application request information.
Each type of ExtensionAbility has its own context. The DriverExtensionAbility provides related capabilities through the [DriverExtensionContext](../reference/apis/js-apis-inner-application-driverExtensionContext.md).
This topic describes how to use DriverExtensionAbility in the following scenarios:
- [DriverExtensionAbility](#driverextensionability)
- [How to Develop](#how-to-develop)
## How to Develop
To implement a driver, create a DriverExtensionAbility in the DevEco Studio project. The procedure is as follows:
1. In the **ets** directory of a module in the project, right-click and choose **New > Directory** to create a directory named **driverextability**.
2. In the **driverextability** directory, right-click and choose **New > TypeScript File** to create a file named **DriverExtAbility.ts**.
3. Open the **DriverExtAbility.ts** file, import the [RPC module](../reference/apis/js-apis-rpc.md), and overload the **onRemoteMessageRequest()** method to receive messages from the application and return the processing result to the application. **REQUEST_VALUE** is used to verify the service request code sent by the application.
```ts
import rpc from '@ohos.rpc';
const REQUEST_CODE = 99;
class StubTest extends rpc.RemoteObject {
constructor(des) {
super(des);
}
// Receive the message sent from the application and return the processing result to the application.
onRemoteMessageRequest(code, data, reply, option) {
if (code === REQUEST_CODE) {
// Receive the data sent from the application.
// When the application calls data.writeInt() multiple times to write data, the driver can receive the corresponding data by calling data.readInt() for multiple times.
let optFir = data.readInt();
let optSec = data.readInt();
// The driver returns the data processing result to the application.
// In the example, two pieces of data are received, and the sum of the two pieces of data is returned to the application.
reply.writeInt(optFir + optSec);
}
return true;
}
}
```
4. In the **DriverExtAbility.ts** file, import the dependency package [DriverExtensionAbility](../reference/apis/js-apis-app-ability-driverExtensionAbility.md), which provides the **onInit()**, **onRelease()**, **onConnect()**, and **onDisconnect()** lifecycle callbacks. Then, customize a class to inherit from [DriverExtensionAbility](../reference/apis/js-apis-app-ability-driverExtensionAbility.md) and override the lifecycle callbacks as required.
```ts
import DriverExtensionAbility from '@ohos.app.ability.DriverExtensionAbility';
import rpc from '@ohos.rpc';
const TAG: string = '[Example].[Entry].[DriverExtAbility]';
const REQUEST_CODE = 99;
class StubTest extends rpc.RemoteObject {
// ...
}
export default class DriverExtAbility extends DriverExtensionAbility {
onInit(want) {
console.info(TAG, `onInit, want: ${want.abilityName}`);
}
onRelease() {
console.info(TAG, `onRelease, want: ${want.abilityName}`);
}
onConnect(want) {
console.info(TAG, `onConnect, want: ${want.abilityName}`);
return new StubTest("test");
}
onDisconnect(want) {
console.info(TAG, `onDisconnect, want: ${want.abilityName}`);
}
onDump() {
console.info(TAG, `onDump, params:` + JSON.stringify(params));
return ['params'];
}
}
```
5. Register the DriverExtensionAbility in the [**module.json5** file](../quick-start/module-configuration-file.md) of the module in the project. Set **type** to **service** and **srcEntry** to the code path of the DriverExtensionAbility component.
```json
{
"module": {
// ...
"extensionAbilities": [
{
"name": "DriverExtAbility",
"icon": "$media:icon",
"description": "driver",
"type": "driver",
"exported": true,
"srcEntry": "./ets/driverextability/DriverExtAbility.ts",
"metadata": [
{
"name": "bus", // The bus is mandatory.
"value": "USB",
},
{
"name": "desc", // Description of the driver, which is mandatory.
"value": "the sample of driverExtensionAbility",
},
{
"name": "vendor", // Driver vendor name, which is mandatory.
"value": "string",
},
{
"name": "vid", // List of supported USB vendor IDs, separated by commas (,). The value cannot be empty.
"value": "string, string",
},
{
"name": "pid", // List of supported USB product IDs, separated by commas (,). The value cannot be empty.
"value": "string, string",
}
]
}
]
}
}
```
# @ohos.app.ability.DriverExtensionAbility (DriverExtensionAbility)
The **DriverExtensionAbility** module provides the ExtensionAbility related to drivers. It provides lifecycle callbacks to be invoked when a driver is created, destroyed, connected, or disconnected.
> **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.
> The APIs of this module can be used only in the stage model.
## Modules to Import
```ts
import DriverExtension from '@ohos.app.ability.DriverExtensionAbility';
```
## Required Permissions
None.
## Attributes
**System capability**: SystemCapability.Driver.ExternalDevice
**System API**: This is a system API and cannot be called by third-party applications.
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| context | [DriverExtensionContext](js-apis-inner-application-driverExtensionContext.md) | Yes| No| Context of the **DriverExtension**. This context is inherited from **ExtensionContext**.|
## DriverExtensionAbility.onInit
onInit(want: Want): void;
Called when a DriverExtensionAbility is created to initialize the service logic.
**System capability**: SystemCapability.Driver.ExternalDevice
**System API**: This is a system API and cannot be called by third-party applications.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-app-ability-want.md) | Yes| Want information related to this DriverExtensionAbility, including the ability name and bundle name.|
**Example**
```ts
class DriverExt extends DriverExtension {
onInit(want) {
console.log('onInit, want: ${want.abilityName}');
}
}
```
## DriverExtensionAbility.onRelease
onRelease(): void;
Called when this DriverExtensionAbility is destroyed to clear resources.
**System capability**: SystemCapability.Driver.ExternalDevice
**System API**: This is a system API and cannot be called by third-party applications.
**Example**
```ts
class DriverExt extends DriverExtension {
onRelease() {
console.log('onRelease');
}
}
```
## DriverExtensionAbility.onConnect
onConnect(want: Want): rpc.RemoteObject | Promise<rpc.RemoteObject>;
Called following **onCreate()** when a DriverExtensionAbility is started by calling **connectAbility()**. A **RemoteObject** object is returned for communication between the server and client.
**System capability**: SystemCapability.Driver.ExternalDevice
**System API**: This is a system API and cannot be called by third-party applications.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-app-ability-want.md)| Yes| Want information related to this DriverExtensionAbility, including the ability name and bundle name.|
**Return value**
| Type| Description|
| -------- | -------- |
| rpc.RemoteObject | A **RemoteObject** object used for communication between the server and client.|
**Example**
```ts
import rpc from '@ohos.rpc';
class StubTest extends rpc.RemoteObject{
constructor(des) {
super(des);
}
onRemoteRequest(code, data, reply, option) {
}
}
class DriverExt extends DriverExtension {
onConnect(want) {
console.log('onConnect , want: ${want.abilityName}');
return new StubTest('test');
}
}
```
If the returned **RemoteObject** object depends on an asynchronous API, you can use the asynchronous lifecycle.
```ts
import rpc from '@ohos.rpc';
class StubTest extends rpc.RemoteObject{
constructor(des) {
super(des);
}
onRemoteRequest(code, data, reply, option) {
}
}
async function getDescriptor() {
// Call the asynchronous function.
return "asyncTest"
}
class DriverExt extends DriverExtension {
async onConnect(want) {
console.log(`onConnect , want: ${want.abilityName}`);
let descriptor = await getDescriptor();
return new StubTest(descriptor);
}
}
```
## DriverExtensionAbility.onDisconnect
onDisconnect(want: Want): void | Promise\<void>;
Called when a client is disconnected from this DriverExtensionAbility.
**System capability**: SystemCapability.Driver.ExternalDevice
**System API**: This is a system API and cannot be called by third-party applications.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want |[Want](js-apis-app-ability-want.md)| Yes| Want information related to this DriverExtensionAbility, including the ability name and bundle name.|
**Example**
```ts
class DriverExt extends DriverExtension {
onDisconnect(want) {
console.log('onDisconnect, want: ${want.abilityName}');
}
}
```
After the **onDisconnect** lifecycle callback is executed, the application may exit. As a result, the asynchronous function in **onDisconnect** may fail to be executed correctly, for example, asynchronously writing data to the database. The asynchronous lifecycle can be used to ensure that the subsequent lifecycle continues after the asynchronous **onDisconnect** is complete.
```ts
class DriverExt extends DriverExtension {
async onDisconnect(want) {
console.log('onDisconnect, want: ${want.abilityName}');
// Call the asynchronous function.
}
}
```
## DriverExtensionAbility.onDump
onDump(params: Array\<string>): Array\<string>;
Dumps client information.
**System capability**: SystemCapability.Driver.ExternalDevice
**System API**: This is a system API and cannot be called by third-party applications.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| params | Array\<string> | Yes| Parameters in the form of a command.|
**Example**
```ts
class DriverExt extends DriverExtension {
onDump(params) {
console.log('dump, params: ${JSON.stringify(params)}');
return ['params'];
}
}
```
# DriverExtensionContext
The **DriverExtensionContext** module provides the context of **DriverExtensionAbility**. It inherits from **ExtensionContext**.
The **DriverExtensionContext** module provides the operations that need to be actively initiated in the **DriverExtensionAbility** implementation.
> **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.
> - The APIs of this module can be used only in the stage model.
## Modules to Import
```ts
import common from '@ohos.app.ability.common';
```
## How to Use
Before using **DriverExtensionContext**, you need to obtain it through a **DriverExtensionAbility** child class instance.
```ts
import DriverExtensionAbility from '@ohos.app.ability.DriverExtensionAbility';
let context;
class EntryAbility extends DriverExtensionAbility {
onInit() {
context = this.context; // Obtain DriverExtensionContext.
}
}
```
## DriverExtensionContext.updateDriverState
updateDriverState(): void;
Updates the driver state. This interface is reserved and does not provide specific functionality currently.
**System capability**: SystemCapability.Driver.ExternalDevice
**Example**
```ts
this.context.updateDriverState() ;
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册