'How to initialize OutputStream
I am developing an app that controls arduino with bluetooth , everything was working fine with the app , I am able to connect with the arduino bluetooth bridge but when i click on the bottons that control the arduino my app crashes , crash log says it happens a Null Point Exception in OutputStream , but im new to this and im not sure how to initialize it , my OutputStream is supposed to transmit the commands ( written in the command string )to the arduino .My doubt is how to initialize the OutputStream? if the question is not clear or lacks information , please tell me .
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;
}
});
Solution 1:[1]
For developers with the same problem , the solution was already given to me , the line of code used to initialize "outputstream" and the source for the answer are written here .
Line of code :
outputStream = new ByteArrayOutputStream(1024);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | med benzekri |