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