【问题标题】:Android Alljoyn: Can we connect two different devices with two different BusAttachmentsAndroid Alljoyn:我们可以用两个不同的 BusAttachments 连接两个不同的设备吗
【发布时间】:2016-03-01 12:31:31
【问题描述】:

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

我在这个职位上遇到了异常

aboutClient.startAboutClient(bus);

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

【问题讨论】:

    标签: android eclipse sensors iot alljoyn


    【解决方案1】:
    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] 
    

    【讨论】:

      猜你喜欢
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      • 2017-10-07
      • 2016-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-29
      相关资源
      最近更新 更多