This topic describes the JavaScript APIs of WVMotion. You can refer to this topic when you create HTML5 applications or miniapps. You can call the APIs to listen for actions that a user performs to a phone, such as blowing into the phone. You can also call the API to make the phone vibrate.
WVMotion.listenBlow
Listens for blow gestures.
Input parameters
[
number]time: optional. The interval at which a blow event is generated when a user blows into the phone. Unit: seconds. In iOS, the value is in the range of[0, 1]. The default value is0. The value increases as the blow gesture continues.
Callback parameters
No callback parameters exist. If blow gesture listening is enabled, the success callback is invoked. Otherwise, the failure callback is invoked.
Event listening
motion.blow: A user blows into the phone when blow gesture listening is enabled.
Event parameters:
[
number]pass: the duration of a blow gesture that generates a blow event. The parameter value varies with the operating system. In iOS, the parameter value is in the range of[0, 1]. This value increases as the blow gesture continues. In Android, the parameter value is fixed to1.
document.addEventListener('motion.blow', function(e) {
alert('A blow gesture is detected.');
}, false);
window.WindVane.call('WVMotion', 'listenBlow', {}, function(e) {
alert('success');
}, function(e) {
alert('failure:' + JSON.stringify(e));
});
WVMotion.stopListenBlow
Stops listening for blow gestures.
Input parameters
No input parameters exist.
Callback parameters
No callback parameters exist. This API operation always invokes the success callback.
window.WindVane.call('WVMotion', 'stopListenBlow', {}, function(e) {
alert('success');
});
WVMotion.listenGyro
Enables or disables monitoring of the gyroscope.
The HTML5 application or miniapp can sense orientation changes of a phone by monitoring the gyroscope. This allows the application or miniapp to rotate the screen accordingly. The gyroscope and gravity sensor in a phone are used to monitor the phone motions. The gyroscope can obtain the Euler angles (roll, pitch, and yaw) of the phone, and the gravity sensor can obtain the magnitude of gravity in three directions.
Since the gyroscope data formats are different in iOS and Android, the gravity sensor data whose format is consistent across operating systems is used as the returned value. HTML5 applications and miniapps can use algorithms to obtain the phone orientation based on the data.
Input parameters
[
boolean]on: specifies whether to enable monitoring of the gyroscope.trueindicates that monitoring is enabled, whilefalseindicates that monitoring is disabled.[
number]frequency: the minimum interval between gyroscope events. Unit: milliseconds. The interval between gyroscope events is always greater than this parameter value.
Callback parameters
No callback parameters exist. If gyroscope monitoring is enabled or disabled, the success callback is invoked. Otherwise, the failure callback is invoked.
Event listening
motion.gyro: The phone orientation changes when the gyroscope is being monitored.
Event parameters:
[
number] x: the gravity magnitude in the x direction. Value range:[-1, 1].[
number] y: the gravity magnitude in the y direction. Value range:[-1, 1].[
number] z: the gravity magnitude in the z direction. Value range:[-1, 1].
document.addEventListener('motion.gyro', function(e) {
alert ('A change in the phone orientation is detected.');
}, false);
var params = {
// Enable or disable monitoring of the gyroscope.
on: true,
// The interval between gyroscope events.
frequency: 100
};
window.WindVane.call('WVMotion', 'listenGyro', params, function(e) {
alert('success');
}, function(e) {
alert('failure:' + JSON.stringify(e));
});
WVMotion.listeningShake
Enables or disables monitoring of shake gestures.
Input parameters
[
boolean]on: specifies whether to enable monitoring of the shake gesture.trueindicates that monitoring is enabled, whilefalseindicates that monitoring is disabled.[
number]frequency: optional. The minimum interval between shake events. The default value is500. Units: milliseconds. The interval between shake events is always greater than this parameter value.[
number]shakeThreshold: optional. The acceleration threshold used to identify shake gestures. A shake gesture is valid only when the shake acceleration exceeds the threshold. Default value:1.2.[
number]shakeNum: optional. The number of shake gestures used to generate a shake event. Default value:1.
Callback parameters
No callback parameters exist. If shake gesture monitoring is enabled or disabled, the success callback is invoked. Otherwise, the failure callback is invoked.
Event listening
motion.shake: A user shakes the phone when shake gestures are being monitored.
Event parameters:
[
number] x: the acceleration in the x direction.[
number] y: the acceleration in the y direction.[
number] z: the acceleration in the z direction.
document.addEventListener('motion.shake', function(e) {
alert ('A shake gesture is detected.');
}, false);
var params = {
// Enable or disable shake gesture monitoring.
on: true
};
window.WindVane.call('WVMotion', 'listeningShake', params, function(e) {
alert('success');
}, function(e) {
alert('failure: ' + JSON.stringify(e));
})
WVMotion.vibrate
Makes the phone vibrate.
Input parameters
[
int]duration: optional. The duration of a vibration. Unit: milliseconds. The operating system determines the parameter value by default. This parameter is available only in WindVane Android, and the parameter may be invalid on some device models.
Callback parameters
No callback parameters exist. This API operation always invokes the success callback.
window.WindVane.call('WVMotion', 'vibrate', {});
var params = {
// The vibration duration.
duration: 7000
};
window.WindVane.call('WVMotion', 'vibrate', params);
WVMotion.startAccelerometer
This API is available only in WindVane Android 1.0.3.4 or later.
Listens for acceleration data.
Input parameters
[
string] interval: optional. The interval at which the callback function of the acceleration is monitored. Default value: 'normal'. Valid values:ui: the callback interval at which the UI is updated. The parameter value is about 60 ms.
game: the callback interval at which the game is updated. The parameter value is about 20 ms.
normal: the normal callback interval. The parameter value is about 200 ms.
Callback parameters
Parameters for a success callback:
No callback parameters exist.
Parameters for a failed callback:
[
string] msg: the message about the failure.
Event listening
WVMotion.Event.accelerometer
Obtains acceleration data.
Event parameters:
[
float] x: data on the x-axis.[
float] y: data on the y-axis.[
float] z: data on the z-axis.
document.addEventListener('WVMotion.Event.accelerometer', function (e) {
alert('event accelerometer: ' + JSON.stringify(e.param));
});
var params = {
interval: 'normal'
};
window.WindVane.call('WVMotion', 'startAccelerometer', params, function(e) {
}, function(e) {
alert('startAccelerometer failure: ' + JSON.stringify(e));
});WVMotion.stopAccelerometer
This API is available only in WindVane Android 1.0.3.4 or later.
Stops listening for acceleration data.
Input parameters
No input parameters exist.
Callback parameters
No callback parameters exist. This API operation always invokes the success callback.
window.WindVane.call('WVMotion', 'stopAccelerometer', {});WVMotion.startCompass
This API is available only in WindVane Android 1.0.3.4 or later.
Listens for compass data.
Input parameters
[
string] interval: optional. the interval at which the callback function of the acceleration data is monitored. Default value: 'normal'. Valid values:ui: the callback interval at which the UI is updated. The parameter value is about 60 ms.
game: the callback interval at which the game is updated. The parameter value is about 20 ms.
normal: the normal callback interval. The parameter value is about 200 ms.
Callback parameters
Parameters for a success callback:
No callback parameters exist.
Parameters for a failed callback:
[
string] msg: the message about the failure.
Event listening
WVMotion.Event.compass
Obtains the compass data.
Event parameters:
[
float] direction: the degree between the direction that you are facing and the due north direction. Value range: [0, 360).[
long] timestamp: the timestamp.
document.addEventListener('WVMotion.Event.compass', function (e) {
alert('event compass: ' + JSON.stringify(e.param));
});
var params = {
interval: 'normal'
};
window.WindVane.call('WVMotion', 'startCompass', params, function(e) {
}, function(e) {
alert('startCompass failure: ' + JSON.stringify(e));
});WVMotion.stopCompass
This API is available only in WindVane Android 1.0.3.4 or later.
Stops listening for compass data.
Input parameters
No input parameters exist.
Callback parameters
No callback parameters exist. This API operation always invokes the success callback.
window.WindVane.call('WVMotion', 'stopCompass', {});