Android Studio, Bluetooth configuration issue

2020-03-31 08:08发布

问题:

So im a newbie in android still, but ive been working on an app that basically i want it to communicate with an arduino through bluetooth, but the point here is that when im trying to configure the bluetooth code, i get thrown an error in the logcat, which is:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.OutputStream android.bluetooth.BluetoothSocket.getOutputStream()' on a null object reference"

, and this has really been giving me problems for a week and i cant find a slution, im sure this is not that complicated for an experienced programmer, so id really appreciate if someone helped me fix this.

Here is my code:

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");
}

and here is where the problem is supposed to be:

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(BluetoothDevice device) throws IOException {

    return  device.createRfcommSocketToServiceRecord(BTMODULEUUID);
    //creates secure outgoing connecetion with BT device using UUID
}

}

https://i.stack.imgur.com/X4dY4.png here is my logcat after adding e.printstacktrace()

https://i.stack.imgur.com/X4dY4.png