accelerometer module sensor

Posted by ShaolinWood on Mon, 24 Jan 2022 01:06:12 +0100

The accelerator module manages the device acceleration sensor, which is used to obtain the device acceleration information, including the acceleration information in three directions: x (screen horizontal direction), y (vertical screen horizontal direction) and z (vertical screen plane direction). Via plus Accelerometer obtains the device acceleration sensor management object.

method:

Object:

Callback method:

jurisdiction:

5 + function modules (permissions)

<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#ffffff">{</span>
<span style="color:#87ceeb">// ...</span>
<span style="color:#ffa0a0">"permissions"</span><span style="color:#ffffff">:{</span>
	<span style="color:#87ceeb">// ...</span>
	<span style="color:#ffa0a0">"Accelerometer"</span><span style="color:#ffffff">:</span> <span style="color:#ffffff">{</span>
		<span style="color:#ffa0a0">"description"</span><span style="color:#ffffff">:</span> <span style="color:#Ffa0a0 ">" acceleration sensor“</span>
	<span style="color:#ffffff">}</span>
<span style="color:#ffffff">}</span>
<span style="color:#ffffff">}</span>
			</code></span></span>

Get the acceleration information of the current device

<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>void</strong></span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">accelerometer</span><span style="color:#ffffff">.</span><span style="color:#ffffff">getCurrentAcceleration</span><span style="color:#ffffff">(</span><span style="color:#ffffff"> successCB</span><span style="color:#ffffff">,</span><span style="color:#ffffff"> errorCB </span><span style="color:#ffffff">);</span>
				</code></span></span>

explain:

Acceleration is the motion sensor of the relative motion change (increase and decrease) made by the equipment in the current direction. The acceleration information includes information in x, y and z directions. Acceleration information can be returned through the successCB callback function. If the acceleration information acquisition fails, call the callback function errorCB.

Parameters:

Return value:

void: None

Example:

<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#87ceeb "> / / get acceleration information</span>
<span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff"> getCurrentAcceleration</span><span style="color:#ffffff">(){</span><span style="color:#ffffff">
	plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">accelerometer</span><span style="color:#ffffff">.</span><span style="color:#ffffff">getCurrentAcceleration</span><span style="color:#ffffff">(</span> <span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff">(</span><span style="color:#ffffff"> a </span><span style="color:#ffffff">)</span> <span style="color:#ffffff">{</span><span style="color:#ffffff">
		plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">nativeUI</span><span style="color:#ffffff">.</span><span style="color:#ffffff">alert</span><span style="color:#ffffff">(</span> <span style="color:#ffa0a0">"Acceleration\nx:"</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> a</span><span style="color:#ffffff">.</span><span style="color:#ffffff">xAxis </span><span style="color:#ffffff">+</span> <span style="color:#ffa0a0">"\ny:"</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> a</span><span style="color:#ffffff">.</span><span style="color:#ffffff">yAxis </span><span style="color:#ffffff">+</span> <span style="color:#ffa0a0">"\nz:"</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> a</span><span style="color:#ffffff">.</span><span style="color:#ffffff">zAxis </span><span style="color:#ffffff">);</span>
	<span style="color:#ffffff">}</span> <span style="color:#ffffff">);</span>
<span style="color:#ffffff">}</span>
				</code></span></span>

Precautions for using plus in uni app

Monitor equipment acceleration change information

<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#98fb98">Number</span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">accelerometer</span><span style="color:#ffffff">.</span><span style="color:#ffffff">watchAcceleration</span><span style="color:#ffffff">(</span><span style="color:#ffffff"> successCB</span><span style="color:#ffffff">,</span><span style="color:#ffffff"> errorCB</span><span style="color:#ffffff">,</span><span style="color:#ffffff"> option </span><span style="color:#ffffff">);</span>
				</code></span></span>

explain:

Acceleration is the motion sensor of the relative motion change (increase and decrease) made by the equipment in the current direction. The acceleration information includes information in x, y and z directions. watchAcceleration obtains the acceleration information of the device every fixed time and returns it through the successCB callback function. The time interval for obtaining equipment acceleration information can be set through the frequency parameter of option. If the acceleration information acquisition fails, call the callback function errorCB.

