async function myCellPhoneMotion(){ document.getElementById('mySpanExport').innerHTML = `Checking if using a cell phone!
`; document.getElementById('mySpanExport').innerHTML += `/Android/i.test(navigator.userAgent): ${/Android/i.test(navigator.userAgent)}
`; document.getElementById('mySpanExport').innerHTML += `/iPhone|iPod/i.test(navigator.userAgent): ${/iPhone|iPod/i.test(navigator.userAgent)}
`; document.getElementById('mySpanExport').innerHTML += `navigator.userAgent: ${navigator.userAgent}
` if (typeof DeviceMotionEvent.requestPermission === 'function') { try { const permissionState = await DeviceMotionEvent.requestPermission(); if (permissionState === 'granted') { window.addEventListener('devicemotion', handleMotionEvent); // myCellPhoneEvent = false myCellPhoneOnce = true } } catch (error) { console.log(error); document.getElementById('mySpanResults').innerHTML = `Error: ${error}`; } } else { // if things are already setup then just add the event listener window.addEventListener('devicemotion', handleMotionEvent); } } function handleMotionEvent(event) { if (myCellPhoneOnce){ const { accelerationIncludingGravity } = event; let { x, y, z } = accelerationIncludingGravity; if ( /Android/i.test(navigator.userAgent) ) { // correct only if not on desktop setting. Android orientation is opposite apple x *= -1; y *= -1; z *= -1; } const formattedX = typeof x === 'number' ? x.toFixed(2) : 'NaN'; const formattedY = typeof y === 'number' ? y.toFixed(2) : 'NaN'; const formattedZ = typeof z === 'number' ? z.toFixed(2) : 'NaN'; // document.getElementById('myDiv01').innerHTML = `iPhone x: ${formattedX}, y: ${formattedY}, z: ${formattedZ}`; // Start collecting data if startTime is null if (startTime === null) { startTime = performance.now(); } // Calculate elapsed time in milliseconds const elapsedTime = performance.now() - startTime; // Collect accelerometer data for 500 ms if (elapsedTime < 410) { // 500 ~ 30 samples, I want 25 samples accelerometerData.push([formattedX, formattedY, formattedZ]); } else if (elapsedTime > 1000 ){} // estentially do nothing if the event handler does not get erased else { // put the data in the main textarea if (mySendFirstLine) { document.getElementById('myArea01').value = 'accelX,accelY,accelZ\r\n'; } // prints first line for CSV saving document.getElementById('myArea01').value += accelerometerData.join('\r\n') // puts linefeed at end of data myShowGraph() myCellPhoneOnce = false window.removeEventListener('devicemotion', handleMotionEvent); } } }