1
votes

I use this XMPP aSmack - How can I get the current user state (offline/online/away/etc.)?
Code for to connect with ejabbered and login is

Class.forName("org.jivesoftware.smack.ReconnectionManager");
System.setProperty("smack.debugEnabled", "true");
ConnectionConfiguration connConfig = new ConnectionConfiguration(
        XmppUtils.HOST, XmppUtils.PORT, XmppUtils.SERVICE);
connConfig.setSecurityMode(SecurityMode.disabled);
connConfig.setSendPresence(true);
XMPPConnection   connection = new XMPPConnection(connConfig);
SASLAuthentication.supportSASLMechanism("PLAIN");
connConfig.setSASLAuthenticationEnabled(true);
connection.connect();                   
connection.login(Fblogin.fb_id, XmppUtils.PASSWORD);
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
setConnection(connection);

//Check the user onlcine or offline
Presence userFromServer=connection.getRoster().getPresence(TOCHATUSERNAME+"/Smack");
final int userState = XmppUtils.retrieveState(userFromServer.getMode(), userFromServer.isAvailable());

// Set the state
getActivity().runOnUiThread(new Runnable() {
     @Override
     public void run() {
         if (userState==0) {
                txt_membersgname.setText("offline");
         } else if (userState==1) {
                txt_membersgname.setText("online");
         } else if (userState==2) {
                txt_membersgname.setText("Busy");
         }
    }
});


public static int retrieveState(Mode userMode, boolean isOnline) {
    /** 0 for offline, 1 for online, 2 for away,3 for busy*/
    int userState = 0; // default return value
    if (userMode == Mode.dnd) {
        Log.e("busy", "busy");
        userState = 3;
    } else if (userMode == Mode.away || userMode == Mode.xa) {
        userState =2;
    } else if (isOnline) {
        Log.e("online", "online");
        userState = 1;
    }
    return userState;
}

I always get Presence unavailable but if i use below code it works correct

Presence userFromServer=connection.getRoster().getPresence(connection.getUser());

what am i doing wrong so how can i get other users Presence ?

1

1 Answers

1
votes

You can get the availability of the user by using user id of a particular user

Presence availability = roster.getPresence(user);

here : user means user id