Android Studio, problema de configuração do Bluetooth
Então, eu sou um novato no Android ainda, mas estou trabalhando em um aplicativo que basicamente quero que ele se comunique com um arduino através de bluetooth, mas o ponto aqui é que, quando estou tentando configurar o código bluetooth, recebo um erro no logcat, que é:
java.lang.NullPointerException: tentativa de chamar o método virtual 'java.io.OutputStream android.bluetooth.BluetoothSocket.getOutputStream ()' em uma referência de objeto nulo "
, e isso realmente está me causando problemas há uma semana e não consigo encontrar uma limitação, tenho certeza de que isso não é tão complicado para um programador experiente, então eu realmente aprecio se alguém me ajudou a corrigir isso.
Aqui está o meu código:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
{
TextView txtArduino, txtString, txtStringLength, sensorView0;
Handler bluetoothIn;
final int handlerState = 0; //used to identify handler message
private BluetoothAdapter btAdapter=null;
private BluetoothSocket btSocket = null;
private StringBuilder recDataString = new StringBuilder();
private ConnectedThread mConnectedThread;
// SPP UUID service - this should work for most devices
private static final UUID BTMODULEUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
// String for MAC address
private static String address = null;
btAdapter = BluetoothAdapter.getDefaultAdapter(); // get Bluetooth adapter
checkBTState();
public void onResume() {
super.onResume();
//Get MAC address from device_list via intent
Intent intent = getIntent();
//Get the MAC address from the DeviceListActivty via EXTRA
address = intent.getStringExtra(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
//create device and set the MAC address
//Log.i("ramiro", " FE:85:EE:7F:E7:6B : " + address);
if(address != null) {
BluetoothDevice device = btAdapter.getRemoteDevice(address);
try {
btSocket = createBluetoothSocket(device);
} catch (IOException e) {
Toast.makeText(getBaseContext(), "La creacción del Socket fallo", Toast.LENGTH_LONG).show();
}
// Establish the Bluetooth socket connection.
try
{
btSocket.connect();
} catch (IOException e) {
try
{
btSocket.close();
} catch (IOException e2)
{
//insert code to deal with this
}
}}
mConnectedThread = new ConnectedThread(btSocket);
mConnectedThread.start();
//I send a character when resuming.beginning transmission to check device is connected
//If it is not an exception will be thrown in the write method and finish() will be called
mConnectedThread.write("x");
}
e aqui é onde o problema deveria estar:
private class ConnectedThread extends Thread {
private final InputStream mmInStream;
private final OutputStream mmOutStream;
//creation of the connect thread
public ConnectedThread(BluetoothSocket socket) {
InputStream tmpIn=null;
OutputStream tmpOut = null;
try {
//Create I/O streams for connection
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
private BluetoothSocket createBluetoothSocket (dispositivo BluetoothDevice) lança IOException {
return device.createRfcommSocketToServiceRecord(BTMODULEUUID);
//creates secure outgoing connecetion with BT device using UUID
}
}
https://i.stack.imgur.com/X4dY4.png aqui está o meu logcat depois de adicionar e.printstacktrace ()