0

我正在尝试使用 alljoyn 库创建一个 iOS 应用程序。但是,当我尝试通过调用快速创建总线附件时,在运行时出现错误busAttachment = AJNBusAttachment.init(applicationName: "BusName", allowRemoteMessages: true); EXC_BAD_ACCESS

这是我用来创建和使用总线附件的类

class HostController: UIViewController {

var hostApi: HostApi!

var busAttachment: AJNBusAttachment!

var sessionOpts: AJNSessionOptions!

var CONTACT_PORT: AJNSessionPort = 12345

var serviceName: String = "org.company.appname.HelloWorld"


override func viewDidLoad() {
    super.viewDidLoad();
    NSLog("HostController viewDidLoad");


    var status: QStatus!

    busAttachment = AJNBusAttachment(applicationName: "BusName", allowRemoteMessages: true);

    busAttachment.registerBusListener(BusListener());


    hostApi = HostApi.init(busAttachment: busAttachment, onPath: "/HostService");

    status = busAttachment.start();

    if (status != ER_OK) {
        NSLog("busAttachment.start, Status: %s", QCC_StatusText(status));
    }

    status = busAttachment.registerBusObject(hostApi);

    if (status != ER_OK) {
        NSLog("busAttachment.registerBusObject, Status: %s", QCC_StatusText(status));
    }


    status = busAttachment.connectWithArguments(nil);

    if (status != ER_OK) {
        NSLog("busAttachment.connectWithArguments, Status: %s", QCC_StatusText(status));
    }


    status = busAttachment.requestWellKnownName(serviceName, withFlags: kAJNBusNameFlagReplaceExisting | kAJNBusNameFlagDoNotQueue)

    if (status != ER_OK) {
        NSLog("busAttachment.requestWellKnownName, Status: %s", QCC_StatusText(status));
    }


    sessionOpts = AJNSessionOptions.init(trafficType: kAJNTrafficMessages, supportsMultipoint: true, proximity: kAJNProximityAny, transportMask: kAJNTransportMaskTCP);



    status = busAttachment.bindSessionOnPort(CONTACT_PORT, withOptions: sessionOpts, withDelegate: nil);

    if (status != ER_OK) {
        NSLog("busAttachment.bindSessionOnPort, Status: %s", QCC_StatusText(status));
    }


    status = busAttachment.advertiseName(serviceName, withTransportMask: sessionOpts.transports);

    if (status != ER_OK) {
        NSLog("busAttachment.advertiseName, Status: %s", QCC_StatusText(status));
    }


    NSLog("Done");
}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning();
    // Dispose of any resources that can be recreated.
}


class BusListener: NSObject, AJNBusListener {}

class SessionPortListener: NSObject, AJNSessionPortListener {

    func shouldAcceptSessionJoinerNamed(joiner: String!, onSessionPort sessionPort: AJNSessionPort, withSessionOptions options: AJNSessionOptions!) -> Bool {
        if (sessionPort == 12345) {
            NSLog("New peer joining session: %s", joiner);
            return true;
        } else {
            return false;
        }
    }

}

}

我可能会遗漏一件事,在 java 上我们必须先调用org.alljoyn.bus.alljoyn.DaemonInit.PrepareDaemon(context);,然后才能使用总线附件,但是在为 iOS 提供的示例中似乎没有等效调用。也许我只是错过了它并且它被放在了其他地方,或者也许在 iOS 上没有同等的东西。

4

1 回答 1

1

与 Java 一样,您需要先初始化 AllJoyn,然后才能使用它。为了解决这个问题,我添加#import "AJNInit.h"了我的桥接头并将它添加到我的 swift 中以初始化 AllJoyn。

AJNInit.alljoynInit();
AJNInit.alljoynRouterInit();

您还应该调用这些方法来释放 AllJoyn 使用的资源。

AJNInit.alljoynShutdown();
AJNInit.alljoynRouterShutdown();
于 2016-03-01T23:40:04.303 回答