Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
Delphi

How to vibrate iOS and Android phones using FireMonkey and XE8

Pawel Glowacki asked about how to vibrate a smartphone from Delphi code for iOS and Android. The following sample code for Delphi and FireMonkey was provided by Thierry Laborde and Williamm Wittwer from Embarcadero France:

unit Umain;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  FMX.Controls.Presentation, FMX.StdCtrls
{$IFDEF ANDROID}
  ,Androidapi.JNI.Os,
  Androidapi.JNI.GraphicsContentViewText,
  Androidapi.Helpers,
  Androidapi.JNIBridge
{$ENDIF}
{$IFDEF IOS}
  ,IOSapi.MediaPlayer,
  IOSapi.CoreGraphics,
  FMX.Platform,
  FMX.Platform.IOS,
  IOSapi.UIKit,
  Macapi.ObjCRuntime,
  Macapi.ObjectiveC,
  iOSapi.Cocoatypes,
  Macapi.CoreFoundation,
  iOSapi.Foundation,
  iOSapi.CoreImage,
  iOSapi.QuartzCore,
  iOSapi.CoreData
{$ENDIF}
  ;

{$IFDEF IOS}
Const
  libAudioToolbox        = '/System/Library/Frameworks/AudioToolbox.framework/AudioToolbox';
  kSystemSoundID_vibrate = $FFF;

Procedure AudioServicesPlaySystemSound( inSystemSoundID: integer ); Cdecl; External libAudioToolbox Name _PU + 'AudioServicesPlaySystemSound';
{$ENDIF}


type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Déclarations privées }
  public
    { Déclarations publiques }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.Button1Click(Sender: TObject);
{$IFDEF ANDROID}
Var
  Vibrator:JVibrator;
{$ENDIF}
begin
{$IFDEF ANDROID}
  Vibrator:=TJVibrator.Wrap((SharedActivityContext.getSystemService(TJContext.JavaClass.VIBRATOR_SERVICE) as ILocalObject).GetObjectID);
  // Vibrate for 500 milliseconds
  Vibrator.vibrate(500);
{$ENDIF}
{$IFDEF IOS}
  AudioServicesPlaySystemSound( kSystemSoundID_vibrate );
{$ENDIF}
end;

end.

Notes: For the iOS part you have to go in the SDK Manager, choose the SDK for iOS and click on the button to add a Path. And parameter it like in the join picture. Don’t forget after to click on the button ‘Update Local File Cache. For the Android part don’t forget to add the authorization for Vibration in the parameter of the project. http://docwiki.embarcadero.com/RADStudio/XE8/en/SDK_Manager


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Related posts
DelphiShowcase

Powerful Business Management iOS & Android App

DelphiShowcase

Visualize Our Astounding Planet's Journey Around The Sun

DelphiShowcase

This Native High-Accuracy Walking Profiler Can Measure Reality

DelphiShowcase

Ultimate Hockey Playview Application Is Delphi Powered

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES