1e41f4b71Sopenharmony_ci# Using Motion and Direction Sensors 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **Web** component can be connected to motion and orientation sensors through standard W3C APIs. When using the acceleration, gyroscope, and device motion event APIs, you need to declare the corresponding sensor permissions in the configuration file. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci- Add the acceleration permission in the **module.json5** file before using the acceleration and device motion event APIs. 6e41f4b71Sopenharmony_ci ``` 7e41f4b71Sopenharmony_ci "requestPermissions":[ 8e41f4b71Sopenharmony_ci { 9e41f4b71Sopenharmony_ci "name" : "ohos.permission.ACCELEROMETER" 10e41f4b71Sopenharmony_ci } 11e41f4b71Sopenharmony_ci ] 12e41f4b71Sopenharmony_ci ``` 13e41f4b71Sopenharmony_ci- Add the gyroscope permission to the **module.json5** file before using the gyroscope APIs. 14e41f4b71Sopenharmony_ci ``` 15e41f4b71Sopenharmony_ci "requestPermissions":[ 16e41f4b71Sopenharmony_ci { 17e41f4b71Sopenharmony_ci "name" : "ohos.permission.GYROSCOPE" 18e41f4b71Sopenharmony_ci } 19e41f4b71Sopenharmony_ci ] 20e41f4b71Sopenharmony_ci ``` 21e41f4b71Sopenharmony_ciFor details about how to add sensor permissions, see [Declaring Permissions in the Configuration File](../security/AccessToken/declare-permissions.md). 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ciWhen connecting the **Web** component to the motion and orientation sensors, configure **onPermissionRequest()** to receive permission request notifications. 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ciYou can obtain the motion and orientation sensors by calling the following standard W3C APIs in JavaScript. 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci| API | Name | Supported| Description | 28e41f4b71Sopenharmony_ci| ------------------------- | ------------ | -------- | ------------------ | 29e41f4b71Sopenharmony_ci| Accelerometer | Acceleration | Yes | - | 30e41f4b71Sopenharmony_ci| Gyroscope | Gyroscope | Yes | - | 31e41f4b71Sopenharmony_ci| AbsoluteOrientationSensor | Absolute orientation | Yes | - | 32e41f4b71Sopenharmony_ci| RelativeOrientationSensor | Relative orientation | No | Does not support by sensor data.| 33e41f4b71Sopenharmony_ci| DeviceMotionEvent | Device motion event| Yes | - | 34e41f4b71Sopenharmony_ci| DeviceOrientationEvent | Device orientation event| Yes | Obtains the absolute orientation.| 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci**RelativeOrientationSensor()** is not configured since it is not supported. Therefore, this API cannot be used to listen for the relative orientation data of the device. 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ciClick buttons on the **index.html** page to listen for corresponding data. The example code is as follows: 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci- Code on the application side: 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci ```ts 43e41f4b71Sopenharmony_ci import { webview } from '@kit.ArkWeb'; 44e41f4b71Sopenharmony_ci import { abilityAccessCtrl, PermissionRequestResult } from '@kit.AbilityKit'; 45e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci @Entry 48e41f4b71Sopenharmony_ci @Component 49e41f4b71Sopenharmony_ci struct WebComponent { 50e41f4b71Sopenharmony_ci controller: webview.WebviewController = new webview.WebviewController() 51e41f4b71Sopenharmony_ci aboutToAppear() { 52e41f4b71Sopenharmony_ci // Enable web frontend page debugging. 53e41f4b71Sopenharmony_ci webview.WebviewController.setWebDebuggingAccess(true); 54e41f4b71Sopenharmony_ci // Create an **AtManager** instance, which is used for application access control. 55e41f4b71Sopenharmony_ci let atManager = abilityAccessCtrl.createAtManager(); 56e41f4b71Sopenharmony_ci try { 57e41f4b71Sopenharmony_ci atManager.requestPermissionsFromUser(getContext(this), ['ohos.permission.ACCELEROMETER', 'ohos.permission.GYROSCOPE'] 58e41f4b71Sopenharmony_ci , (err: BusinessError, data: PermissionRequestResult) => { 59e41f4b71Sopenharmony_ci console.info('data:' + JSON.stringify(data)); 60e41f4b71Sopenharmony_ci console.info('data permissions:' + data.permissions); 61e41f4b71Sopenharmony_ci console.info('data authResults:' + data.authResults); 62e41f4b71Sopenharmony_ci }) 63e41f4b71Sopenharmony_ci } catch (error) { 64e41f4b71Sopenharmony_ci console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 65e41f4b71Sopenharmony_ci } 66e41f4b71Sopenharmony_ci } 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci build() { 69e41f4b71Sopenharmony_ci Column() { 70e41f4b71Sopenharmony_ci Web({ src: $rawfile('index.html'), controller: this.controller }) 71e41f4b71Sopenharmony_ci .onPermissionRequest((event) => { 72e41f4b71Sopenharmony_ci if (event) { 73e41f4b71Sopenharmony_ci AlertDialog.show({ 74e41f4b71Sopenharmony_ci title: 'title', 75e41f4b71Sopenharmony_ci message: 'text', 76e41f4b71Sopenharmony_ci primaryButton: { 77e41f4b71Sopenharmony_ci value: 'deny', 78e41f4b71Sopenharmony_ci action: () => { 79e41f4b71Sopenharmony_ci event.request.deny(); 80e41f4b71Sopenharmony_ci } 81e41f4b71Sopenharmony_ci }, 82e41f4b71Sopenharmony_ci secondaryButton: { 83e41f4b71Sopenharmony_ci value: 'onConfirm', 84e41f4b71Sopenharmony_ci action: () => { 85e41f4b71Sopenharmony_ci event.request.grant(event.request.getAccessibleResource()); 86e41f4b71Sopenharmony_ci } 87e41f4b71Sopenharmony_ci }, 88e41f4b71Sopenharmony_ci autoCancel: false, 89e41f4b71Sopenharmony_ci cancel: () => { 90e41f4b71Sopenharmony_ci event.request.deny(); 91e41f4b71Sopenharmony_ci } 92e41f4b71Sopenharmony_ci }) 93e41f4b71Sopenharmony_ci } 94e41f4b71Sopenharmony_ci }) 95e41f4b71Sopenharmony_ci } 96e41f4b71Sopenharmony_ci } 97e41f4b71Sopenharmony_ci } 98e41f4b71Sopenharmony_ci ``` 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci- Code of the **index.html** page: 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ci ```html 103e41f4b71Sopenharmony_ci <!DOCTYPE HTML> 104e41f4b71Sopenharmony_ci <html> 105e41f4b71Sopenharmony_ci <head> 106e41f4b71Sopenharmony_ci <meta charset="utf-8" /> 107e41f4b71Sopenharmony_ci <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> 108e41f4b71Sopenharmony_ci <meta name="misapplication-tap-highlight" content="no" /> 109e41f4b71Sopenharmony_ci <meta name="HandheldFriendly" content="true" /> 110e41f4b71Sopenharmony_ci <meta name="MobileOptimized" content="320" /> 111e41f4b71Sopenharmony_ci <title>Motion and direction sensor</title> 112e41f4b71Sopenharmony_ci <meta charset="UTF-8"> 113e41f4b71Sopenharmony_ci <style> 114e41f4b71Sopenharmony_ci body { 115e41f4b71Sopenharmony_ci font-family: Arial, sans-serif; 116e41f4b71Sopenharmony_ci } 117e41f4b71Sopenharmony_ci </style> 118e41f4b71Sopenharmony_ci <script type="text/javascript"> 119e41f4b71Sopenharmony_ci function getAccelerometer() { 120e41f4b71Sopenharmony_ci var acc = new Accelerometer({frequency: 60}); 121e41f4b71Sopenharmony_ci acc.addEventListener('activate', () => console.log('Ready to measure.')); 122e41f4b71Sopenharmony_ci acc.addEventListener('error', error => console.log(`Error: ${error.name}`)); 123e41f4b71Sopenharmony_ci acc.addEventListener('reading', () => { 124e41f4b71Sopenharmony_ci console.log(`[doc_sensor]Accelerometer ${acc.timestamp}, ${acc.x}, ${acc.y}, ${acc.z}.`); 125e41f4b71Sopenharmony_ci }); 126e41f4b71Sopenharmony_ci acc.start(); 127e41f4b71Sopenharmony_ci } 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci function getGyroscope() { 130e41f4b71Sopenharmony_ci var gyr = new Gyroscope({frequency: 60}); 131e41f4b71Sopenharmony_ci gyr.addEventListener('activate', () => console.log('Ready to measure.')); 132e41f4b71Sopenharmony_ci gyr.addEventListener('error', error => console.log(`Error: ${error.name}`)); 133e41f4b71Sopenharmony_ci gyr.addEventListener('reading', () => { 134e41f4b71Sopenharmony_ci console.log(`[doc_sensor]Gyroscope ${gyr.timestamp}, ${gyr.x}, ${gyr.y}, ${gyr.z}.`); 135e41f4b71Sopenharmony_ci }); 136e41f4b71Sopenharmony_ci gyr.start(); 137e41f4b71Sopenharmony_ci } 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci function getAbsoluteOrientationSensor() { 140e41f4b71Sopenharmony_ci var aos = new AbsoluteOrientationSensor({frequency: 60}); 141e41f4b71Sopenharmony_ci aos.addEventListener('activate', () => console.log('Ready to measure.')); 142e41f4b71Sopenharmony_ci aos.addEventListener('error', error => console.log(`Error: ${error.name}`)); 143e41f4b71Sopenharmony_ci aos.addEventListener('reading', () => { 144e41f4b71Sopenharmony_ci console.log(`AbsoluteOrientationSensor data: ${ros.timestamp}, ${ros.quaternion}`); 145e41f4b71Sopenharmony_ci }); 146e41f4b71Sopenharmony_ci aos.start(); 147e41f4b71Sopenharmony_ci } 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ci function listenDeviceMotionEvent() { 150e41f4b71Sopenharmony_ci removeDeviceMotionEvent(); 151e41f4b71Sopenharmony_ci if ('DeviceMotionEvent' in window) { 152e41f4b71Sopenharmony_ci window.addEventListener('devicemotion', handleMotionEvent, false); 153e41f4b71Sopenharmony_ci } else { 154e41f4b71Sopenharmony_ci console.log ("DeviceMotionEvent is not supported."); 155e41f4b71Sopenharmony_ci } 156e41f4b71Sopenharmony_ci } 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ci function removeDeviceMotionEvent() { 159e41f4b71Sopenharmony_ci if ('DeviceMotionEvent' in window) { 160e41f4b71Sopenharmony_ci window.removeEventListener('devicemotion', handleMotionEvent, false); 161e41f4b71Sopenharmony_ci } else { 162e41f4b71Sopenharmony_ci console.log ("DeviceOrientationEvent is not supported."); 163e41f4b71Sopenharmony_ci } 164e41f4b71Sopenharmony_ci } 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ci function handleMotionEvent(event) { 167e41f4b71Sopenharmony_ci const x = event.accelerationIncludingGravity.x; 168e41f4b71Sopenharmony_ci const y = event.accelerationIncludingGravity.y; 169e41f4b71Sopenharmony_ci const z = event.accelerationIncludingGravity.z; 170e41f4b71Sopenharmony_ci console.log(`DeviceMotionEvent data: ${event.timeStamp}, ${x}, ${y}, ${z}`); 171e41f4b71Sopenharmony_ci } 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ci function listenDeviceOrientationEvent() { 174e41f4b71Sopenharmony_ci removeDeviceOrientationEvent(); 175e41f4b71Sopenharmony_ci if ('DeviceOrientationEvent' in window) { 176e41f4b71Sopenharmony_ci window.addEventListener('deviceorientation', handleOrientationEvent, false); 177e41f4b71Sopenharmony_ci } else { 178e41f4b71Sopenharmony_ci console.log ("DeviceOrientationEvent is not supported."); 179e41f4b71Sopenharmony_ci } 180e41f4b71Sopenharmony_ci } 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ci function removeDeviceOrientationEvent() { 183e41f4b71Sopenharmony_ci if ('DeviceOrientationEvent' in window) { 184e41f4b71Sopenharmony_ci window.removeEventListener('deviceorientation', handleOrientationEvent, false); 185e41f4b71Sopenharmony_ci } else { 186e41f4b71Sopenharmony_ci console.log ("DeviceOrientationEvent is not supported."); 187e41f4b71Sopenharmony_ci } 188e41f4b71Sopenharmony_ci } 189e41f4b71Sopenharmony_ci 190e41f4b71Sopenharmony_ci function listenDeviceOrientationEvent2() { 191e41f4b71Sopenharmony_ci removeDeviceOrientationEvent2(); 192e41f4b71Sopenharmony_ci if ('DeviceOrientationEvent' in window) { 193e41f4b71Sopenharmony_ci window.addEventListener('deviceorientationabsolute', handleOrientationEvent, false); 194e41f4b71Sopenharmony_ci } else { 195e41f4b71Sopenharmony_ci console.log ("DeviceOrientationEvent is not supported."); 196e41f4b71Sopenharmony_ci } 197e41f4b71Sopenharmony_ci } 198e41f4b71Sopenharmony_ci 199e41f4b71Sopenharmony_ci function removeDeviceOrientationEvent2() { 200e41f4b71Sopenharmony_ci if ('DeviceOrientationEvent' in window) { 201e41f4b71Sopenharmony_ci window.removeEventListener('deviceorientationabsolute', handleOrientationEvent, false); 202e41f4b71Sopenharmony_ci } else { 203e41f4b71Sopenharmony_ci console.log ("DeviceOrientationEvent is not supported."); 204e41f4b71Sopenharmony_ci } 205e41f4b71Sopenharmony_ci } 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_ci function handleOrientationEvent(event) { 208e41f4b71Sopenharmony_ci console.log(`DeviceOrientationEvent data: ${event.timeStamp}, ${event.absolute}, ${event.alpha}, ${event.beta}, ${event.gamma}`); 209e41f4b71Sopenharmony_ci } 210e41f4b71Sopenharmony_ci </script> 211e41f4b71Sopenharmony_ci </head> 212e41f4b71Sopenharmony_ci <body> 213e41f4b71Sopenharmony_ci <div id="dcontent" class="dcontent"> 214e41f4b71Sopenharmony_ci <h3>Motion and direction:</h3> 215e41f4b71Sopenharmony_ci <ul class="dlist"> 216e41f4b71Sopenharmony_ci <li><button type="button" onclick="getAccelerometer()">Acceleration</button></li>; 217e41f4b71Sopenharmony_ci <li><button type="button" onclick="getGyroscope()">Gyroscope</button></li> 218e41f4b71Sopenharmony_ci <li><button type="button" onclick="getAbsoluteOrientationSensor()">Device orientation (absolute positioning) </button></li>; 219e41f4b71Sopenharmony_ci <li><button type="button" onclick="listenDeviceMotionEvent()">Device motion event</button></li>; 220e41f4b71Sopenharmony_ci <li><button type="button" onclick="listenDeviceOrientationEvent()">Device orientation event</button></li>; 221e41f4b71Sopenharmony_ci <li><button type="button" onclick="listenDeviceOrientationEvent2()">Device orientation event (absolute positioning) </button></li>; 222e41f4b71Sopenharmony_ci </ul> 223e41f4b71Sopenharmony_ci </div> 224e41f4b71Sopenharmony_ci </body> 225e41f4b71Sopenharmony_ci </html> 226e41f4b71Sopenharmony_ci ``` 227