Using sensors in the VCL in Delphi XE6

Posted by on in Blogs

One of the new features for the VCL XE6 is a availability of components for sensors.
Now, opening the Tool Palette in VCL project, you will see three new components on the Sensors tab. You may have already used them in your FM applications, then you can begin to use them and VCL. No changes, only one restriction - VCL applications only for windows. If you have a Windows tablet with sensors you can try to make your application more user-friendly and comfortable.

Two tips
- Setup on your tablet PAServer for windows. It make your work more comfortable.
- Try configure remote access to tablet. Usually on the tablet is preinstalled Windows without RDP, but you can use remote assistant, which greatly facilitates the development and debugging.
If you have never used the sensors in FM you should know that each component provides access to one sensor from the list (category). For example, TMotionSensor allows you to connect to the following sensors: accelerometer, motion detector, gyroscope, speedometer. It depends on the model of the tablet.

Let's begin!
I have a tablet which has a compass. What should I do to use it?
Put TOrientationSensor on the VCL form. Select Events tab in the Object Inspector and create an event OnSensorChoosing. This event allows you to define what we are going to use the sensor. The compass code can be as follows:

procedure TForm5.osCompassSensorChoosing(Sender: TObject; const Sensors: TSensorArray; var ChoseSensorIndex: Integer);
I : integer;
for I := 0 to Length(Sensors) - 1 do
if (Sensors[I] as TCustomOrientationSensor).SensorType = TOrientationSensorType.Compass3D then
ChoseSensorIndex := I;

You can see what I have called component osCompass.
Nothing complicated. Just looping through the array and returns the index of the first sensor, which is a compass.
We want to get the azimuth? Activate the sensor value and try get value:

osCompass.Active := True;
LAzimuth := osCompass.Sensor.CompMagHeading;

Or other property, that supported by the sensor. The set of supported properties stored in AvailableProperties


Then you can add TTimer and periodically poll the sensor or ask it if necessary when you need.


Check out more tips and tricks in this development video: