9

How do I access the accelerometer in iOS using Delphi XE2?

I tried looking through the IDE but did not find a component.

Johan
  • 74,508
  • 24
  • 191
  • 319

3 Answers3

4

Delphi XE2 supports deploying to OSX on a mac. Deploying to an iPhone is apparently pretty easy, but it still involves using XCode as a final deployment tool. Since Delphi doesn't "officially" support deploying to iPhone hardware, I doubt they have included any components that are specific to it.

Disclaimer: I just downloaded XE2 a few hours ago, and I've only deployed one mac application so far (and it worked beautifully). So I'm far from an expert.

Edit: Anders Ohlsson has apparently written an accelerometer component for XE2. I have not tried it, but here it is.

JosephStyons
  • 57,317
  • 63
  • 160
  • 234
  • Curious about the downvote. If I'm wrong about this, I'd love to hear what the correct way is. Some kind of simulated direct iOS hardware access from Delphi on Windows would be a real treat. – JosephStyons Sep 21 '11 at 13:17
  • I've included the source code below and I've used the template to write a component for the iOS compass/GPS. – Johan Nov 09 '11 at 22:11
0

Joseph is correct. Philip Hess has been contributing to the Delphi iOS newsgroups, and other useful information, eg:

https://forums.embarcadero.com/message.jspa?messageID=390713&tstart=125

He also has this page, here:

http://web.me.com/macpgmr/ObjP/Xcode4/ObjP_Intro.html

Which may be helpful to you. I'm currently wading through it all in order to use the CoreLocation services (amongst others). It's definitely not for the faint of heart.

Dave N
  • 1
0

There's a component that reads the accelerometer on the Embarcadero blog:
http://edn.embarcadero.com/article/41748
Here's the download link for the component: http://cc.embarcadero.com/item/28609

And here's the source code by Anders Ohlsson:
(I used this to build a component for the GPS/Compass).

unit Accelerometer;

{$IFDEF FPC}
{$mode objfpc}{$H+}
{$modeswitch objectivec1}
{$ENDIF}

interface

uses
  SysUtils, Classes, FMX_Types
{$IFDEF FPC}
  , iPhoneAll
{$ENDIF}
  ;

type
  TAccelerateEvent = procedure(x,y : Double) of object;

  TiOSAccelerometer = class(TFmxObject)
  private
    FOnAccelerate: TAccelerateEvent;
    { Private declarations }
  protected
    { Protected declarations }
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
  published
    { Published declarations }
    property OnAccelerate: TAccelerateEvent read FOnAccelerate write FOnAccelerate;
  end;

{$IFDEF FPC}
const
  kAccelerometerFrequency = 100.0; // Hz
{$ENDIF}

{$IFDEF FPC}
type
  UIAcceleration = objcclass external (NSObject)
  public
    function timestamp: NSTimeInterval; message 'timestamp';
    function x: UIAccelerationValue; message 'x';
    function y: UIAccelerationValue; message 'y';
    function z: UIAccelerationValue; message 'z';
  end;
{$ENDIF}

{$IFDEF FPC}
type
  AccDelegate = objcclass(NSObject)
    procedure accelerometer_didAccelerate(accelerometer: UIAccelerometer; acceleration: UIAcceleration); message 'accelerometer:didAccelerate:';
  end;
{$ENDIF}

{$IFDEF FPC}
var
  AccDelegatVar: AccDelegate;
{$ENDIF}

var
  MyAccelerometer: TiOSAccelerometer;

procedure Register;

implementation

{$IFDEF FPC}
procedure AccDelegate.accelerometer_didAccelerate(accelerometer: UIAccelerometer; acceleration: UIAcceleration);
begin
  if Assigned(MyAccelerometer) then
    if Assigned(MyAccelerometer.FOnAccelerate) then
      MyAccelerometer.FOnAccelerate(acceleration.x,acceleration.y);
end;
{$ENDIF}

constructor TiOSAccelerometer.Create(AOwner: TComponent);
begin
  inherited;
  {$IFDEF FPC}
  UIAccelerometer.sharedAccelerometer.setUpdateInterval(1.0 / kAccelerometerFrequency);
  AccDelegatVar := AccDelegate.alloc;
  UIAccelerometer.sharedAccelerometer.setDelegate(AccDelegatVar);
  {$ENDIF}
end;

procedure Register;
begin
  RegisterComponents('iOS', [TiOSAccelerometer]);
end;

end.
Johan
  • 74,508
  • 24
  • 191
  • 319