-2
我正在開發一款應用程序,它可以控制arduino和藍牙,一切都可以正常工作,我可以連接到arduino藍牙橋,但是當我點擊控制arduino的botton時,我的應用程序崩潰了,崩潰日誌說它在OutputStream中發生空點異常,但即時通訊新的這一點,我不知道如何初始化它,我的OutputStream應該傳輸命令(寫入命令字符串)到arduino。我懷疑是如何初始化OutputStream?如果問題不清楚或缺乏信息,請告訴我。如何初始化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;
}
});
https://developer.android.com/reference/java/io/OutputStream.html – Shark
嘗試一些像'outputStream = new ByteArrayOutputStream(1024; // size' – Shark
應用不再崩潰,非常感謝。 –