如何初始化OutputStream

时间:2016-12-27 13:34:15

标签: java android nullpointerexception bluetooth arduino

我正在开发一个用蓝牙控制arduino的应用程序,一切都运行良好的应用程序,我能够连接到arduino蓝牙桥,但当我点击控制arduino我的应用程序崩溃的底部,崩溃日志说它在OutputStream中发生了Null Point Exception,但是我对此并不知道如何初始化它,我的OutputStream应该将命令(在命令字符串中写入)传递给arduino。我怀疑如何初始化OutputStream ?如果问题不明确或缺乏信息,请告诉我。

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;



public class MainActivity extends AppCompatActivity {

    Button  forward_btn,forward_left_btn , forward_right_btn , reverse_btn , reverse_right_btn , reverse_left_btn , btnConexao ;
    public static final int SOLICITA_ATIVACAO = 1; // é o codigo numero 1 , é diferenciado o codigo pq pode haver varias solicitações na msma tela
    public static final int SOLICITA_CONEXAO = 2;

    BluetoothAdapter meuBluetoothAdapter=null; //declarar o meu adptador bluetooth
    BluetoothDevice meuDevice=null;
    BluetoothSocket meuSocket=null;// faz transiçao dos dados



    boolean conexao =  false; //variavel para a conexao que indica se a conexao está em andamento ou nao

    private OutputStream outputStream  ;

    private static String MAC = null;
    String command; //string variable that will store value to be transmitted to the bluetooth module

    UUID MEU_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); // canal de comunicaçao bluetooth , UUID- ID do canal
//protocolo Rfcomm



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

        forward_btn = (Button) findViewById(R.id.forward_btn); //definir botoes e associar ao ficheiro xml
        forward_left_btn = (Button) findViewById(R.id.forward_left_btn);
        forward_right_btn = (Button) findViewById(R.id.forward_right_btn);
        reverse_btn = (Button) findViewById(R.id.reverse_btn);
        reverse_left_btn = (Button) findViewById(R.id.reverse_left_btn);
        reverse_right_btn = (Button) findViewById(R.id.reverse_right_btn);
        btnConexao = (Button) findViewById(R.id.btnConexao);




        forward_btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            // comando para o botao frente , forward button
            public boolean onTouch(View v, MotionEvent event) {   // comandos bluetooth correspondentes do arduino

                if (event.getAction() == MotionEvent.ACTION_DOWN) //MotionEvent.ACTION_DOWN é quando botao é segurado
                {
                    command = "1";

                    try
                    {
                        outputStream.write(command.getBytes()); //transmite o valor do comando para o modulo bluetooth
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                    return true;
                }
                else if(event.getAction() == MotionEvent.ACTION_UP)
                {
                    command = "10";
                    try
                    {
                        outputStream.write(command.getBytes());
                    }
                    catch(IOException e)
                    {
                        e.printStackTrace();
                    }

                }

                return false;
            }

        });

        //OnTouchListener para o botao recuar ( reverse button) (pressionar alongado)
        reverse_btn.setOnTouchListener(new View.OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                if(event.getAction() == MotionEvent.ACTION_DOWN)
                {
                    command = "2";

                    try
                    {
                        outputStream.write(command.getBytes());
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
                else if(event.getAction() == MotionEvent.ACTION_UP)
                {
                    command = "10";
                    try
                    {
                        outputStream.write(command.getBytes());
                    }
                    catch(IOException e)
                    {

                    }

                }
                return false;
            }
        });

1 个答案:

答案 0 :(得分:1)

对于有相同问题的开发人员,已经给出了解决方案,用于初始化“outputstream”的代码行和答案的来源如下:

来源: https://developer.android.com/reference/java/io/OutputStream.html

代码行: outputStream = new ByteArrayOutputStream(1024; // size