libqi-java helper
package com.ghostwan.collaborativedialog;
import android.content.*;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import com.aldebaran.qi.*;
import com.aldebaran.robotservice.IRobotService;
import java.util.concurrent.ExecutionException;
/**
* Created 10/02/2017.
*/
public class QiHelper {
private String TAG = DCService.TAG;
private static final String ACTION_ROBOT_SERVICE = "com.aldebaran.action.ROBOT_SERVICE";
private static final String ROBOT_SERVICE_PACKAGE = "com.aldebaran.robotservice";
private static final String USER = "tablet";
private Session session;
private IRobotService robotService;
private Context context;
private SessionListener sessionListener;
public QiHelper(Context context, SessionListener sessionListener) {
this.context = context;
this.sessionListener = sessionListener;
}
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
robotService = IRobotService.Stub.asInterface(service);
try {
connectionSessionForPrivateSD();
} catch (RemoteException e) {
Log.e(TAG, "error :", e);
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
robotService = null;
}
};
private void connectSession(String endPoint, String token) {
if(session != null) {
session.close();
}
session = new Session();
session.setClientAuthenticator(new UserTokenAuthenticator(USER, token));
session.connect(endPoint).andThen(new QiCallback<Void>() {
@Override
public void onResult(Void aVoid) {
try {
onSessionConnected(session);
} catch (ExecutionException e) {
Log.e(TAG, "error :", e);
}
}
}, FutureCallbackType.Async);
}
private void connectionSessionForPrivateSD() throws RemoteException {
connectSession(robotService.getPrivateEndpoint(), robotService.getPrivateToken());
}
public void start() {
Intent intent = new Intent(ACTION_ROBOT_SERVICE);
intent.setPackage(ROBOT_SERVICE_PACKAGE);
context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}
public void stop() {
context.unbindService(serviceConnection);
session.close();
}
// These methods are called in another thread, due to FutureCallbackType.Async
// Don't call this method on the main thread ( onCreate, onStart, onResume...)
private void onSessionConnected(Session session) throws ExecutionException {
sessionListener.onSessionConnected(session);
}
}