Getting Pepper to say an incoming message

Posted 4 months ago by Cgma20

Post a topic
Solved
C
Cgma20

 Hello,


I am trying to get Pepper to say an incoming message that i send over a Bluetooth connection. I can currently send the message, receive the message on my pepper application and do something with that message such as write it to the logs. But i am currently facing the issue of getting pepper to say the message that i send. I have tried several different ways and the closest i have gotten is the current code which returns the error code "missing qiContext or speechengine"

 

package com.example.pepper_project;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.aldebaran.qi.Future;
import com.aldebaran.qi.sdk.QiContext;
import com.aldebaran.qi.sdk.QiSDK;
import com.aldebaran.qi.sdk.RobotLifecycleCallbacks;
import com.aldebaran.qi.sdk.builder.SayBuilder;
import com.aldebaran.qi.sdk.design.activity.RobotActivity;
import com.aldebaran.qi.sdk.design.activity.conversationstatus.SpeechBarDisplayStrategy;
import com.aldebaran.qi.sdk.object.conversation.Phrase;
import com.aldebaran.qi.sdk.object.conversation.Say;
import com.aldebaran.qi.sdk.object.conversation.SpeechEngine;
import com.aldebaran.qi.sdk.util.FutureUtils;

import java.util.concurrent.TimeUnit;


public class MainActivity2 extends RobotActivity implements RobotLifecycleCallbacks {

    private static final String TAG = "MainActivity";

    private QiContext qiContext;
    StringBuilder message;
    private Object SpeechEngine;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);


        setSpeechBarDisplayStrategy(SpeechBarDisplayStrategy.IMMERSIVE);
        message = new StringBuilder();

        LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, new IntentFilter("incomingMessage"));

        Button BTSettings = (Button) findViewById(R.id.BTSettings);

        BTSettings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent BTSettingsIntent = new Intent(MainActivity2.this, BluetoothSettings.class);
                startActivity(BTSettingsIntent);
            }
        });

    }

    BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String text = intent.getStringExtra("theMessage");

            message.append(text);
            Log.d(TAG, "onReceive: Message:" + message);
            Phrase phrase = new Phrase(message.toString());

            speak(phrase);
            message.delete(0,100);
        }
    };




    public void speak(Phrase phrase){
        QiSDK.register(this,this);
        this.qiContext = qiContext;

        FutureUtils.wait(0, TimeUnit.SECONDS).andThenConsume((ignore) -> {
            Say say = SayBuilder.with(qiContext)
                    .withPhrase(phrase)
                    .build();

            say.run();
            });

    }


    @Override
    public void onRobotFocusGained(QiContext qiContext) {

        
    }

    @Override
    public void onRobotFocusLost() {

    }

    @Override
    public void onRobotFocusRefused(String reason) {

    }

    @Override
    protected void onDestroy(){
        QiSDK.unregister(this,this);
        super.onDestroy();
    }
}

 

0 Votes


2 Comments

Sorted by
Lukas Brandt

Lukas Brandt posted 4 months ago

You should register the QiSDK in the onCreate Method. Once it register the QiSDK it will trigger the overwritten onRobotFocusGained Method, where you get your qiContext.
What you are doing in the speak() Method is registering the QiSDK that will call the onRobotFocusGained where nothing happens and then you set the Global qiContext that is null to itself. So it will be null.
You should set it in the onRobotFocusGained.
I changed your code to:

public class MainActivity2 extends RobotActivity implements RobotLifecycleCallbacks {

    private static final String TAG = "MainActivity";

    private QiContext qiContext;
    StringBuilder message;
    private Object SpeechEngine;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);


        setSpeechBarDisplayStrategy(SpeechBarDisplayStrategy.IMMERSIVE);
        message = new StringBuilder();

        LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, new IntentFilter("incomingMessage"));

        Button BTSettings = (Button) findViewById(R.id.BTSettings);

        BTSettings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent BTSettingsIntent = new Intent(MainActivity2.this, BluetoothSettings.class);
                startActivity(BTSettingsIntent);
            }
        });

        QiSDK.register(this,this);
    }

    BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String text = intent.getStringExtra("theMessage");

            message.append(text);
            Log.d(TAG, "onReceive: Message:" + message);
            Phrase phrase = new Phrase(message.toString());

            speak(phrase);
            message.delete(0,100);
        }
    };




    public void speak(Phrase phrase){
        FutureUtils.wait(0, TimeUnit.SECONDS).andThenConsume((ignore) -> {
            Say say = SayBuilder.with(qiContext)
                    .withPhrase(phrase)
                    .build();

            say.run();
            });

    }


    @Override
    public void onRobotFocusGained(QiContext qiContext) {
        this.qiContext = qiContext;
    }

    @Override
    public void onRobotFocusLost() {

    }

    @Override
    public void onRobotFocusRefused(String reason) {

    }

    @Override
    protected void onDestroy(){
        QiSDK.unregister(this,this);
        super.onDestroy();
    }
}



0 Votes

C

Cgma20 posted 4 months ago

Thank you so much! It had moved that around quite a bit to see if it worked but i guess i never got the magic combination :D It works now 

 

0 Votes

Login or Sign up to post a comment