1# DriverExtensionAbility 2 3[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. 4 5 6The [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. 7Each type of ExtensionAbility has its own context. The DriverExtensionAbility provides related capabilities through the [DriverExtensionContext](../reference/apis/js-apis-inner-application-driverExtensionContext.md). 8 9This topic describes how to use DriverExtensionAbility in the following scenarios: 10 11- [DriverExtensionAbility](#driverextensionability) 12 - [How to Develop](#how-to-develop) 13 14## How to Develop 15 16To implement a driver, create a DriverExtensionAbility in the DevEco Studio project. The procedure is as follows: 17 181. In the **ets** directory of a module in the project, right-click and choose **New > Directory** to create a directory named **driverextability**. 19 202. In the **driverextability** directory, right-click and choose **New > TypeScript File** to create a file named **DriverExtAbility.ts**. 21 223. 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. 23 24 ```ts 25 import rpc from '@ohos.rpc'; 26 27 const REQUEST_CODE = 99; 28 29 class StubTest extends rpc.RemoteObject { 30 constructor(des: string) { 31 super(des); 32 } 33 34 // Receive the message sent from the application and return the processing result to the application. 35 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, 36 option: rpc.MessageOption) { 37 if (code === REQUEST_CODE) { 38 // Receive the data sent from the application. 39 // 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. 40 let optFir: number = data.readInt(); 41 let optSec: number = data.readInt(); 42 // The driver returns the data processing result to the application. 43 // In the example, two pieces of data are received, and the sum of the two pieces of data is returned to the application. 44 reply.writeInt(optFir + optSec); 45 } 46 return true; 47 } 48 } 49 ``` 50 51 524. 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. 53 54 ```ts 55 import DriverExtensionAbility from '@ohos.app.ability.DriverExtensionAbility'; 56 import Want from '@ohos.app.ability.Want'; 57 import rpc from '@ohos.rpc'; 58 59 const TAG: string = '[Example].[Entry].[DriverExtAbility]'; 60 const REQUEST_CODE = 99; 61 62 class StubTest extends rpc.RemoteObject { 63 // ... 64 } 65 66 export default class DriverExtAbility extends DriverExtensionAbility { 67 onInit(want: Want) { 68 console.info(TAG, `onInit, want: ${want.abilityName}`); 69 } 70 71 onRelease() { 72 console.info(TAG, `onRelease`); 73 } 74 75 onConnect(want: Want) { 76 console.info(TAG, `onConnect, want: ${want.abilityName}`); 77 return new StubTest("test"); 78 } 79 80 onDisconnect(want: Want) { 81 console.info(TAG, `onDisconnect, want: ${want.abilityName}`); 82 } 83 84 onDump(params: Array<string>) { 85 console.info(TAG, `onDump, params:` + JSON.stringify(params)); 86 return ['params']; 87 } 88 } 89 ``` 90 915. 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. 92 93 ```json 94 { 95 "module": { 96 // ... 97 "extensionAbilities": [ 98 { 99 "name": "DriverExtAbility", 100 "icon": "$media:icon", 101 "description": "driver", 102 "type": "driver", 103 "exported": true, 104 "srcEntry": "./ets/driverextability/DriverExtAbility.ts", 105 "metadata": [ 106 { 107 "name": "bus", // The bus is mandatory. 108 "value": "USB", 109 }, 110 { 111 "name": "desc", // Description of the driver, which is mandatory. 112 "value": "the sample of driverExtensionAbility", 113 }, 114 { 115 "name": "vendor", // Driver vendor name, which is mandatory. 116 "value": "string", 117 }, 118 { 119 "name": "vid", // List of supported USB vendor IDs, separated by commas (,). The value cannot be empty. 120 "value": "string, string", 121 }, 122 { 123 "name": "pid", // List of supported USB product IDs, separated by commas (,). The value cannot be empty. 124 "value": "string, string", 125 } 126 ] 127 } 128 ] 129 } 130 } 131 ``` 132