FireMonkeyアプリケーションでTOrientationSensorのSensor.TiltXがNaNを返すときの対処法

TOrientationSensorコンポーネントを使うと、デバイスの傾き、距離、コンパス方位に関する情報を取得できます。

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;

Sensor.TiltXプロパティの値がNaNになるときは、TOrientationSensorコンポーネントの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;
      Label1.Text := IntToStr(I);
      Exit;
    end;
  end;
  ChoseSensorIndex := 0;
end;

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください