Skip to content

Listing the available sensors using RAD Studio XE3 and FireMonkey 2

RAD Studio XE3 includes new classes for identifying, managing and using any sensors that are available on your target Windows and Mac computers. In this blog post I show you how to list all of the sensors that are available on a computer (you can do this using Delphi and C++Builder). Delphi XE3 and C++Builder XE3 include the new the new System.Sensors unit.

From the unit, the categories of sensors (TSensorCategory) are:

  • Location
  • Environmental
  • Motion
  • Orientation
  • Mechanical
  • Electrical
  • Biometric
  • Light
  • Scanner

For each sensor category the unit defines (currently) the following possible sensor types:

  TLocationSensorType = (GPS, Static, Lookup, Triangulation, Broadcast, DeadReckoning, Other);
  TEnvironmentalSensorType = (Temperature, AtmosphericPressure, Humidity, WindSpeed, WindDirection);
  TMotionSensorType = (Accelerometer1D, Accelerometer2D, Accelerometer3D, MotionDetector, Gyrometer1D, Gyrometer2D, Gyrometer3D, Speedometer);
  TOrientationSensorType = (Compass1D, Compass2D, Compass3D, Inclinometer1D, Inclinometer2D, Inclinometer3D, Distance1D, Distance2D, Distance3D);
  TElectricalSensorType = (Voltage, Current, Capacitance, Resistance, Inductance, ElectricalPower, Potentiometer);
  TMechanicalSensorType = (BooleanSwitch, BooleanSwitchArray, MultiValueSwitch, Force, Scale, Pressure, Strain);
  TBiometricSensorType = (HumanPresence, HumanProximity, Touch);
  TLightSensorType = (AmbientLight);
  TScannerSensorType = (RFID, Barcode);

The following code will get the number of sensors on a computer (you first have to activate the sensors) and displays the names of the sensors and their category:

interface
uses
  System.SysUtils, System.Types, System.UITypes, System.Rtti, System.Classes,
  System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.Layouts,
  FMX.Memo, FMX.Media, System.Sensors;

type
  TForm3 = class(TForm)
    Button2: TButton;
    Label2: TLabel;
    Memo2: TMemo;
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
    NumberOfSensors : integer;
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

{$R *.fmx}
uses System.TypInfo;

procedure TForm3.Button2Click(Sender: TObject);
var
  i : integer;
begin
  // get list of found sensors - if any
  TSensorManager.Current.Active := true;
  NumberofSensors := TSensorManager.Current.Count;
  Label2.Text := 'Sensors: '+IntToStr(NumberOfSensors);
  Memo2.Lines.Clear;
  for i := 0 to NumberOfSensors-1 do begin
    Memo2.Lines.Add(
      IntToStr(i)
      + ': '
      + TSensorManager.Current.Sensors[i].Name
      + '", Category: '
      + GetEnumName(System.TypeInfo(TSensorCategory),
            Ord(TSensorManager.Current.Sensors[i].Category))
    );
  end;
  TSensorManager.Current.Active := false;
end;

On my Samsung Slate 7 computer running Windows 8 Professional, the Delphi XE3 code will find several hardware and software based sensors including the following:

FireMonkey 2 also includes two new non-visual components, TLocationSensor and TMotionSensor, that you can drag and drop onto your forms. With these components and sensor-based code you can build FireMonkey 2 applications that take advantage of hardware in currently available notebooks/slates and have your applications ready for future FireMonkey mobile device support  (see the RAD Studio Mobile Roadmap at http://edn.embarcadero.com/article/42544).

{ 3 } Comments

  1. normnet | September 27, 2012 at 6:27 pm | Permalink

    Are these sensors typically USB?
    Would the sensor manufacturer’s proprietary drivers also be required?
    Is their an example of communication with one of these devices?

  2. pascalbbs | September 28, 2012 at 3:15 am | Permalink

    good!

  3. Phil Grainger | September 30, 2012 at 5:55 pm | Permalink

    Although it is not surprising given the direction RAD Studio is heading, towards support of the mobile market that is, it is none the less a very interesting extension. Add device control and you could almost be entering an embedded market.

Bad Behavior has blocked 4 access attempts in the last 7 days.

Close