当TOrientationSensor组件的Active属性设置为True时,将获取信息。
OrientationSensor1.Active := True;
在OnSensorChoosing事件中,指定要使用的传感器。
以下代码检查并选择可以获取设备倾斜度的传感器。
procedure TForm1.OrientationSensor1SensorChoosing(Sender: TObject;
const Sensors: TSensorArray; var ChoseSensorIndex: Integer);
var
I: Integer;
begin
for I := 0 to High(Sensors) do
begin
if TCustomOrientationSensor.TProperty.TiltX
in TCustomOrientationSensor(Sensors[I]).AvailableProperties then
begin
ChoseSensorIndex := I;
Exit;
end;
end;
ChoseSensorIndex := 0;
end;
以下代码检查并选择可以获取指南针航向的传感器。
procedure TForm1.OrientationSensor2SensorChoosing(Sender: TObject;
const Sensors: TSensorArray; var ChoseSensorIndex: Integer);
var
I: Integer;
begin
for I := 0 to High(Sensors) do
begin
if TCustomOrientationSensor.TProperty.HeadingX
in TCustomOrientationSensor(Sensors[I]).AvailableProperties then
begin
ChoseSensorIndex := I;
Exit;
end;
end;
ChoseSensorIndex := 0;
end;
在OnDataChanged事件中,获取传感器信息。
以下代码显示设备倾斜度。
procedure TForm1.OrientationSensor1DataChanged(Sender: TObject);
begin
LabelTiltX.Text := FloatToStr(OrientationSensor1.Sensor.TiltX);
LabelTiltY.Text := FloatToStr(OrientationSensor1.Sensor.TiltY);
LabelTiltZ.Text := FloatToStr(OrientationSensor1.Sensor.TiltZ);
end;
以下代码显示指南针的方向。
procedure TForm1.OrientationSensor2DataChanged(Sender: TObject);
begin
LabelHeadingX.Text := FloatToStr(OrientationSensor2.Sensor.HeadingX);
LabelHeadingY.Text := FloatToStr(OrientationSensor2.Sensor.HeadingY);
LabelHeadingZ.Text := FloatToStr(OrientationSensor2.Sensor.HeadingZ);
end;