Android Example Vibrating Service - Also with Java code inside B4A project

This is a simple project that makes use of the device's vibrator. You can do the following:
1. Switch the vibrator ON for a specified duration of time (in milliseconds)
2. Switch the vibrator ON/OFF according to a specified pattern (with a repeat option included)
The library files (fiddleAround.jar and fiddleAround.xml) are in the /files folder. Copy them to your additional libraries folder and refresh the libraries in the project. Then select fiddleAround.

The Java code that was used to generate the vibration methods with:

B4X:
package com.johan.Vibrate;

import android.content.Context;
import android.os.Vibrator;


import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.BA;

@ShortName("B4Avibrate")

public class Vibrate
{

/**
*Vibrate with a given pattern.
*Pass in an array of Longs that are the durations for which to turn on or off the vibrator in milliseconds.
*The first value indicates the number of milliseconds to wait before turning the vibrator on. The next
*value indicates the number of milliseconds for which to keep the vibrator on before turning it off.
*Subsequent values alternate between durations in milliseconds to turn the vibrator off or to turn the vibrator on.
*Parameters:
*pattern ---> an array of longs of times for which to turn the vibrator on or off.
*repeat ---> the index into pattern at which to repeat, or -1 if you don't want to repeat.
*
*You need to add the following to the manifest file of the B4A project:
*AddPermission("android.permission.VIBRATE")
*Example:<code>
*Dim mv As Vibrate
*Dim pattern() As Long
*pattern = Array As Long(0, 100, 1000, 300, 1000, 100, 1000, 200, 1000)
*Dim repeat as Int = 0
*mv.vibratePattern(pattern, repeat)</code>
*
*/
public void vibratePattern(final BA ba,long[] pattern, int repeat) {
   
  Vibrator v = (Vibrator) ba.context.getSystemService(Context.VIBRATOR_SERVICE);
  if (v.hasVibrator()) {

    v.vibrate(pattern, repeat);
  }
}

/**
*Cancel vibration (in case of method vibratePattern being active)
*You need to add the following to the manifest file of the B4A project:
*AddPermission("android.permission.VIBRATE")
*Example:<code>
*Dim mv As Vibrate
*mv.vibrateCancel</code>
*
*/
public void vibrateCancel(final BA ba) {
   
  Vibrator v = (Vibrator) ba.context.getSystemService(Context.VIBRATOR_SERVICE);
  if (v.hasVibrator()) {
     v.cancel();
  }
}

/**
*Vibrate continuously for the specified number of milliseconds
*You need to add the following to the manifest file of the B4A project:
*AddPermission("android.permission.VIBRATE")
*Example:<code>
*Dim mv As Vibrate
*Dim duration as Long = 2000
*mv.vibrateOnce(duration)</code>
*
*/
public void vibrateOnce(final BA ba, long duration) {
   
  Vibrator v = (Vibrator) ba.context.getSystemService(Context.VIBRATOR_SERVICE);
  if (v.hasVibrator()) {
     v.vibrate(duration);
  }
}

}
 

Attachments

  • JHSVIBRATE.zip
    74.4 KB · Views: 922
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
The attached project (JHSVIBRATE_JAVA) was created with B4A (V4.3 Beta) that allows you to add Java code inside the B4A project.
 

Attachments

  • JHSVIBRATE_JAVA.zip
    72.5 KB · Views: 738

xpectmore

Member
Licensed User
Longtime User
hi! nice codes examples.what about write a lib using JavaObject ? after the compiling succefully a library with java inline code using JavaObject why in the call of library(on press a button call code from library) will say the method java ,inline,wasn't found then apk will crash?
 

Douglas Farias

Expert
Licensed User
Longtime User
Hi @Johan Schoeman
its possible add amplitude to your example pls?

  • If you want to control strength of the vibration you need to use AMPLITUDE parameter.
  • In android vibration AMPLITUDE 0 says vibration motor is off and 255 says vibration would be performed at it’s full strength.

and one question.
on vibrateOnce its possible pass a infinity value? vibrate infinity...

thank you
 
Top