Revision history [back]

click to hide/show revision 1
initial version

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

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? From the analyzing point after reading the source code, I think it started from below is it true? If now, Please guide me. CMIIW.

thanks

 @Override
    public void handleMessage(Message msg) {
        switch(msg.what) {
        /* Connect to a remote instance of an object implementing the SimpleInterface. */
        case CONNECT: {
            org.alljoyn.bus.alljoyn.DaemonInit.PrepareDaemon(getApplicationContext());
            /*
             * 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() {
                @Override
                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;
                        sendMessage(msg);
                    }
                }
            });

            /* 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) {
                finish();
                return;
            }

            /*
             * 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) {
                finish();
                return;
            }

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

            /*
             * 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
                public void sessionLost(int sessionId, int reason) {
                    mIsConnected = false;
                    logInfo(String.format("MyBusListener.sessionLost(sessionId = %d, reason = %d)", sessionId,reason));
                    mHandler.sendEmptyMessage(MESSAGE_START_PROGRESS_DIALOG);
                }
            });
            logStatus("BusAttachment.joinSession() - sessionId: " + sessionId.value, status);

            if (status == Status.OK) {
                /*
                 * To communicate with an AllJoyn object, we create a ProxyBusObject.  
                 * A ProxyBusObject is composed of a name, path, sessionID and interfaces.
                 * 
                 * This ProxyBusObject is located at the well-known SERVICE_NAME, under path
                 * "/SimpleService", uses sessionID of CONTACT_PORT, and implements the SimpleInterface.
                 */
                mProxyObj =  mBus.getProxyBusObject(SERVICE_NAME, 
                                                    "/SimpleService",
                                                    sessionId.value,
                                                    new Class<?>[] { SimpleInterface.class });

                /* We make calls to the methods of the AllJoyn object through one of its interfaces. */
                mSimpleInterface =  mProxyObj.getInterface(SimpleInterface.class);

                mSessionId = sessionId.value;
                mIsConnected = true;
                mHandler.sendEmptyMessage(MESSAGE_STOP_PROGRESS_DIALOG);
            }
            break;
        }

        /* Release all resources acquired in the connect. */
        case DISCONNECT: {
            mIsStoppingDiscovery = true;
            if (mIsConnected) {
                Status status = mBus.leaveSession(mSessionId);
                logStatus("BusAttachment.leaveSession()", status);
            }
            mBus.disconnect();
            getLooper().quit();
            break;
        }

        /*
         * Call the service's Ping method through the ProxyBusObject.
         *
         * This will also print the String that was sent to the service and the String that was
         * received from the service to the user interface.
         */
        case PING: {
            try {
                if (mSimpleInterface != null) {
                    sendUiMessage(MESSAGE_PING, msg.obj);
                    String reply = mSimpleInterface.Ping((String) msg.obj);
                    sendUiMessage(MESSAGE_PING_REPLY, reply);
                }
            } catch (BusException ex) {
                logException("SimpleInterface.Ping()", ex);
            }
            break;
        }
        default:
            break;
        }
    }

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

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? From the analyzing point after reading the source code, I think it started from below is it true? If now, Please guide me. CMIIW.

thanks

 @Override
    public void handleMessage(Message msg) {
        switch(msg.what) {
        /* Connect to a remote instance of an object implementing the SimpleInterface. */
        case CONNECT: {
            org.alljoyn.bus.alljoyn.DaemonInit.PrepareDaemon(getApplicationContext());
            /*
             * 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() {
                @Override
                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;
                        sendMessage(msg);
                    }
                }
            });

            /* 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) {
                finish();
                return;
            }

            /*
             * 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) {
                finish();
                return;
            }

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

            /*
             * 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
                public void sessionLost(int sessionId, int reason) {
                    mIsConnected = false;
                    logInfo(String.format("MyBusListener.sessionLost(sessionId = %d, reason = %d)", sessionId,reason));
                    mHandler.sendEmptyMessage(MESSAGE_START_PROGRESS_DIALOG);
                }
            });
            logStatus("BusAttachment.joinSession() - sessionId: " + sessionId.value, status);

            if (status == Status.OK) {
                /*
                 * To communicate with an AllJoyn object, we create a ProxyBusObject.  
                 * A ProxyBusObject is composed of a name, path, sessionID and interfaces.
                 * 
                 * This ProxyBusObject is located at the well-known SERVICE_NAME, under path
                 * "/SimpleService", uses sessionID of CONTACT_PORT, and implements the SimpleInterface.
                 */
                mProxyObj =  mBus.getProxyBusObject(SERVICE_NAME, 
                                                    "/SimpleService",
                                                    sessionId.value,
                                                    new Class<?>[] { SimpleInterface.class });

                /* We make calls to the methods of the AllJoyn object through one of its interfaces. */
                mSimpleInterface =  mProxyObj.getInterface(SimpleInterface.class);

                mSessionId = sessionId.value;
                mIsConnected = true;
                mHandler.sendEmptyMessage(MESSAGE_STOP_PROGRESS_DIALOG);
            }
            break;
        }

        /* Release all resources acquired in the connect. */
        case DISCONNECT: {
            mIsStoppingDiscovery = true;
            if (mIsConnected) {
                Status status = mBus.leaveSession(mSessionId);
                logStatus("BusAttachment.leaveSession()", status);
            }
            mBus.disconnect();
            getLooper().quit();
            break;
        }

        /*
         * Call the service's Ping method through the ProxyBusObject.
         *
         * This will also print the String that was sent to the service and the String that was
         * received from the service to the user interface.
         */
        case PING: {
            try {
                if (mSimpleInterface != null) {
                    sendUiMessage(MESSAGE_PING, msg.obj);
                    String reply = mSimpleInterface.Ping((String) msg.obj);
                    sendUiMessage(MESSAGE_PING_REPLY, reply);
                }
            } catch (BusException ex) {
                logException("SimpleInterface.Ping()", ex);
            }
            break;
        }
        default:
            break;
        }
    }

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

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? From the analyzing point after reading the source code, I think it started from below is it true? If now, no, Please guide me. CMIIW.

thanks

 @Override
    public void handleMessage(Message msg) {
        switch(msg.what) {
        /* Connect to a remote instance of an object implementing the SimpleInterface. */
        case CONNECT: {
            org.alljoyn.bus.alljoyn.DaemonInit.PrepareDaemon(getApplicationContext());
            /*
             * 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() {
                @Override
                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;
                        sendMessage(msg);
                    }
                }
            });

            /* 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) {
                finish();
                return;
            }

            /*
             * 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) {
                finish();
                return;
            }

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

            /*
             * 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
                public void sessionLost(int sessionId, int reason) {
                    mIsConnected = false;
                    logInfo(String.format("MyBusListener.sessionLost(sessionId = %d, reason = %d)", sessionId,reason));
                    mHandler.sendEmptyMessage(MESSAGE_START_PROGRESS_DIALOG);
                }
            });
            logStatus("BusAttachment.joinSession() - sessionId: " + sessionId.value, status);

            if (status == Status.OK) {
                /*
                 * To communicate with an AllJoyn object, we create a ProxyBusObject.  
                 * A ProxyBusObject is composed of a name, path, sessionID and interfaces.
                 * 
                 * This ProxyBusObject is located at the well-known SERVICE_NAME, under path
                 * "/SimpleService", uses sessionID of CONTACT_PORT, and implements the SimpleInterface.
                 */
                mProxyObj =  mBus.getProxyBusObject(SERVICE_NAME, 
                                                    "/SimpleService",
                                                    sessionId.value,
                                                    new Class<?>[] { SimpleInterface.class });

                /* We make calls to the methods of the AllJoyn object through one of its interfaces. */
                mSimpleInterface =  mProxyObj.getInterface(SimpleInterface.class);

                mSessionId = sessionId.value;
                mIsConnected = true;
                mHandler.sendEmptyMessage(MESSAGE_STOP_PROGRESS_DIALOG);
            }
            break;
        }

        /* Release all resources acquired in the connect. */
        case DISCONNECT: {
            mIsStoppingDiscovery = true;
            if (mIsConnected) {
                Status status = mBus.leaveSession(mSessionId);
                logStatus("BusAttachment.leaveSession()", status);
            }
            mBus.disconnect();
            getLooper().quit();
            break;
        }

        /*
         * Call the service's Ping method through the ProxyBusObject.
         *
         * This will also print the String that was sent to the service and the String that was
         * received from the service to the user interface.
         */
        case PING: {
            try {
                if (mSimpleInterface != null) {
                    sendUiMessage(MESSAGE_PING, msg.obj);
                    String reply = mSimpleInterface.Ping((String) msg.obj);
                    sendUiMessage(MESSAGE_PING_REPLY, reply);
                }
            } catch (BusException ex) {
                logException("SimpleInterface.Ping()", ex);
            }
            break;
        }
        default:
            break;
        }
    }

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

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? From the After analyzing point after reading the source code, code of the samples, I think it started from we shall modify the below part, is it true? If no, not, Please guide me. CMIIW.

