Receive String via Bluetooth and print in a text box

Asked

Viewed 84 times

1

I have a Java code that I adapted to receive a string via bluetooth and when running it in Android Studio does not present errors. However, in the app by clicking the "Start" button to go to Main2activity (Where the code in question is), it shows the error "App X stopped".

Below is the code, in case someone can help me or has another example that I can use it, thank you.

Note: The bluetooth connection is working smoothly.

package com.hlp.bluetoothfinal;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main2Activity extends AppCompatActivity {

    private EditText textView;
    private EditText textView2;

    private static final int MESSAGE_READ = 3;

    ConnectedThread connectedThread;

    Handler mHandler;
    StringBuilder dadosBluetooth = new StringBuilder( );



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

        textView = (EditText) findViewById( R.id.textView );

        textView2 = (EditText) findViewById( R.id.textView2 );

        mHandler = new Handler(  ){ // Função

            @Override
            public void handleMessage(@NonNull Message msg) { //metodo

                if(msg.what == MESSAGE_READ) { //

                    String recebidos = (String) msg.obj; // receber dado

                    dadosBluetooth.append(recebidos); // juntar dados

                    int fimInformacao = dadosBluetooth.indexOf("}"); //vendo se a informação está completa

                    if(fimInformacao > 0){ // chegou o final

                        String dadosCompletos = dadosBluetooth.substring(0, fimInformacao ); //dadosCompletos, verifica se tem o inicio { e o fim }

                        int tamInformacao = dadosCompletos.length(); // capturando o tamanho inteiro da informação

                        if(dadosBluetooth.charAt(0) == '{') {  // se a posição zero for igual { então

                            String dadosFinais = dadosBluetooth.substring(1, tamInformacao); // a dados finais recebe desde a posição 1 até o tamanho da informação

                            //Log.d("Recebidos", dadosFinais);

                            textView.setText(dadosFinais);
                        }

                        dadosBluetooth.delete(0, dadosBluetooth.length());
                    }
                }
            }

        };
  • You can’t tell what the problem is. We need more information, for example which versions of the SDK the code was compiled and executed on, emulated and tested device models, emulator bug and alert reports, and device bug and alert reports.

1 answer

0

I know this way of receiving data but you were not clear on the following points: - How do you know if the app is working before you arrive at Activity where the code with Handler is,

  • If the Activity that has the start button has something beyond this button.

I will suggest what should be done and based on your feedback I will edit my reply I hope not be warned by moderation.

1º. Connect the device in developer mode and click run to install.ja during the installation press the ALT+6 keys to open the logcat. In the logcat window select the error option(follow print)

inserir a descrição da imagem aqui

now every error that occurs will be shown on that screen.

Once you install click START on your app to make the error occur. As this Voce goes into the logcat and checks where the error was marked. The indication will be in blue and contains information of the name of the Activity and the line responsible for the error and the name of the error.

With this procedure you can make the correction.

The probable cause is a nullPointException or in Activity that contains the START button or Activity that is with the connection thread because even if the IDE does not red-flag error, so-called warnings that are the yellow markings may bring some kind of problem but not always what may be your case.

I believe the error is in a deletion that has to be made in mHandler because the way it is can cause memory leakage, but I await the answer on the error before instructing you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.