Android Studio, problema de configuración de Bluetooth
Así que todavía soy un novato en Android, pero he estado trabajando en una aplicación que básicamente quiero que se comunique con un arduino a través de bluetooth, pero el punto aquí es que cuando intento configurar el código de bluetooth, recibo un error en el logcat, que es:
java.lang.NullPointerException: intento de invocar el método virtual 'java.io.OutputStream android.bluetooth.BluetoothSocket.getOutputStream ()' en una referencia de objeto nulo "
, y esto realmente me ha estado dando problemas durante una semana y no puedo encontrar una solución, estoy seguro de que esto no es tan complicado para un programador experimentado, así que realmente agradecería si alguien me ayudó a solucionarlo.
Aquí está mi 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");
}
y aquí es donde se supone que está el problema:
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;
}
BluetoothSocket privado createBluetoothSocket (dispositivo BluetoothDevice) lanza IOException {
return device.createRfcommSocketToServiceRecord(BTMODULEUUID);
//creates secure outgoing connecetion with BT device using UUID
}
}
https://i.stack.imgur.com/X4dY4.png aquí está mi logcat después de agregar e.printstacktrace ()