Nick S.
Nick S.

Reputation: 21

Sending byte from Android to Arduino over Bluetooth causes application to crash

my name is Nick and I'm new to developing Android apps. I'm trying to develop a simple app to open and close an electric door lock. My current goal is to create an app with just 3 buttons: a button to connect to the Bluetooth module (HC-06), a button to unlock the door and a button to lock the door. This app should send a 1 or a 0 to the Arduino via Bluetooth, which will then lock or unlock the door. Note that in this stage of the project, the electric lock is replaced by an LED. So far I've managed to create a button that connects to the Bluetooth module, if you press one of the other buttons (openDoor & closeDoor) the application will stop working.

My question is: What should I change in the code in order to get it to work, instead of crashing the application?

Code of the application:

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;




public class MainActivity extends Activity implements OnClickListener{

TextView myLabel;
BluetoothAdapter btAdapter;
BluetoothDevice mDevice;
Set<BluetoothDevice> devicesArray;
Button button;
OutputStream mmOutStream;
BluetoothDevice mmDevice;
BluetoothSocket mmSocket;
InputStream mmInputStream;
OutputStream mmOutputStream;
Thread workerThread;
byte one;
byte zero;
byte[] readBuffer;
int readBufferPosition;
volatile boolean stopWorker;

public void openDoor(View view)
{
    Toast.makeText(this, "Door is unlocked", Toast.LENGTH_SHORT).show();
    try
    {
    unlockDoor();
    }
    catch (IOException ex){}                
}

public void closeDoor (View view)
{
    Toast.makeText(this, "Door is locked", Toast.LENGTH_SHORT).show();
    try
    {
    lockDoor();
    }
    catch (IOException ex){}
}

public void onMyButtonClick(View view)
{
    try
    {
        findBT();
        openBT();
    }
    catch (IOException ex){}   
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btAdapter = BluetoothAdapter.getDefaultAdapter();
    if (btAdapter == null) {
    // Device does not support Bluetooth
    }
    if (!btAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, 0);
        }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
}

void findBT()
{
    btAdapter = BluetoothAdapter.getDefaultAdapter();
    if(btAdapter == null)
    {
        Toast.makeText(this, "No Bluetooth adapter available", Toast.LENGTH_SHORT).show();
    }

    if(!btAdapter.isEnabled())
    {
        Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBluetooth, 0);
    }

    Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
    if(pairedDevices.size() > 0)
    {
        for(BluetoothDevice device : pairedDevices)
        {
            if(device.getName().equals("HC-06")) 
            {
                mmDevice = device;
                break;
            }
        }
    }
    Toast.makeText(this, "Bluetooth device found", Toast.LENGTH_SHORT).show();
}
void openBT() throws IOException
{
    UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard      SerialPortService ID
    mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);        
    mmSocket.connect();
    mmOutputStream = mmSocket.getOutputStream();
    mmInputStream = mmSocket.getInputStream();



    Toast.makeText(this, "Bluetooth connection established", Toast.LENGTH_SHORT).show();
}


 private void unlockDoor() throws IOException {
     one = 1;
     mmOutStream.write(one);
     mmOutStream.flush();
     mmOutStream.close(); 



 }
 private void lockDoor() throws IOException {
     zero = 0;
     mmOutStream.write(zero);
     mmOutStream.flush();
     mmOutStream.close();
 }
}

LogCat:

01-02 19:22:11.970: I/Process(1855): Sending signal. PID: 1855 SIG: 9
01-02 19:22:15.438: D/OpenGLRenderer(2964): Render dirty regions requested: true
01-02 19:22:15.445: D/Atlas(2964): Validating map...
01-02 19:22:15.512: I/Adreno-EGL(2964): <qeglDrvAPI_eglInitialize:410>: QUALCOMM Build: 10/24/14,      167c270, I68fa98814b
01-02 19:22:15.513: I/OpenGLRenderer(2964): Initialized EGL, version 1.4
01-02 19:22:15.526: D/OpenGLRenderer(2964): Enabling debug mode 0
01-02 19:22:17.176: W/BluetoothAdapter(2964): getBluetoothService() called with no   BluetoothManagerCallback
01-02 19:22:21.425: I/Choreographer(2964): Skipped 255 frames!  The application may be doing too  much work on its main thread.
01-02 19:22:21.550: V/RenderScript(2964): Application requested CPU execution
01-02 19:22:21.557: V/RenderScript(2964): 0xa1f36000 Launching thread(s), CPUs 4
01-02 19:22:24.554: D/AndroidRuntime(2964): Shutting down VM
01-02 19:22:24.555: E/AndroidRuntime(2964): FATAL EXCEPTION: main
01-02 19:22:24.555: E/AndroidRuntime(2964): Process: com.nickstaal.rnbtlock, PID: 2964
01-02 19:22:24.555: E/AndroidRuntime(2964): java.lang.IllegalStateException: Could not execute method of the activity
01-02 19:22:24.555: E/AndroidRuntime(2964):     at android.view.View$1.onClick(View.java:4007)
01-02 19:22:24.555: E/AndroidRuntime(2964):     at android.view.View.performClick(View.java:4756)
01-02 19:22:24.555: E/AndroidRuntime(2964):     at android.view.View$PerformClick.run(View.java:19749)
01-02 19:22:24.555: E/AndroidRuntime(2964):     at android.os.Handler.handleCallback(Handler.java:739)
01-02 19:22:24.555: E/AndroidRuntime(2964):     at android.os.Handler.dispatchMessage(Handler.java:95)
01-02 19:22:24.555: E/AndroidRuntime(2964):     at android.os.Looper.loop(Looper.java:135)
01-02 19:22:24.555: E/AndroidRuntime(2964):     at android.app.ActivityThread.main(ActivityThread.java:5221)
01-02 19:22:24.555: E/AndroidRuntime(2964):     at java.lang.reflect.Method.invoke(Native Method)
01-02 19:22:24.555: E/AndroidRuntime(2964):     at  java.lang.reflect.Method.invoke(Method.java:372)
01-02 19:22:24.555: E/AndroidRuntime(2964):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
01-02 19:22:24.555: E/AndroidRuntime(2964):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
01-02 19:22:24.555: E/AndroidRuntime(2964): Caused by: java.lang.reflect.InvocationTargetException
01-02 19:22:24.555: E/AndroidRuntime(2964):     at java.lang.reflect.Method.invoke(Native Method)
01-02 19:22:24.555: E/AndroidRuntime(2964):     at java.lang.reflect.Method.invoke(Method.java:372)
01-02 19:22:24.555: E/AndroidRuntime(2964):     at android.view.View$1.onClick(View.java:4002)
01-02 19:22:24.555: E/AndroidRuntime(2964):     ... 10 more
01-02 19:22:24.555: E/AndroidRuntime(2964): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(int)' on a null object reference
01-02 19:22:24.555: E/AndroidRuntime(2964):     at  com.nickstaal.rnbtlock.MainActivity.unlockDoor(MainActivity.java:172)
01-02 19:22:24.555: E/AndroidRuntime(2964):     at com.nickstaal.rnbtlock.MainActivity.openDoor(MainActivity.java:57)
01-02 19:22:24.555: E/AndroidRuntime(2964):     ... 13 more

Upvotes: 1

Views: 1826

Answers (1)

Nick S.
Nick S.

Reputation: 21

After doing quite a lot of research I managed to find a working solution. I changed the code of lockDoor() and unlockDoor() to this:

public void unlockDoor() throws IOException {
     try {
            mmOutStream = mmSocket.getOutputStream();
        } catch (IOException e) {}

        String message = "1";

        byte[] msgBuffer = message.getBytes();

        try {
            mmOutStream.write(msgBuffer);
        } catch (IOException e) {}
    } 




 public void lockDoor() throws IOException {
     try {
            mmOutStream = mmSocket.getOutputStream();
        } catch (IOException e) {}

        String message = "0";

        byte[] msgBuffer = message.getBytes();

        try {
            mmOutStream.write(msgBuffer);
        } catch (IOException e) {}
    } 

Thanks for trying to help anyways, I'm sure I'll come across other problems later on. :)

Upvotes: 1

Related Questions