Parameters:

  • successCB: ( AccelerometerSuccessCallback ) required # successful callback function

    When the acceleration information of the device is successfully obtained, the callback will return the acceleration information.

  • errorCB: ( AccelerometerErrorCallback (optional) failed callback function

    When the device acceleration information fails to be obtained, the callback function returns an error message.

  • options: ( AccelerometerOption ) optional acceleration information parameters

    Monitor the parameters of equipment acceleration information, such as the frequency of updating data, etc.

Return value:

Number: used to identify the acceleration information listener, which can be cancelled by clearWatch method.

Example:

<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>var</strong></span><span style="color:#ffffff"> wid </span><span style="color:#ffffff">=</span> <span style="color:#f0e68c"><strong>null</strong></span><span style="color:#ffffff">;</span>
<span style="color:#87ceeb "> / / monitor the acceleration change of the device</span>
<span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff"> watchAcceleration</span><span style="color:#ffffff">(){</span><span style="color:#ffffff">
	wid </span><span style="color:#ffffff">=</span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">accelerometer</span><span style="color:#ffffff">.</span><span style="color:#ffffff">watchAcceleration</span><span style="color:#ffffff">(</span> <span style="color:#f0e68c"><strong>function</strong></span> <span style="color:#ffffff">(</span><span style="color:#ffffff"> a </span><span style="color:#ffffff">)</span> <span style="color:#ffffff">{</span><span style="color:#ffffff">
		console</span><span style="color:#ffffff">.</span><span style="color:#ffffff">log</span><span style="color:#ffffff">(</span> <span style="color:#ffa0a0">"x:"</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> a</span><span style="color:#ffffff">.</span><span style="color:#ffffff">xAxis </span><span style="color:#ffffff">+</span> <span style="color:#ffa0a0">"\ny:"</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> a</span><span style="color:#ffffff">.</span><span style="color:#ffffff">yAxis </span><span style="color:#ffffff">+</span> <span style="color:#ffa0a0">"\nz:"</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> a</span><span style="color:#ffffff">.</span><span style="color:#ffffff">zAxis</span><span style="color:#ffffff">);</span>
	<span style="color:#ffffff">},</span> <span style="color:#f0e68c"><strong>function</strong></span> <span style="color:#ffffff">(</span><span style="color:#ffffff"> e </span><span style="color:#ffffff">)</span> <span style="color:#ffffff">{</span><span style="color:#ffffff">
		plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">nativeUI</span><span style="color:#ffffff">.</span><span style="color:#ffffff">alert</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">"watchAcceleration error: "</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> JSON</span><span style="color:#ffffff">.</span><span style="color:#ffffff">stringify</span><span style="color:#ffffff">(</span><span style="color:#ffffff">e</span><span style="color:#ffffff">));</span> 
	<span style="color:#ffffff">}</span> <span style="color:#ffffff">);</span> 
<span style="color:#ffffff">}</span>
				</code></span></span>

Precautions for using plus in uni app

Turn off the acceleration information of the monitoring device

<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>void</strong></span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">accelerometer</span><span style="color:#ffffff">.</span><span style="color:#ffffff">clearWatch</span><span style="color:#ffffff">(</span><span style="color:#ffffff"> watchId </span><span style="color:#ffffff">);</span>
				</code></span></span>

explain:

Close the monitoring device acceleration information, and close the open monitoring operation calling the watchAcceleration method.

Parameters:

  • watchId: (Number) required

    The acceleration listener ID that needs to be cancelled, and the return value of calling the watchAcceleration method.

Return value:

void: None

Example:

<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>var</strong></span><span style="color:#ffffff"> wid </span><span style="color:#ffffff">=</span> <span style="color:#f0e68c"><strong>null</strong></span><span style="color:#ffffff">;</span>
<span style="color:#87ceeb">// wid = plus.accelerometer.watchAcceleration(...);</span>
<span style="color:#87ceeb "> / / turn off the monitoring device</span>
<span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff"> clearWatch</span><span style="color:#ffffff">(){</span><span style="color:#ffffff">
	plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">accelerometer</span><span style="color:#ffffff">.</span><span style="color:#ffffff">clearWatch</span><span style="color:#ffffff">(</span><span style="color:#ffffff">wid</span><span style="color:#ffffff">);</span><span style="color:#ffffff">
	wid </span><span style="color:#ffffff">=</span> <span style="color:#f0e68c"><strong>null</strong></span><span style="color:#ffffff">;</span>
<span style="color:#ffffff">}</span>
				</code></span></span>

Precautions for using plus in uni app

Equipment acceleration information object

<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>interface</strong></span> <span style="color:#98fb98">Acceleration</span> <span style="color:#ffffff">{</span>
	<span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#98fb98">Number</span><span style="color:#ffffff"> xAxis</span><span style="color:#ffffff">;</span>
	<span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#98fb98">Number</span><span style="color:#ffffff"> yAxis</span><span style="color:#ffffff">;</span>
	<span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#98fb98">Number</span><span style="color:#ffffff"> zAxis</span><span style="color:#ffffff">;</span>  
<span style="color:#ffffff">}</span>
				</code></span></span>

explain:

JSON object, which saves and obtains the acceleration information of the device, including the acceleration information in x, y and z directions.

Properties:

  • xAxis: (Number type) acceleration in the x-axis direction

    Obtain the acceleration in the x-axis direction of the current device, floating-point data, which is consistent with the acceleration value in physics.

  • yAxis: (Number type) acceleration in y-axis direction

    Obtain the acceleration in the y-axis direction of the current device, floating-point data, which is consistent with the acceleration value in physics.

  • zAxis: (type Number) acceleration in z-axis direction

    Obtain the acceleration in the z-axis direction of the current device, floating-point data, which is consistent with the acceleration value in physics.

Monitoring equipment acceleration sensor parameters

<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>interface</strong></span> <span style="color:#98fb98">Acceleration</span> <span style="color:#ffffff">{</span>
	<span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#98fb98">DOMString</span><span style="color:#ffffff"> frequency</span><span style="color:#ffffff">;</span>
<span style="color:#ffffff">}</span>
				</code></span></span>

explain:

JSON object, which is used to set parameters for obtaining equipment acceleration information.

Properties:

  • frequency: (Number type) the interval between updating acceleration information

    The time interval for the listener to obtain acceleration information, in MS, and the default value is 500ms

Example:

<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>var</strong></span><span style="color:#ffffff"> wid </span><span style="color:#ffffff">=</span> <span style="color:#f0e68c"><strong>null</strong></span><span style="color:#ffffff">;</span>
<span style="color:#87ceeb "> / / monitor the acceleration change of the device</span>
<span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff"> watchAcceleration</span><span style="color:#ffffff">(){</span><span style="color:#ffffff">
	wid </span><span style="color:#ffffff">=</span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">accelerometer</span><span style="color:#ffffff">.</span><span style="color:#ffffff">watchAcceleration</span><span style="color:#ffffff">(</span> <span style="color:#f0e68c"><strong>function</strong></span> <span style="color:#ffffff">(</span><span style="color:#ffffff"> a </span><span style="color:#ffffff">)</span> <span style="color:#ffffff">{</span><span style="color:#ffffff">
		console</span><span style="color:#ffffff">.</span><span style="color:#ffffff">log</span><span style="color:#ffffff">(</span> <span style="color:#ffa0a0">"x:"</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> a</span><span style="color:#ffffff">.</span><span style="color:#ffffff">xAxis </span><span style="color:#ffffff">+</span> <span style="color:#ffa0a0">"\ny:"</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> a</span><span style="color:#ffffff">.</span><span style="color:#ffffff">yAxis </span><span style="color:#ffffff">+</span> <span style="color:#ffa0a0">"\nz:"</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> a</span><span style="color:#ffffff">.</span><span style="color:#ffffff">zAxis</span><span style="color:#ffffff">);</span>
	<span style="color:#ffffff">},</span> <span style="color:#f0e68c"><strong>function</strong></span> <span style="color:#ffffff">(</span><span style="color:#ffffff"> e </span><span style="color:#ffffff">)</span> <span style="color:#ffffff">{</span><span style="color:#ffffff">
		plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">nativeUI</span><span style="color:#ffffff">.</span><span style="color:#ffffff">alert</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">"watchAcceleration error: "</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> JSON</span><span style="color:#ffffff">.</span><span style="color:#ffffff">stringify</span><span style="color:#ffffff">(</span><span style="color:#ffffff">e</span><span style="color:#ffffff">));</span> 
	<span style="color:#ffffff">},</span> <span style="color:#ffffff">{</span><span style="color:#ffffff">frequency</span><span style="color:#ffffff">:</span><span style="color:#cd5c5c">1000</span><span style="color:#ffffff">});</span> <span style="color:#87ceeb "> / / set the update interval to 1s</span>
<span style="color:#ffffff">}</span>
				</code></span></span>

Precautions for using plus in uni app

Callback function to obtain device acceleration information successfully

<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>void</strong></span><span style="color:#ffffff"> onSuccess</span><span style="color:#ffffff">(</span><span style="color:#ffffff"> acceleration </span><span style="color:#ffffff">)</span> <span style="color:#ffffff">{</span>
	<span style="color:#87ceeb">// Get acceleration code.</span>
<span style="color:#ffffff">}</span>
				</code></span></span>

Parameters:

  • acceleration: ( Acceleration (required) acceleration information of equipment

    Acceleration type object, which is used to obtain the detailed acceleration value in each direction.

Return value:

void: None

Callback function failed to get device acceleration information

<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>function</strong></span> <span style="color:#f0e68c"><strong>void</strong></span><span style="color:#ffffff"> onAccelerometerError</span><span style="color:#ffffff">(</span><span style="color:#98fb98">Exception</span><span style="color:#ffffff"> error</span><span style="color:#ffffff">)</span> <span style="color:#ffffff">{</span>
	<span style="color:#87ceeb">// Handle error</span>
	<span style="color:#f0e68c"><strong>var</strong></span><span style="color:#ffffff"> code </span><span style="color:#ffffff">=</span><span style="color:#ffffff"> error</span><span style="color:#ffffff">.</span><span style="color:#ffffff">code</span><span style="color:#ffffff">;</span> <span style="color:#87ceeb "> / / error code</span>
	<span style="color:#f0e68c"><strong>var</strong></span><span style="color:#ffffff"> message </span><span style="color:#ffffff">=</span><span style="color:#ffffff"> error</span><span style="color:#ffffff">.</span><span style="color:#ffffff">message</span><span style="color:#ffffff">;</span> <span style="color:#87ceeb "> / / error description</span>
<span style="color:#ffffff">}</span>
				</code></span></span>

Parameters:

  • Error: (Exception) required to obtain the error information of acceleration operation

    You can use error Code (type Number) to obtain the error code; You can use error Message (String type) gets the error description information.

Return value:

void: None

Topics: Javascript Vue.js elementUI