Vibrator Introduction
Vibrator is the object that allows you to use the vibrating function of the mobile device. The user can create desired vibration patterns, as well as simple vibrations, using the properties and methods provided.
Generating Vibration
To generate vibrations on the mobile device, call the play method, and to stop vibration, call the stop method.
${sample}
If you enter the number of vibration repetitions and touch the Play button, then the vibration is repeated as many times as the entered number. If you enter 0, it repeats infinitely and the log message is output at the bottom whenever vibration occurs. If you touch the Stop button while the vibration is occurring, the vibration will stop.
${sample_element}
- Vibrator > hasVibrator
This is the method that checks if the vibration function is available.
- Vibrator > repeatcount
This is the property that sets the number of vibration pattern repetitions. If the property value is not set, then it is applied as 1, and when set to 0, then the vibration is repeated infinitely.
- Vibrator > play
This is the method that generates vibration. It generates vibration by repeating as many times as the value set in the repeatcount property according to the pattern set in the patterns property.
- Vibrator > stop
This is the method that stops vibration.
${sample_step}
1
Configuring the Screen
Select the Vibrator object and click on the design screen to add it. The added Vibrator object can be checked in the Invisible Object window.
Place the Static and Button components to configure the screen and the Edit component to receive the number of repetitions appropriately as shown in the example figure.
Components and objects used to configure the screen are as follows.
Component / Object | ID |
---|---|
Vibrator | Vibrator00 |
Static | stt_count |
Edit | edt_count |
Button | btn_vibPlay |
btn_vibStop | |
TextArea | TextArea00 |
2
Writing the User Function
Declare numCount, the global variable to output the number of vibrations as the log message for each vibration, and declare the initData function to initialize the timer and variable every time the operation is performed.
//Global Variable var numCount = 0; //Variables initialization function this.initData = function() { this.killTimer(0); numCount = 0; this.TextArea00.set_value(this.TextArea00.value +"\n"); };
Declare the timer function that outputs log messages to TextArea at specific time intervals.
//Functions that are called repeatedly at specified time intervals this.timer = function() { if(this.Vibrator00.repeatcount == 0 || numCount < this.Vibrator00.repeatcount) { numCount++; this.TextArea00.set_value(this.TextArea00.value +"\nVibration occurs: "+ numCount +" times"); this.TextArea00.scrollBy(0, this.TextArea00.getVScrollPos()+19); } else { this.initData(); } };
3
Writing the Form Event Function
oninit Event Function
Initialize TextArea after loading the form and before displaying the log message.
this.sample_vibrator_01_oninit = function(obj:nexacro.Form,e:nexacro.EventInfo) { this.TextArea00.set_value(""); };
ontimer Event Function
Call the timer function at every specified time interval.
this.sample_vibrator_01_ontimer = function(obj:nexacro.Form,e:nexacro.TimerEventInfo) { if(e.timerid == 0) { this.timer(); } };
4
Writing the Play Button Event Function
Write the onclick event function of the Play button as follows. Check whether the vibration function of the mobile device is available, and then execute the vibration function as many times as the number of repetitions set by the user.
this.btn_vibPlay_onclick = function(obj:nexacro.Button,e:nexacro.ClickEventInfo) { if(this.Vibrator00.hasVibrator()) { var repeatCount = this.edt_count.value; if(repeatCount !== null && repeatCount !== "") { this.Vibrator00.set_repeatcount(repeatCount); this.Vibrator00.play(); } else { alert("Please enter the count of repeat for vibration."); } } else { alert("Vibration function is not available."); } };
5
Writing the Stop Button Event Function
Write the onclick event function of the Stop button as follows. Stop the vibration function of the mobile device.
this.btn_vibStop_onclick = function(obj:nexacro.Button,e:nexacro.ClickEventInfo) { this.Vibrator00.stop(); };
6
Writing the Vibrator Event Function
onplay Event Function
This is the event that occurs when the play method of the Vibrator object is called. When the user executes play, then the timer is set to output the log message.
this.Vibrator00_onplay = function(obj:nexacro.Vibrator,e:nexacro.VibratorEventInfo) { this.setTimer(0, 200); };
onstop Event Function
This is the event that occurs when the stop method of the Vibrator object is called. When the user executes stop, then the initialize function is called.
this.Vibrator00_onstop = function(obj:nexacro.Vibrator,e:nexacro.VibratorEventInfo) { this.initData(); };
onerror Event Function
This is the event that occurs when an error occurs while performing the function of the Vibrator object. The error code is output to TextArea.
this.Vibrator00_onerror = function(obj:nexacro.Vibrator,e:nexacro.VibratorErrorEventInfo) { var strResult = "\n> Vibrator00_onerror()" +"\n["+ e.errortype +"] "+ e.statuscode +" "+ e.errormsg; this.TextArea00.set_value(this.TextArea00.value + strResult); };
7
Writing the Edit Component Event Function
Write the onkeydown event function of the Edit component as follows. Process the enter key input of the virtual keyboard.
this.edt_count_onkeydown = function(obj:nexacro.Edit,e:nexacro.KeyEventInfo) { if(e.keycode == "13") { this.btn_vibPlay_onclick(); this.btn_vibPlay.setFocus(); } };
8
Checking on the Mobile Device
Enter the number of vibration repetitions and touch the Play button to check if the vibration repeats the entered number of times. Enter 0 to check if the vibration repeats infinitely, and then touch the Stop button to check if the vibration stops.
Setting Vibration Pattern
Use the patterns property when you want to set and generate the vibration to the desired pattern.
In the iOS environment, the patterns property is not operated, and the vibration pattern provided by iOS is applied.
${sample}
Enter the vibration pattern with numbers and commas (,) as follows, and then touch the Play button to generate vibration. To stop vibration, touch the Stop button.
The vibration pattern consists of vibration-free and vibration pairs. For example, if the pattern of "200,1000,200,500" is entered, then no vibration for 200ms, vibration for 1000ms, no vibration for 200ms, and vibration for 500ms occurs.
${sample_element}
- Vibrator > hasVibrator
This is the method that checks if the vibration function of the mobile device is available.
- Vibrator > patterns
This is the property that sets the vibration pattern. The pattern value is set in the form of an array of vibration-free and vibration pairs. For example, if set as "10, 100, 20, 200", no vibration for 10ms, vibration for 100ms, no vibration for 20ms, and vibration for 200ms occurs. This function is not supported in iOS.
- Vibrator > play
This is the method that generates vibration. It generates vibration by repeating as many times as the value set in the repeatcount property according to the pattern set in the patterns property.
- Vibrator > stop
This is the method that stops vibration.
${sample_step}
1
Configuring the Screen
Select the Vibrator object and click on the design screen to add it. The added Vibrator object can be checked in the Invisible Object window.
Place the Static and Button components to configure the screen and the Edit component to receive the number of repetitions appropriately as shown in the example figure.
Components and objects used to configure the screen are as follows.
Component / Object | ID |
---|---|
Vibrator | Vibrator00 |
Static | stt_pattern |
Edit | edt_pattern |
Button | btn_vibPlay |
btn_vibStop |
2
Writing the Play Button Event Function
Write the onclick event function of the Play button as follows. Check if the vibration function of the mobile device is available and then execute the vibration function according to the vibration pattern set by the user.
this.btn_vibPlay_onclick = function(obj:nexacro.Button,e:nexacro.ClickEventInfo) { if(this.Vibrator00.hasVibrator()) { var strInput = this.edt_pattern.value; if(strInput !== null && strInput !== "") { var arrPattern = strInput.split(","); this.Vibrator00.set_patterns(arrPattern); this.Vibrator00.play(); } else { alert("Please enter a vibration pattern."); } } else { alert("Vibration function is not available."); } };
3
Writing the Stop Button Event Function
Write the onclick event function of the Stop button as follows. Stop the vibration function of the mobile device.
this.btn_vibStop_onclick = function(obj:nexacro.Button,e:nexacro.ClickEventInfo) { this.Vibrator00.stop(); };
4
Writing the onerror Event Function of Vibrator Object
This is the event that occurs when an error occurs while executing the function of the Vibrator object. The error code is output.
this.Vibrator00_onerror = function(obj:nexacro.Vibrator,e:nexacro.VibratorErrorEventInfo) { var strResult = "\n> Vibrator00_onerror()" +"\n["+ e.errortype +"] "+ e.statuscode +" "+ e.errormsg; alert(strResult); };
5
Writing the Edit Component Event Function
Write the onkeydown event function of the Edit component as follows. Process the enter key input of the virtual keyboard.
this.edt_count_onkeydown = function(obj:nexacro.Edit,e:nexacro.KeyEventInfo) { if(e.keycode == "13") { this.btn_vibPlay_onclick(); this.btn_vibPlay.setFocus(); } };
6
Checking on the Mobile Device
Enter the vibration pattern and touch the Play button to check if the vibration occurs according to the input pattern. While vibration is occurring, touch the Stop button to check if the vibration stops.