android - Delphi XE6 和 Android(estimote beacon sdk) ClassCastException

标签 android delphi estimote

这件事我已经做了好几天了,一点运气都没有。这是 java2pasBeaconManager$ServiceReadyCallback 提供的:

  [JavaSignature('com/estimote/sdk/BeaconManager$ServiceReadyCallback')]
  JBeaconManager_ServiceReadyCallback = interface(JObject)
    ['{335D3A89-7137-44CE-9969-BECC2F3AB8AC}']
    procedure onServiceReady ; cdecl;                                           // ()V A: $401
  end;

由此我创建了如下匿名方法:

TAnServiceReadyCallBack = reference to function : JBeaconManager_ServiceReadyCallback;

下面我怎么调用它:

var
  an : TAnServiceReadyCallBack;
  bm : JBeaconManager;
begin
bm := TJBeaconManager.JavaClass.init(SharedActivityContext);
an := function : JBeaconManager_ServiceReadyCallback
      begin
         Result := TJBeaconManager_ServiceReadyCallback.Wrap((context as ILocalObject).GetObjectID);
      if Assigned(Result) then
         Memo1.Lines.Add('servicereadycallback init')
      else
        Memo1.Lines.Add('servicereadycallback not init')
end;
bm.connect(an); 

在那个 bm.connect(an) 调用中,我收到如下消息:

java.lang.ClassCastException: com.embarcadero.firemonkey.FMXNativeActivity cannot be cast to com.estimote.sdk.BeaconManager$ServiceReadyCallback

这是根据 estimote sdk 的内容:

   beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
     @Override public void onServiceReady() {
       try {
         beaconManager.startRanging(ALL_ESTIMOTE_BEACONS);
       } catch (RemoteException e) {
         Log.e(TAG, "Cannot start ranging", e);
       }
     }
   });

beacon sdk连接方法代码如下:

  public void connect(ServiceReadyCallback callback)
  {
    if (!checkPermissionsAndService()) {
      L.e("AndroidManifest.xml does not contain android.permission.BLUETOOTH or android.permission.BLUETOOTH_ADMIN permissions. BeaconService may be also not declared in AndroidManifest.xml.");
    }
    this.callback = ((ServiceReadyCallback)Preconditions.checkNotNull(callback, "callback cannot be null"));
    if (isConnectedToService()) {
      callback.onServiceReady();
    }
    boolean bound = this.context.bindService(new Intent(this.context, BeaconService.class), this.serviceConnection, 1);
    if (!bound) {
      L.w("Could not bind service: make sure thatcom.estimote.sdk.service.BeaconService is declared in AndroidManifest.xml");
    }
  }

我还尝试制作一个 delphi 类,如下所示:

  TServiceReadyCallback = class(TJavaLocal,
    JBeaconManager_ServiceReadyCallback)
  private
    fParent: TForm4;
  public
    constructor Create(parent: TForm4; context: JContext);
    function equals(o: JObject): boolean; cdecl;
    function getClass: Jlang_Class; cdecl;
    function hashCode: Integer; cdecl;
    procedure notify; cdecl;
    procedure notifyAll; cdecl;
    function toString: JString; cdecl;
    procedure wait; overload; cdecl;
    procedure wait(millis: Int64); overload; cdecl;
    procedure wait(millis: Int64; nanos: Integer); overload; cdecl;
    procedure onServiceReady; cdecl;
  end;

constructor TServiceReadyCallback.Create(parent: TForm4;
context: JContext);
begin
  fParent := parent;
  fParent.Memo1.Lines.Add('ServiceReadyCallback Created');
end;

使用这个类我使用如下:

var
  fservice: TServiceReadyCallback;
  context: JContext; 
  bm : JBeaconManager; 
begin
  CallInUIThreadAndWaitFinishing(
  procedure
  begin
  context := SharedActivityContext;
  bm := TJBeaconManager.JavaClass.init(context); 
  fservice := TServiceReadyCallback.Create(Self, context);
  bm.connect(fservice);
  end);
end;

在使用它时,我从 beaconmanager.java 收到 callback cannot be null'

this.callback = ((ServiceReadyCallback)Preconditions.checkNotNull(callback, "callback cannot be null"));

我非常需要这个,因为我的客户给我施加压力。我已经研究了至少 3-4 个月,但没有任何运气。这里有人可以帮我解决这个问题吗?

最佳答案

您还没有提供 JNI Bridge 包装器的完整定义,应该如下所示:

  JBeaconManager_ServiceReadyCallbackClass = interface(JObjectClass)
    ['{D1DC624A-9431-4724-A0B6-2FE039BDCC33}']
    procedure onServiceReady ; cdecl;
  end;

  [JavaSignature('com/estimote/sdk/BeaconManager_ServiceReadyCallback')]
  JBeaconManager_ServiceReadyCallback = interface(JObject)
    ['{40394705-FCFB-442E-B3C7-3EA9825CDDBF}']
    procedure onServiceReady ; cdecl;
  end;

  TJBeaconManager_ServiceReadyCallback = class(TJavaGenericImport<JBeaconManager_ServiceReadyCallbackClass, JBeaconManager_ServiceReadyCallback>)
  end;

关于android - Delphi XE6 和 Android(estimote beacon sdk) ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25310213/

相关文章:

android - 当互联网未连接时,Retrofit 会不断重试

delphi - 与字符串列表比较

ios - Estimote Beacon 加速度计更新时间

ios - 为什么 ESTBeaconManager 符合 CLLocationManagerDelegate

android - 通过删除前一个 fragment 来替换当前 fragment

java - Android Realm : No results when added in singleton

Android:与充当主机的 USB 设备通信

Delphi Printer.Canvas.TextWidth 属性

delphi - 如何在 Delphi 备忘录上进行 Unicode 修饰

android - 如何使用 estimote beacon android 在没有 UUID 的情况下开始测距?