From 7c3b534256308196005ff2b5dd740c62e0da25ba Mon Sep 17 00:00:00 2001 From: Rui Mendes Date: Mon, 29 Jun 2026 14:58:50 +0100 Subject: [PATCH 1/4] docs(motion): add orientation listener example to README --- motion/README.md | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/motion/README.md b/motion/README.md index f6c04f035..0cd5046e6 100644 --- a/motion/README.md +++ b/motion/README.md @@ -12,7 +12,7 @@ npx cap sync ## Permissions This plugin is currently implemented using Web APIs. Most browsers require -permission before using this API. To request permission, prompt the user for +permission before using these APIs. To request permission, prompt the user for permission on any user-initiated action (such as a button click): ```typescript @@ -21,10 +21,12 @@ import { Motion } from '@capacitor/motion'; let accelHandler: PluginListenerHandle; +let orientationHandler: PluginListenerHandle; myButton.addEventListener('click', async () => { try { await DeviceMotionEvent.requestPermission(); + await DeviceOrientationEvent.requestPermission(); } catch (e) { // Handle error return; @@ -34,13 +36,20 @@ myButton.addEventListener('click', async () => { accelHandler = await Motion.addListener('accel', event => { console.log('Device motion event:', event); }); + + orientationHandler = await Motion.addListener('orientation', event => { + console.log('Device orientation event:', event); + }); }); -// Stop the acceleration listener -const stopAcceleration = () => { +// Stop a specific listener +const stopListening = () => { if (accelHandler) { accelHandler.remove(); } + if (orientationHandler) { + orientationHandler.remove(); + } }; // Remove all listeners @@ -51,7 +60,10 @@ const removeListeners = () => { See the [`DeviceMotionEvent`](https://developer.mozilla.org/en-US/docs/Web/API/DeviceMotionEvent) -API to understand the data supplied in the 'accel' event. +and +[`DeviceOrientationEvent`](https://developer.mozilla.org/en-US/docs/Web/API/DeviceOrientationEvent) +APIs to understand the data supplied in the `'accel'` and `'orientation'` +events respectively. ## API From 9cc99150f9fe9419f91714e18e250b7539d202ff Mon Sep 17 00:00:00 2001 From: Rui Mendes Date: Tue, 30 Jun 2026 12:26:26 +0100 Subject: [PATCH 2/4] split accel and orientation into separate examples --- motion/README.md | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/motion/README.md b/motion/README.md index 0cd5046e6..dfbae9856 100644 --- a/motion/README.md +++ b/motion/README.md @@ -23,10 +23,9 @@ import { Motion } from '@capacitor/motion'; let accelHandler: PluginListenerHandle; let orientationHandler: PluginListenerHandle; -myButton.addEventListener('click', async () => { +myAccelerationButton.addEventListener('click', async () => { try { await DeviceMotionEvent.requestPermission(); - await DeviceOrientationEvent.requestPermission(); } catch (e) { // Handle error return; @@ -36,17 +35,31 @@ myButton.addEventListener('click', async () => { accelHandler = await Motion.addListener('accel', event => { console.log('Device motion event:', event); }); +}); + +myOrientationButton.addEventListener('click', async () => { + try { + await DeviceOrientationEvent.requestPermission(); + } catch (e) { + // Handle error + return; + } + // Once the user approves, can start listening: orientationHandler = await Motion.addListener('orientation', event => { console.log('Device orientation event:', event); }); }); -// Stop a specific listener -const stopListening = () => { +// Stop the acceleration listener +const stopAcceleration = () => { if (accelHandler) { accelHandler.remove(); } +}; + +// Stop the orientation listener +const stopOrientation = () => { if (orientationHandler) { orientationHandler.remove(); } From d9fd7df0bce7357fc5a01c22c72c71b52795bb3b Mon Sep 17 00:00:00 2001 From: Rui Mendes Date: Thu, 2 Jul 2026 16:56:52 +0100 Subject: [PATCH 3/4] fix examples --- motion/README.md | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/motion/README.md b/motion/README.md index dfbae9856..538a753b1 100644 --- a/motion/README.md +++ b/motion/README.md @@ -11,9 +11,10 @@ npx cap sync ## Permissions -This plugin is currently implemented using Web APIs. Most browsers require -permission before using these APIs. To request permission, prompt the user for -permission on any user-initiated action (such as a button click): +This plugin is currently implemented using Web APIs. On iOS, permission must +be requested before accessing device motion or orientation events. Other +platforms do not require the check. To request permission, prompt the user +on any user-initiated action (such as a button click): ```typescript import { PluginListenerHandle } from '@capacitor/core'; @@ -24,11 +25,14 @@ let accelHandler: PluginListenerHandle; let orientationHandler: PluginListenerHandle; myAccelerationButton.addEventListener('click', async () => { - try { - await DeviceMotionEvent.requestPermission(); - } catch (e) { - // Handle error - return; + if (typeof DeviceMotionEvent.requestPermission === 'function') { + try { + const permission = await DeviceMotionEvent.requestPermission(); + if (permission !== 'granted') return; + } catch (e) { + // Handle error + return; + } } // Once the user approves, can start listening: @@ -38,11 +42,14 @@ myAccelerationButton.addEventListener('click', async () => { }); myOrientationButton.addEventListener('click', async () => { - try { - await DeviceOrientationEvent.requestPermission(); - } catch (e) { - // Handle error - return; + if (typeof DeviceOrientationEvent.requestPermission === 'function') { + try { + const permission = await DeviceOrientationEvent.requestPermission(); + if (permission !== 'granted') return; + } catch (e) { + // Handle error + return; + } } // Once the user approves, can start listening: From 6630c0b12cd7e5ea9c343f110648bbebadae157b Mon Sep 17 00:00:00 2001 From: Rui Mendes Date: Thu, 2 Jul 2026 17:30:41 +0100 Subject: [PATCH 4/4] change permission description --- motion/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/motion/README.md b/motion/README.md index 538a753b1..132626cb2 100644 --- a/motion/README.md +++ b/motion/README.md @@ -11,10 +11,10 @@ npx cap sync ## Permissions -This plugin is currently implemented using Web APIs. On iOS, permission must -be requested before accessing device motion or orientation events. Other -platforms do not require the check. To request permission, prompt the user -on any user-initiated action (such as a button click): +This plugin is currently implemented using Web APIs. On iOS devices, +permission must be requested before accessing device motion or orientation +events. To request permission, prompt the user on any user-initiated action +(such as a button click): ```typescript import { PluginListenerHandle } from '@capacitor/core';