# Using Motion and Orientation Sensors ## Overview The motion and orientation sensors, such as an accelerometer and a gyroscope, can monitor the motion status and orientation change (such as rotation and tilt) of the device. Through the W3C standards-compliant API, the **Web** component can access the sensor data to implement richer user interaction features. For example, in a web application, you can use an accelerometer to identify the motion mode and instruct the user to exercise, and use a gyroscope to capture the tilt and rotation of the device in the hand of the player, implementing game experience without button control. To access the motion and orientation sensors, invoke the following W3C standards-compliant APIs in JavaScript: | API | Name | Description | | ------------------------- | ------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------- | | Accelerometer | Accelerometer | Obtains acceleration data in the X, Y, and Z axes. | | Gyroscope | Gyroscope | Obtains angular velocity data in the X, Y, and Z axes. | | AbsoluteOrientationSensor | Absolute orientation | Obtains the quaternion including the X, Y, Z, and W components that indicates the absolute orientation of the device. | | RelativeOrientationSensor | Relative orientation | Obtains the quaternion including the X, Y, Z, and W components that indicates the relative orientation of the device. | | DeviceMotionEvent | Device motion event| Listens for the device motion events to obtain the acceleration data of the device in the X, Y, and Z axes, acceleration data that contains gravity in the X, Y, and Z axes, and rotation rate data of the device in the alpha, beta, and gamma axes.| | DeviceOrientationEvent | Device orientation event| Listens for the device orientation events to obtain the angles of the device around the X, Y, and Z axes. | ## Required Permission To use the preceding APIs, you need to declare the corresponding sensor permissions in the **module.json5** file. For details, see [Declaring Permissions](../security/AccessToken/declare-permissions.md). ``` "requestPermissions":[ { "name": "ohos.permission.ACCELEROMETER" // Accelerometer permission }, { "name": "ohos.permission.GYROSCOPE" // Gyroscope permission } ] ``` When the **Web** component is connected to a motion or orientation sensor, configure [onPermissionRequest](../reference/apis-arkweb/ts-basic-components-web.md#onpermissionrequest9) to receive permission request notifications. ## How to Develop 1. In the application code, configure [onPermissionRequest](../reference/apis-arkweb/ts-basic-components-web.md#onpermissionrequest9) for the **Web** component and use the [getAccessibleResource](../reference/apis-arkweb/ts-basic-components-web.md#getaccessibleresource9) API of [PermissionRequest](../reference/apis-arkweb/ts-basic-components-web.md#permissionrequest9) to obtain the resource type of the request permission. When the resource type is **TYPE_SENSOR**, the sensor is authorized. ```ts import { webview } from '@kit.ArkWeb'; import { abilityAccessCtrl, PermissionRequestResult } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; @Entry @Component struct WebComponent { controller: webview.WebviewController = new webview.WebviewController() aboutToAppear() { // Enable web frontend page debugging. webview.WebviewController.setWebDebuggingAccess(true); // Create an **AtManager** instance, which is used for application access control. let atManager = abilityAccessCtrl.createAtManager(); try { atManager.requestPermissionsFromUser(getContext(this), ['ohos.permission.ACCELEROMETER', 'ohos.permission.GYROSCOPE'] , (err: BusinessError, data: PermissionRequestResult) => { console.info('data:' + JSON.stringify(data)); console.info('data permissions:' + data.permissions); console.info('data authResults:' + data.authResults); }) } catch (error) { console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); } } build() { Column() { Web({ src: $rawfile('index.html'), controller: this.controller }) .onPermissionRequest((event) => { if (event) { AlertDialog.show({ title: 'title', message: 'text', primaryButton: { value: 'deny', action: () => { event.request.deny(); } }, secondaryButton: { value: 'onConfirm', action: () => { event.request.grant(event.request.getAccessibleResource()); } }, autoCancel: false, cancel: () => { event.request.deny(); } }) } }) } } } ``` 2. In the frontend page code, use JavaScript to call the W3C standards-compliant API related to the sensor. ```html Motion and direction sensor

Motion and orientation:

```