I want to code a motion detector (Accelerometer) using the AndroidApi directly. I currently have tested implementations (per Embarcadero examples) using TMotionSensor as well as TSensorManager, but both seem to have power drain issues (ie. the phone gets hot).
My TSensorManager implementation looks like this:
procedure TfrmTabbed.InitSensorMan;
var
FSensors: TSensorArray;
Sensor: TCustomSensor;
begin
TSensorManager.Current.Activate;
FSensors := TSensorManager.Current.GetSensorsByCategory(TSensorCategory.Motion);
FSensor := nil;
for Sensor in FSensors do
begin
if TCustomMotionSensor(Sensor).SensorType = TMotionSensorType.Accelerometer3D then
begin
FSensor := TCustomMotionSensor(Sensor);
Break;
end;
end;
MotionTimer.Interval := 250;
MotionTimer.Active := True;
end;
So now, using How to detect movement of an android device? as a reference, I start writing code like this:
uses
Androidapi.Sensor,
Androidapi.JNI.JavaTypes;
{$R *.fmx}
procedure TForm2.FormCreate(Sender: TObject);
var
Obj: JObject;
SensorManager: JSensorManager;
begin
Obj := TAndroidHelper.Context.getSystemService(TJContext.JavaClass.SENSOR_SERVICE);
if Obj <> nil then
begin
SensorManager := TJsensorManager.Wrap(Obj);
end;
I guess anyone who is familiar with this area, will realize that there's no JSensorManager
declared anywhere in C:\Program Files (x86)\Embarcadero\Studio\18.0\source
. There's a Androidapi.JNI.Telephony.pas
, but no Androidapi.JNI.Sensor(s).pas
!
My question is, is it possible to access the SENSOR_SERVICE from Delphi in this way, and if so, how can I implement it?
Addendum
I tried Java2op. It seems to require a very specific version (1.7.25?) of the JDK to not produce a "class or interface expected" error. So I tried Java2Pas instead. The free version only parses Android.jar, but that seems to be sufficient for my purposes.