在 Delphi 中使用第三方Android API

vbkedwbf  于 2023-02-04  发布在  Android
关注(0)|答案(1)|浏览(279)

我正在尝试使用Delphi Seattle中的Honeywell SDK来控制Android设备上的扫描仪。
Honeywell为我提供了一个名为DataCollection.jar的SDK jar。我使用Java2OP实用程序来创建我的JNI桥,但遇到了一些麻烦。老实说,我是这方面的新手,不确定在哪里寻找或如何实现它。我也搜索过类似的示例。
我知道我必须创建一个TJAidmanager类型的对象来访问条形码阅读器函数,它被定义为:

JAidcManagerClass = interface(JObjectClass)
['{EE70EF80-4CA8-4FE0-B912-6453CA0336CD}']
{class} function _GetBARCODE_DEVICE_CONNECTED: Integer; cdecl;
{class} function _GetBARCODE_DEVICE_DISCONNECTED: Integer; cdecl;
{class} procedure addBarcodeDeviceListener(P1:      JAidcManager_BarcodeDeviceListener); cdecl;
{class} procedure close; cdecl;
{class} procedure create(P1: JContext; P2: JAidcManager_CreatedCallback);    cdecl;
{class} function createBarcodeReader: JBarcodeReader; cdecl; overload;
{class} function createBarcodeReader(P1: JString): JBarcodeReader; cdecl;     overload;
{class} function listBarcodeDevices: JList; cdecl;
{class} function listConnectedBarcodeDevices: JList; cdecl;
{class} procedure removeBarcodeDeviceListener(P1:    JAidcManager_BarcodeDeviceListener); cdecl;
{class} property BARCODE_DEVICE_CONNECTED: Integer read     _GetBARCODE_DEVICE_CONNECTED;
{class} property BARCODE_DEVICE_DISCONNECTED: Integer read   _GetBARCODE_DEVICE_DISCONNECTED;
end;

[JavaSignature('com/honeywell/aidc/AidcManager')]
JAidcManager = interface(JObject)
['{98A668C5-9204-4F0C-98B0-A8E1F1124738}']
end;
TJAidcManager = class(TJavaGenericImport<JAidcManagerClass, JAidcManager>)    end;

java示例代码如下所示:

import com.honeywell.aidc.*;
public class MainActivity extends Activity {
private AidcManager manager;
private BarcodeReader reader;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// create the AidcManager providing a Context and an
// CreatedCallback implementation.
AidcManager.create(this, new AidcManager.CreatedCallback() {

@Override
public void onCreated(AidcManager aidcManager) {
manager = aidcManager;
reader = manager.createBarcodeReader();
}
}
}

@Override
public void onDestroy() {
super.onDestroy();
// make sure that AidcManager isn't null
if (reader != null) {
// close BarcodeReader to clean up resources.
// once closed, the object can no longer be used.
reader.close();
}
if (manager != null) {
// close AidcManager to disconnect from the scanner service.
// once closed, the object can no longer be used.
manager.close();
}
}
}

现在当我尝试在Delphi中使用它时,我已经尝试过了,但是得到了一个分段错误:

var cb : JAidcManager_CreatedCallback;
begin
      TJAidcManager.JavaClass.create(SharedActivity,TJAidcManager_CreatedCallback.Wrap((cb as ILocalObject).getobjectID));

JAidcManager_CreatedCallback的定义如下:

JAidcManager_CreatedCallbackClass = interface(IJavaClass)
    ['{E27EF6BB-C642-42B6-98CA-6C270A1AD5D1}']
    {class} procedure onCreated(P1: JAidcManager); cdecl;//Deprecated
  end;

  [JavaSignature('com/honeywell/aidc/AidcManager$CreatedCallback')]
  JAidcManager_CreatedCallback = interface(IJavaInstance)
    ['{E2713D70-1CE6-43AA-9181-7054BD787F1B}']
  end;
  TJAidcManager_CreatedCallback =   class(TJavaGenericImport<JAidcManager_CreatedCallbackClass,   JAidcManager_CreatedCallback>) end;

我假设我必须为TJAidcManager类型的变量实现onCreated事件,但我不确定如何将它们链接在一起。
然后我试了这个:

var   cb : JObject;
begin
  cb := TjObject.Create;
  TJAidcManager.JavaClass.create(SharedActivityContext,TJAidcManager_CreatedCallback.Wrap((cb as ILocalObject).getobjectID));

我还在实现部分的JNIBridge单元中添加了以下内容:

procedure onCreated(P1: JAidcManager); cdecl;//Deprecated
begin
   Form2.manager := P1;
end;

当运行这个函数时,我会得到一个"java.lang.SecurityException:不允许绑定到服务意图{acct = com. honeywell. decode. DecodeService cmp = com. intermec. datacollectionservice/. DataCollectionService} "错误。我尝试将"绑定远程视图"添加到权限,但没有帮助。
如果有人能就如何在Delphi中创建AidcManager向我提供建议或指出正确的方向,我将不胜感激。

ckx4rj1h

ckx4rj1h1#

这个问题已经解决了,我需要扩展com.emarcadero.firemonkey.FMXNativeActivity。
有关完整解决方案的详细信息,请访问Embaradero Forum with link to Source

相关问题