• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.ability.DriverExtensionAbility (DriverExtensionAbility)
2
3The **DriverExtensionAbility** module provides the ExtensionAbility related to drivers. It provides lifecycle callbacks to be invoked when a driver is created, destroyed, connected, or disconnected.
4
5> **NOTE**
6>
7> 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.
8> The APIs of this module can be used only in the stage model.
9
10## Modules to Import
11
12```ts
13import DriverExtension from '@ohos.app.ability.DriverExtensionAbility';
14```
15
16## Required Permissions
17
18None.
19
20## Attributes
21
22**System capability**: SystemCapability.Driver.ExternalDevice
23
24
25| Name| Type| Readable| Writable| Description|
26| -------- | -------- | -------- | -------- | -------- |
27| context | [DriverExtensionContext](js-apis-inner-application-driverExtensionContext.md)  | Yes| No| Context of the **DriverExtension**. This context is inherited from **ExtensionContext**.|
28
29
30## DriverExtensionAbility.onInit
31
32onInit(want: Want): void;
33
34Called when a DriverExtensionAbility is created to initialize the service logic.
35
36**System capability**: SystemCapability.Driver.ExternalDevice
37
38**Parameters**
39
40| Name| Type| Mandatory| Description|
41| -------- | -------- | -------- | -------- |
42| want |  [Want](js-apis-app-ability-want.md) | Yes| Want information related to this DriverExtensionAbility, including the ability name and bundle name.|
43
44**Example**
45
46  ```ts
47  class DriverExt extends DriverExtension {
48    onInit(want : Want) {
49      console.log('onInit, want: ${want.abilityName}');
50    }
51  }
52  ```
53
54
55## DriverExtensionAbility.onRelease
56
57onRelease(): void;
58
59Called when this DriverExtensionAbility is destroyed to clear resources.
60
61**System capability**: SystemCapability.Driver.ExternalDevice
62
63**Example**
64
65  ```ts
66  class DriverExt extends DriverExtension {
67    onRelease() {
68      console.log('onRelease');
69    }
70  }
71  ```
72
73
74## DriverExtensionAbility.onConnect
75
76onConnect(want: Want): rpc.RemoteObject | Promise<rpc.RemoteObject>;
77
78Called following **onCreate()** when a DriverExtensionAbility is started by calling **connectAbility()**. A **RemoteObject** object is returned for communication between the server and client.
79
80**System capability**: SystemCapability.Driver.ExternalDevice
81
82**Parameters**
83
84| Name| Type| Mandatory| Description|
85| -------- | -------- | -------- | -------- |
86| want |  [Want](js-apis-app-ability-want.md)| Yes| Want information related to this DriverExtensionAbility, including the ability name and bundle name.|
87
88**Return value**
89
90| Type| Description|
91| -------- | -------- |
92| rpc.RemoteObject | A **RemoteObject** object used for communication between the server and client.|
93
94**Example**
95
96  ```ts
97  import rpc from '@ohos.rpc';
98  class StubTest extends rpc.RemoteObject{
99      constructor(des : string) {
100          super(des);
101      }
102      onRemoteMessageRequest(code : number, data : MessageSequence, reply : MessageSequence, option : MessageOption) {
103      }
104  }
105  class DriverExt extends DriverExtension {
106    onConnect(want : Want) {
107      console.log('onConnect , want: ${want.abilityName}');
108      return new StubTest('test');
109    }
110  }
111  ```
112
113If the returned **RemoteObject** object depends on an asynchronous API, you can use the asynchronous lifecycle.
114
115  ```ts
116import rpc from '@ohos.rpc';
117class StubTest extends rpc.RemoteObject{
118    constructor(des : string) {
119        super(des);
120    }
121    onRemoteMessageRequest(code : number, data : MessageSequence, reply : MessageSequence, option : MessageOption) {
122    }
123}
124async function getDescriptor() {
125    // Call the asynchronous function.
126    return "asyncTest"
127}
128class DriverExt extends DriverExtension {
129  async onConnect(want : Want) {
130    console.log(`onConnect , want: ${want.abilityName}`);
131    let descriptor = await getDescriptor();
132    return new StubTest(descriptor);
133  }
134}
135  ```
136
137## DriverExtensionAbility.onDisconnect
138
139onDisconnect(want: Want): void | Promise\<void>;
140
141Called when a client is disconnected from this DriverExtensionAbility.
142
143**System capability**: SystemCapability.Driver.ExternalDevice
144
145**Parameters**
146
147| Name| Type| Mandatory| Description|
148| -------- | -------- | -------- | -------- |
149| want |[Want](js-apis-app-ability-want.md)| Yes| Want information related to this DriverExtensionAbility, including the ability name and bundle name.|
150
151**Example**
152
153  ```ts
154  class DriverExt extends DriverExtension {
155    onDisconnect(want : Want) {
156      console.log('onDisconnect, want: ${want.abilityName}');
157    }
158  }
159  ```
160
161After 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.
162
163  ```ts
164class DriverExt extends DriverExtension {
165  async onDisconnect(want : Want) {
166    console.log('onDisconnect, want: ${want.abilityName}');
167    // Call the asynchronous function.
168  }
169}
170  ```
171
172
173## DriverExtensionAbility.onDump
174
175onDump(params: Array\<string>): Array\<string>;
176
177Dumps client information.
178
179**System capability**: SystemCapability.Driver.ExternalDevice
180
181**Parameters**
182
183| Name| Type| Mandatory| Description|
184| -------- | -------- | -------- | -------- |
185| params | Array\<string> | Yes| Parameters in the form of a command.|
186
187**Example**
188
189  ```ts
190  class DriverExt extends DriverExtension {
191      onDump(params : Array<string>) {
192          console.log('dump, params: ${JSON.stringify(params)}');
193          return ['params'];
194      }
195  }
196  ```
197