android toast 自启动,启用蓝牙时,Android语音到文本无法显示自定义toast消息

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 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();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值