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