iOS ExternalAccessory框架 - "外部异常0"开场时

时间:2015-01-20 05:12:38

标签: delphi firemonkey delphi-xe7

我需要在iOS上连接蓝牙设备(通过MFi认证)。不幸的是,Embarcadero没有将外部附件框架头文件导入Delphi单元,但我想也许我可以看看iOSapi.Foundation.pas并自己进行转换。外部附件框架由7个头文件组成:

  • EAAccessory.h
  • EAAccessoryManager.h
  • EASession.h
  • EAWiFiUnconfiguredAccessory.h
  • EAWiFiUnconfiguredAccessoryBrowser.h
  • ExternalAccessory.h
  • ExternalAccessoryDefines.h

由于我的设备使用蓝牙,因此我没有导入WiFi头文件。

这是我的uEAAccessoryManager.pasuEAAccessory.pasuEASession.pas

配件经理:

unit uEAAccessoryManager;

interface

uses
  Macapi.ObjectiveC,
  iOSapi.Foundation;

type
  EABluetoothAccessoryPickerCompletion = procedure(error: Pointer) of object;

type
  EAAccessoryManager = interface(NSObject)
    ['{C7D9DF32-44D2-4190-8202-801318FF57D9}']
    procedure showBluetoothAccessoryPickerWithNameFilter(predicate: NSPredicate; completion: EABluetoothAccessoryPickerCompletion); cdecl;
    procedure registerForLocalNotifications; cdecl;
    procedure unregisterForLocalNotifications; cdecl;
    function connectedAccessories: NSArray; cdecl;
  end;

  EAAccessoryManagerClass = interface(NSObjectClass)
    ['{015D15E4-6CDF-4117-A398-EC1DB11C9AEA}']
    function sharedAccessoryManager: Pointer; cdecl;
  end;

  TEAAccessoryManager = class(TOCGenericImport<EAAccessoryManagerClass, EAAccessoryManager>)
  end;

implementation

end.

附件

unit uEAAccessory;

interface

uses
  Macapi.ObjectiveC,
  iOSapi.Foundation,
  iOSapi.CocoaTypes;

type
  EAAccessory = interface(NSObject)
    ['{1DF56B17-AE6A-4CDC-A8F5-DB35F52535BE}']
    function connected: Boolean; cdecl;
    function connectionID: NSUInteger; cdecl;
    function manufacturer: NSString; cdecl;
    function name: NSString; cdecl;
    function modelNumber: NSString; cdecl;
    function serialNumber: NSString; cdecl;
    function firmwareRevision: NSString; cdecl;
    function hardwareRevision: NSString; cdecl;
    function protocolStrings: NSArray; cdecl;
    function id: Pointer; cdecl;
    procedure setID(id: Pointer); cdecl;
  end;

  EAAccessoryClass = interface(NSObjectClass)
    ['{AF265097-4A5A-4055-8B04-56A438354EA7}']
  end;

  TEAAccessory = class(TOCGenericImport<EAAccessoryClass, EAAccessory>)
  end;

  EAAccessoryDelegate = interface(IObjectiveC)
    ['{CF8586FA-20B1-4D1F-96AE-F96A3BC70556}']
    procedure accessoryDidDisconnect(accessory: EAAccessory); cdecl;
  end;

implementation

end.

会话:

unit uEASession;

interface

uses
  Macapi.ObjectiveC,
  iOSapi.Foundation,
  iOSapi.CocoaTypes;

type
  EASession = interface(NSObject)
    ['{25D62FB5-EB6F-47BD-86D9-D31017D99900}']
    function accessory: Pointer; cdecl;
    function protocolString: NSString; cdecl;
    function inputStream: NSInputStream; cdecl;
    function outputStream: NSOutputStream; cdecl;
    function initWithAccessory(accessory: Pointer; protocolString: Pointer): Pointer; cdecl;
  end;

  EASessionClass = interface(NSObjectClass)
    ['{CA3F3621-E688-43BE-83CF-5580045E378C}']
  end;

  TEASession = class(TOCGenericImport<EASessionClass, EASession>)
  end;

implementation

end.

我可以获取已连接设备的列表,并获取有关设备的信息:

procedure TfrmMain.ListPairedDevices;
var
  i: Integer;
  FPairedDevices: NSArray;
begin
  lBDevices.Clear; // TListBox

  FPairedDevices := TEAAccessoryManager.Wrap(TEAAccessoryManager.OCClass.sharedAccessoryManager).connectedAccessories;
  for i := 0 to FPairedDevices.count - 1 do
    lBDevices.Items.Add(NSStrToStr(TEAAccessory.Wrap(FPairedDevices.objectAtIndex(i)).name));
end;

但是当我想打开一个会话时,我会得到&#34;外部异常0&#34;在我的iPhone 4上拨打initWithAccessory()。这是我的代码:

procedure TfrmMain.SendData;
var
  p, p1, p2: Pointer;
  b: array of Byte;
begin
  p := TEAAccessoryManager.Wrap(TEAAccessoryManager.OCClass.sharedAccessoryManager).connectedAccessories.objectAtIndex(lBDevices.ItemIndex);
  AEAAccessory := TEAAccessory.Wrap(p);
  p1 := (AEAAccessory as ILocalObject).GetObjectID;
  p2 := (StrToNSStr('MFi.protocol.name.here.can.not.disclose.due.to.NDA') as ILocalObject).GetObjectID;
  p := TEASession.Alloc.initWithAccessory(p1, p2); // EXCEPTION HERE
  AEASession := TEASession.Wrap(p);
  SetLength(b, 1);
  b[0] := 5;
  AEASession.outputStream.write(@b[0], 1);
  Sleep(100);
  AEASession.inputStream.read(@b[0], 1);
  if b[0] = 6 then
    ShowMessage('ACK received!');
end;

那么,我的EASession.h翻译有什么问题吗?

0 个答案:

没有答案