11

I have developed application for two different sensors. They are working fine separately but when I try to use them togather and create two diffent buses than Alljoyn gives this exception.

org.alljoyn.services.common.BusAlreadyExistException: The object has been set previously with a BusAttachment.

Below is my source code for connection. Can anyone tell me why I'm having this issue.

private void connect() 
        {           org.alljoyn.bus.alljoyn.DaemonInit.PrepareDaemon(getApplicationContext());

            bus = new BusAttachment("ControlPanelBrowser", BusAttachment.RemoteMessage.Receive);
            bus.registerBusListener(new BusListener());


            Status status = bus.registerBusObject(mControlPanelSignalInterface, Constants.SERVICE_PATH);


            if (status != Status.OK) {

                Log.d(TAG, "Problem while registering bus object");

            }   

            SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
            srpPassword = settings.getString(PREFS_PASSWORD, DEFAULT_SECURED_SRP_PASSWORD);

            SrpAnonymousKeyListener authListener = new SrpAnonymousKeyListener(this, logger, AUTH_MECHANISMS);
            Status authStatus = bus.registerAuthListener(authListener.getAuthMechanismsAsString(), 
                    authListener, getKeyStoreFileName());

            if ( authStatus != Status.OK ) {
                Log.e(TAG, "Failed to register AuthListener");
            }

            status = bus.connect();


            if (Status.OK == status){

                String daemonName = Constants.DAEMON_NAME_PREFIX + ".ControlPanelBrowser.G" + 
                        bus.getGlobalGUIDString();

                int flag = BusAttachment.ALLJOYN_REQUESTNAME_FLAG_DO_NOT_QUEUE;

                Status reqStatus = bus.requestName(daemonName, flag);

                if (reqStatus == Status.OK) {

                    Status adStatus = bus.advertiseName(Constants.DAEMON_QUIET_PREFIX +
                            daemonName, SessionOpts.TRANSPORT_ANY);

                    if (adStatus != Status.OK){
                        bus.releaseName(daemonName);
                        Log.e(TAG, "Failed to advertise daemon name: '" + daemonName + "', Error: '" + status + "'");
                    }
                    else{
                        Log.d(TAG, "Succefully advertised daemon name: '" + daemonName + "'");
                    }
                }
                else {
                    Log.e(TAG, "Failed to request daemon name: '" + daemonName + "', Error: '" + status + "'");
                }
            }


            status = bus.registerSignalHandlers(mControlPanelSignalInterface);

            if (status != Status.OK) {
                Log.d(TAG, "Problem while registering signal handlers");
            }

            // Initialize AboutService

            aboutClient = AboutServiceImpl.getInstance();
            aboutClient.setLogger(logger);
            try {
                aboutClient.startAboutClient(bus);

                for (String iface :  ANNOUNCE_IFACES) {


                    aboutClient.addAnnouncementHandler(this, new String[] {iface});

                }
            } catch (Exception e) {

                logger.error(TAG, "Unable to start AboutService, Error: " + e.getMessage());

            }


        }
AVI
  • 5,516
  • 5
  • 29
  • 38
Robin Royal
  • 1,788
  • 1
  • 18
  • 29
  • you should call `registerBusObject` twice (once for each sensor) and creating only one single bus attachment – Lino Feb 26 '16 at 13:54
  • I had tried this but no success. Now I'm creating two different busAttachment object in two different classes but same error is occurring. Now how can I solve this issue? – Robin Royal Feb 29 '16 at 07:02
  • which is the line that throws the `BusAlreadyExistException`? – Lino Feb 29 '16 at 15:51
  • @Lino I'm finding issue at this link. aboutClient.startAboutClient(bus); – Robin Royal Mar 01 '16 at 05:56

2 Answers2

1

use registerBusObject twince and then you can make one signle bus attachment

0

why dont you create two Interfaces, one interface for one sensor respectively. then add these two interfaces in a class which implements these two interfaces and the busObject and register an implemntation of this class as a BusObject.

For example

Sensor1_interface.java and Sensor2_interface.java //are my two interface classes

create a new class Sensor_InterfaceList which inplements the two interfaces and the BusObject

class Sensor_InterfaceList implements Sensor1_interface,Sensor2_interface,BusObject
   {
    // implment your interfaces here
    .....
    }
private Sensor_InterfaceList mySensor_InterfaceList;
mySensor_InterfaceList = new Sensor_InterfaceList();
myBus.registerBusObject(mySensor_InterfaceList,"/your/path");

This should solve your problem :)

Ajit Jain
  • 105
  • 6
  • or you can also add two objects on the same bus but on different paths.example `myBus.registerBusObject(mySensor1,"/my/path1"); myBus.registerBusObject(mySensor2,"/my/path2");` – Ajit Jain Mar 09 '16 at 16:14