thanks

 @Override
    public void handleMessage(Message msg) {
        switch(msg.what) {
        /* Connect to a remote instance of an object implementing the SimpleInterface. */
        case CONNECT: {
            org.alljoyn.bus.alljoyn.DaemonInit.PrepareDaemon(getApplicationContext());
            /*
             * 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() {
                @Override
                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;
                        sendMessage(msg);
                    }
                }
            });

            /* 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) {
                finish();
                return;
            }

            /*
             * 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) {
                finish();
                return;
            }

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

            /*
             * 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
                public void sessionLost(int sessionId, int reason) {
                    mIsConnected = false;
                    logInfo(String.format("MyBusListener.sessionLost(sessionId = %d, reason = %d)", sessionId,reason));
                    mHandler.sendEmptyMessage(MESSAGE_START_PROGRESS_DIALOG);
                }
            });
            logStatus("BusAttachment.joinSession() - sessionId: " + sessionId.value, status);

            if (status == Status.OK) {
                /*
                 * To communicate with an AllJoyn object, we create a ProxyBusObject.  
                 * A ProxyBusObject is composed of a name, path, sessionID and interfaces.
                 * 
                 * This ProxyBusObject is located at the well-known SERVICE_NAME, under path
                 * "/SimpleService", uses sessionID of CONTACT_PORT, and implements the SimpleInterface.
                 */
                mProxyObj =  mBus.getProxyBusObject(SERVICE_NAME, 
                                                    "/SimpleService",
                                                    sessionId.value,
                                                    new Class<?>[] { SimpleInterface.class });

                /* We make calls to the methods of the AllJoyn object through one of its interfaces. */
                mSimpleInterface =  mProxyObj.getInterface(SimpleInterface.class);

                mSessionId = sessionId.value;
                mIsConnected = true;
                mHandler.sendEmptyMessage(MESSAGE_STOP_PROGRESS_DIALOG);
            }
            break;
        }

        /* Release all resources acquired in the connect. */
        case DISCONNECT: {
            mIsStoppingDiscovery = true;
            if (mIsConnected) {
                Status status = mBus.leaveSession(mSessionId);
                logStatus("BusAttachment.leaveSession()", status);
            }
            mBus.disconnect();
            getLooper().quit();
            break;
        }

        /*
         * Call the service's Ping method through the ProxyBusObject.
         *
         * This will also print the String that was sent to the service and the String that was
         * received from the service to the user interface.
         */
        case PING: {
            try {
                if (mSimpleInterface != null) {
                    sendUiMessage(MESSAGE_PING, msg.obj);
                    String reply = mSimpleInterface.Ping((String) msg.obj);
                    sendUiMessage(MESSAGE_PING_REPLY, reply);
                }
            } catch (BusException ex) {
                logException("SimpleInterface.Ping()", ex);
            }
            break;
        }
        default:
            break;
        }
    }