Android Alljoyn:我们可以使用两个不同的BusAttachments连接两个不同的设备

时间:2016-03-01 12:31:32

标签: android eclipse sensor iot alljoyn

我一直在尝试使用两个不同的BusAttachment对象连接两个不同的设备,但是我收到了BusAlreadyExistException?

我在这个位置得到例外

aboutClient.startAboutClient(bus);

我尝试了很多方法但找不到解决方法。我该如何解决这个问题。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

I don't know your complete code,so I just give some examples as below,hope you can get something on it.

server:
int main(int argc, char** argv, char** envArg)
{
    signal(SIGINT, SigIntHandler);

    QStatus status = ER_OK;

    s_msgBus = new BusAttachment("myApp", true);

    InterfaceDescription* testIntf = NULL;
    s_msgBus->CreateInterface(INTERFACE_NAME, testIntf);

    testIntf->AddMethod("cat", "ss",  "s", "inStr1,inStr2,outStr", 0);

    testIntf->Activate();

s_msgBus->RegisterBusListener(s_busListener);

s_msgBus->Start();

    BasicSampleObject testObj;

    testObj.AddInterface(*testIntf);

    const MethodEntry methodEntries[] = {
            { exampleIntf->GetMember("cat"), static_cast<MessageReceiver::MethodHandler>(&BasicSampleObject::Cat) }
        };

    testObj.AddMethodHandlers(methodEntries, sizeof(methodEntries) / sizeof(methodEntries[0]));

    s_msgBus->RegisterBusObject(testObj);

    /*
        link to router,if exist,connect to it and bundled router
        then recieve and send message
    */
    s_msbBus->Connect();

/*
    start AdvertiseName,three steps:
    1、request well-known name
    2、creat session
    3、AdvertiseName
*/
    const uint32_t flags = DBUS_NAME_FLAG_REPLACE_EXISTING | DBUS_NAME_FLAG_DO_NOT_QUEUE;
    s_msgBus->RequestName(SERVICE_NAME, flags);

    const TransportMask SERVICE_TRANSPORT_TYPE = TRANSPORT_ANY;
    SessionOpts opts(SessionOpts::TRAFFIC_MESSAGES, false, SessionOpts::PROXIMITY_ANY, SERVICE_TRANSPORT_TYPE);
    SessionPort sp = SERVICE_PORT;

    QStatus status = s_msgBus->BindSessionPort(sp, opts, s_busListener);

    s_msgBus->AdvertiseName(SERVICE_NAME, mask);

    WaitForSigInt();

    delete s_msgBus;
    s_msgBus = NULL;

    printf("Basic service exiting with status 0x%04x (%s).\n", status, QCC_StatusText(status));

    return (int) status;
}

client:
int main(int argc, char** argv, char** envArg)
{
    signal(SIGINT, SigIntHandler);
QStatus status = ER_OK;
g_msgBus = new BusAttachment("myApp", true);
g_msgBus->CreateInterface(INTERFACE_NAME, testIntf);
testIntf->AddMethod("cat", "ss",  "s", "inStr1,inStr2,outStr", 0);
testIntf->Activate();

g_msgBus->Start();
g_msgBus->Connect();
static MyBusListener s_busListener;
g_msgBus->RegisterBusListener(s_busListener);
//same as the server

g_msgBus->FindAdvertisedName(SERVICE_NAME);

WaitForJoinSessionCompletion();

ProxyBusObject remoteObj(*g_msgBus, SERVICE_NAME, SERVICE_PATH, s_sessionId);
const InterfaceDescription* alljoynTestIntf = g_msgBus->GetInterface(INTERFACE_NAME);

assert(alljoynTestIntf);

remoteObj.AddInterface(*alljoynTestIntf);

Message reply(*g_msgBus);
MsgArg inputs[2];

inputs[0].Set("s", "Hello ");
inputs[1].Set("s", "World!");

QStatus status = remoteObj.MethodCall(SERVICE_NAME, "cat", inputs, 2, reply, 5000);

if (ER_OK == status) {
    printf("'%s.%s' (path='%s') returned '%s'.\n", SERVICE_NAME, "cat",
           SERVICE_PATH, reply->GetArg(0)->v_string.str);

delete g_msgBus;
g_msgBus = NULL;

printf("Basic client exiting with status 0x%04x (%s).\n", status, QCC_StatusText(status));

return (int) status;
}
result:
[server][1]
[client][1]