목차

Accelerometer

설명

제약사항

Android

N/A

iOS

accelerometer.getCurrentAcceleration
accelerometer.watchAcceleration

환경 설정

Android

<plugin name="Accelerometer" value="org.apache.cordova.AccelListener" />

iOS

<key>Plugins</key>
<dict>
    <key>Accelerometer</key>
    <string>CDVAccelerometer</string>
</dict>

관련 함수

accelerometer.getCurrentAcceleration

navigator.accelerometer.getCurrentAcceleration(accelerometerSuccess, accelerometerError);
function onSuccess(acceleration) {
    alert('Acceleration X: ' + acceleration.x + '\n' +
          'Acceleration Y: ' + acceleration.y + '\n' +
          'Acceleration Z: ' + acceleration.z + '\n' +
          'Timestamp: '      + acceleration.timestamp + '\n');
};

function onError() {
    alert('onError!');
};

navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);

accelerometer.watchAcceleration

var watchID = navigator.accelerometer.watchAcceleration(accelerometerSuccess,
                                                       accelerometerError,
                                                       [accelerometerOptions]);
function onSuccess(acceleration) {
    alert('Acceleration X: ' + acceleration.x + '\n' +
          'Acceleration Y: ' + acceleration.y + '\n' +
          'Acceleration Z: ' + acceleration.z + '\n' +
          'Timestamp: '      + acceleration.timestamp + '\n');
};

function onError() {
    alert('onError!');
};

var options = { frequency: 3000 };  // Update every 3 seconds

var watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);

accelerometer.clearWatch

navigator.accelerometer.clearWatch(watchID);
var watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);

...

navigator.accelerometer.clearWatch(watchID);

파라미터

accelerometerSuccess

function onSuccess(acceleration) {
    alert('Acceleration X: ' + acceleration.x + '\n' +
          'Acceleration Y: ' + acceleration.y + '\n' +
          'Acceleration Z: ' + acceleration.z + '\n' +
          'Timestamp: '      + acceleration.timestamp + '\n');
};

accelerometerError

function() {
    // Handle the error
}

accelerometerOptions

유형설명타입비고
frequency특정 시간 간격으로 가속 센서를 감지한다.Number기본값 : 10000

Objects

Acceleration

function onSuccess(acceleration) {
    alert('Acceleration X: ' + acceleration.x + '\n' +
          'Acceleration Y: ' + acceleration.y + '\n' +
          'Acceleration Z: ' + acceleration.z + '\n' +
          'Timestamp: '      + acceleration.timestamp + '\n');
};

function onError() {
    alert('onError!');
};

navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);
유형설명타입비고
Xx 축 가속 값을 기록.Number값 : 0 ~ 1
Yy 축 가속 값을 기록.Number값 : 0 ~ 1
Zz 축 가속 값을 기록.Number값 : 0 ~ 1
timestamp측정한 milliseconds 단위의 시간.DOMTimeStampmilliseconds

참고 자료