程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> Delphi獲取Android下GPS的NMEA 0183數據,nmea0183

Delphi獲取Android下GPS的NMEA 0183數據,nmea0183

編輯:Delphi

Delphi獲取Android下GPS的NMEA 0183數據,nmea0183


下面的程序,可以實現Android下獲取GNSS的NMEA0183數據:

unit utAndroidNmea;

interface

uses Androidapi.JNIBridge, Androidapi.JNI.App, Androidapi.NativeActivity, Androidapi.JNI.JavaTypes, Androidapi.JNI.Location;

type
  TonNmeaReceived=procedure(timestamp: Int64; nmea: String) of Object;

  TJGpsStatus_NmeaListener = class(TJavaGenericImport<JGpsStatus_NmeaListenerClass, JGpsStatus_NmeaListener>) end;

  TNmeaProvider=class(TJavaLocal,JGpsStatus_NmeaListenerClass, JGpsStatus_NmeaListener)
  protected
    FLocationManager:JLocationManager;
    FOnNmeaReceived:TonNmeaReceived;
  public
    procedure onNmeaReceived(timestamp: Int64; nmea: JString); cdecl;
  public
    constructor Create;
    destructor Destroy;override;
    function Run:Boolean;
    property OnNmeaLineReceived:TOnNmeaReceived read FOnNmeaReceived write FOnNmeaReceived;
  end;



implementation

uses Androidapi.Helpers, Androidapi.JNI.GraphicsContentViewText,  FMX.Helpers.Android,
FMX.Platform.Android, System.SysUtils, System.Android.Sensors;

{ TNmea }

constructor TNmeaProvider.Create;
begin
  inherited Create;
end;

destructor TNmeaProvider.Destroy;
begin
  if Assigned(FLocationManager) then FLocationManager.removeNmeaListener(Self );
  inherited;
end;

procedure TNmeaProvider.onNmeaReceived(timestamp: Int64; nmea: JString); cdecl;
begin
  if Assigned(FOnNmeaReceived) then FOnNmeaReceived(timestamp, JStringToString(nmea));
end;


function TNmeaProvider.Run:Boolean;
begin
  CallInUiThread(procedure
  var
    LocationService: JObject;
  begin
    LocationService := SharedActivityContext.getSystemService(TJContext.JavaClass.LOCATION_SERVICE);
    FLocationManager := TJLocationManager.Wrap((LocationService as ILocalObject).GetObjectID);
    FLocationManager.addNmeaListener(Self);
  end);

end;

end.

 

用法:

在Form中添加一個TLocationSensor, 設置Active:=True;

然後定義OnNmeaReceive方法:

procedure TForm1.OnNmeaReceived(timestamp: int64; nmeasentence: String);
begin
  FStream.Write(PChar(nmeasentence)^, Length(nmeasentence)*sizeof(Char));

end;

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved