0

I am trying to write a code where it will be able to send bluetooth signal to arduino when a specific speech to text is being detected. Speech to text is able to display custom toasts when bluetooth is disabled. Unfortunately, when bluetooth is enabled, speech to text is not able to display custom toasts (it cannot detect the specific text). There might be a logic error in my code. Please guys I need help. Thank you.

public void speechRecognition(View view) {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
    try {
        startActivityForResult(intent, RESULT_SPEECH);

    } catch (ActivityNotFoundException a) {
        getCustomToast("Oops! Your Device Doesn't Support Speech to Text!");
    }
}

public void turnBluetoothOn(View view) {
    if(!myBluetoothAdapter.isEnabled()) {
        Intent turnOnIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(turnOnIntent, REQUEST_ENABLE_BT);
        getCustomToast("Bluetooth Has Been Turned On");
    }
    else {
        getCustomToast("Bluetooth Is Already On");
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(requestCode) {
        case REQUEST_ENABLE_BT: {
            if(myBluetoothAdapter.isEnabled()) {
                displayIndicator.setText("Status: Bluetooth Enabled");

            }
            else {
                displayIndicator.setText("Status: Bluetooth Disabled");
            }
            break;
        }
        default: {
            if (resultCode == RESULT_OK && null != data) {

                ArrayList<String> text = data
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

                displayIndicator.setText(text.get(0));

                if("grab".equals(displayIndicator.getText().toString())) {
                    getCustomToast("Grabbing Object");
                    mConnectedThread.write("G");
                }
                else if("release".equals(displayIndicator.getText().toString())) {
                    getCustomToast("Releasing Object");
                    mConnectedThread.write("Z");
                }
                else {
                    getCustomToast("Speech Error: Please RETRY!");
                }   
            }
        }
    }   
}

@Override
public void onResume() {
     super.onResume();
     Log.d(TAG, "...onResume - try connect...");
     // Set up a pointer to the remote node using it's address.
     BluetoothDevice device = myBluetoothAdapter.getRemoteDevice(robotBluetoothAddress);

     // Two things are needed to make a connection:
     // A MAC address, which we got above.
     // A Service ID or UUID.  In this case we are using the
     // UUID for SPP.
     try {
            myBluetoothSocket = createBluetoothSocket(device);

        } catch (IOException e) {
            errorExit("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + ".");
        }

        // Discovery is resource intensive.  Make sure it isn't going on
        // when you attempt to connect and pass your message.
        myBluetoothAdapter.cancelDiscovery();

        // Establish the connection.  This will block until it connects.
        Log.d(TAG, "...Connecting...");
        try {
             myBluetoothSocket.connect();
          Log.d(TAG, "....Connection ok...");
        } catch (IOException e) {
          try {
             myBluetoothSocket.close();
          } catch (IOException e2) {
            errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
          }
        }

        // Create a data stream so we can talk to server.
        Log.d(TAG, "...Create Socket...");

        mConnectedThread = new ConnectedThread( myBluetoothSocket);
        mConnectedThread.start();
}


@Override
public void onPause() {
    super.onPause();
    Log.d(TAG, "...In onPause()...");
    /*
    try {
        myBluetoothSocket.close();
    } catch (IOException e2) {
        errorExit("Fatal Error", "In onPause() and failed to close socket." + e2.getMessage() + ".");
    }
    */
}


@Override
protected void onDestroy() {
    if(D) Log.e(TAG, "+++ ON DESTROY +++");
    super.onDestroy();
    //unregisterReceiver(bReceiver);
}

public void getCustomToast(String message)
    {
        LayoutInflater inflater = getLayoutInflater();
        View layout  = inflater.inflate(R.layout.activity_custom_toast, (ViewGroup)     findViewById(R.id.toast_layout));
        TextView text = (TextView) layout.findViewById(R.id.toast_text_1);
        text.setText(message);
        Toast customToast = new Toast(this);
        customToast.setDuration(Toast.LENGTH_SHORT);
        customToast.setView(layout);
        customToast.show();
    }
4

0 回答 0