How to discover available devices around us and put it into listview(Android)?

asked 2014-06-21 03:11:48 -0700

gumuruh gravatar image

updated 2014-06-23 18:46:14 -0700

Hi guys!

I'm glad that now we're already have this alljoyn library that help us to connect from one device to another.

Let's assume there are two Android devices implementing the example as its given from the AllJoyn library.

from the Android -> Sample Service and from the Android -> Sample Client

How to make the Client (discover) the another devices... and put its name found into the ListView?

From which line of code should I modify to obtain that purpose? After analyzing the source code of the samples, I think we shall modify the below part, is it true? If not, Please guide me. CMIIW.


    public void handleMessage(Message msg) {
        switch(msg.what) {
        /* Connect to a remote instance of an object implementing the SimpleInterface. */
        case CONNECT: {
             * All communication through AllJoyn begins with a BusAttachment.
             * A BusAttachment needs a name. The actual name is unimportant except for internal
             * security. As a default we use the class name as the name.
             * By default AllJoyn does not allow communication between devices (i.e. bus to bus
             * communication). The second argument must be set to Receive to allow communication
             * between devices.
            mBus = new BusAttachment(getPackageName(), BusAttachment.RemoteMessage.Receive);

             * Create a bus listener class
            mBus.registerBusListener(new BusListener() {
                public void foundAdvertisedName(String name, short transport, String namePrefix) {
                    logInfo(String.format("MyBusListener.foundAdvertisedName(%s, 0x%04x, %s)", name, transport, namePrefix));
                     * This client will only join the first service that it sees advertising
                     * the indicated well-known name.  If the program is already a member of 
                     * a session (i.e. connected to a service) we will not attempt to join 
                     * another session.
                     * It is possible to join multiple session however joining multiple 
                     * sessions is not shown in this sample. 
                    if(!mIsConnected) {
                        Message msg = obtainMessage(JOIN_SESSION);
                        msg.arg1 = transport;
                        msg.obj = name;

            /* To communicate with AllJoyn objects, we must connect the BusAttachment to the bus. */
            Status status = mBus.connect();
            logStatus("BusAttachment.connect()", status);
            if (Status.OK != status) {

             * Now find an instance of the AllJoyn object we want to call.  We start by looking for
             * a name, then connecting to the device that is advertising that name.
             * In this case, we are looking for the well-known SERVICE_NAME.
            status = mBus.findAdvertisedName(SERVICE_NAME);
            logStatus(String.format("BusAttachement.findAdvertisedName(%s)", SERVICE_NAME), status);
            if (Status.OK != status) {

        case (JOIN_SESSION): {
             * If discovery is currently being stopped don't join to any other sessions.
            if (mIsStoppingDiscovery) {

             * In order to join the session, we need to provide the well-known
             * contact port.  This is pre-arranged between both sides as part
             * of the definition of the chat service.  As a result of joining
             * the session, we get a session identifier which we must use to 
             * identify the created session communication channel whenever we
             * talk to the remote side.
            short contactPort = CONTACT_PORT;
            SessionOpts sessionOpts = new SessionOpts();
            sessionOpts.transports = (short)msg.arg1;
            Mutable.IntegerValue sessionId = new Mutable.IntegerValue();

            Status status = mBus.joinSession((String) msg.obj, contactPort, sessionId, sessionOpts, new SessionListener() {
                @Override ...
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2014-06-23 12:51:47 -0700

bspencer gravatar image

You will want to read up this section that explain details about the wellknown name. Next read the following sections on how to discover and advertise.

The Sample application you reference are designed to be paired with each other. IE one advertises the other discovers. There is no design for handling wellknown name collisions. IE if you have 2 service applications running then the client will connect to whichever one it discovers(FoundAdvertisedName callback) first. In order to make a modification to the sample the wellknown name on the service should change to include a random identifier or some way to distinguish it from the other devices (user input or device system value). Then the client side the FoundAdvertisedName method will provide you with the service name that was found. You can split off the prefix argument and would have something that you could then display in a list.

Another option is to use the AboutFeature and perform service level discovery/advertisement. Using About you find devices/applications with a list of interfaces that are implemented. You can then display the "Friendly Name" from the AboutData in a UI. Then use the port and busName to connect to when you wish to interact with the device/application.

edit flag offensive delete publish link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2014-06-21 03:11:48 -0700

Seen: 440 times

Last updated: Jun 23 '14