I am currently working on a project where i'm building an app that uses Bluetooth to send commands to my Arduino Uno and everything is working perfectly except for the actual sending of data.
Whenever I try to send data it throws a Null Pointer Exception and what I know of these exceptions is that they are thrown when something wasn't initialized or cannot be found (Correct me if i'm wrong). So I think the problem is that my connectedThread wasn't properly initialized but I don't know where to do this and what to do.
There is some other code there for speech-to-text so just ignore that.
I would really appreciate any help or advice.
Thank you so much!
My Main_Activity.java:
package marggraffd.developer.com.robot;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.speech.RecognizerIntent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Set;
import java.util.UUID;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {
public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private static final int SUCCESS_CONNECT = 0;
private static final int MESSAGE_READ = 1;
private static final String TAG = null;
Toolbar toolbar;
FloatingActionButton fab;
Button forward, reverse, left, right, stop;
private final int REQ_CODE_SPEECH_INPUT = 100;
TextView speech;
String text;
ArrayAdapter<String> listAdapter;
ListView listView;
ArrayList<String> pairedDevices;
ArrayList<BluetoothDevice> devices;
private BluetoothAdapter btAdapter;
Set<BluetoothDevice> devicesArray;
IntentFilter filter;
BroadcastReceiver receiver;
ConnectedThread connectedThread = null;
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case SUCCESS_CONNECT:
ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket) msg.obj);
Toast.makeText(getApplicationContext(), getResources().getString(R.string.bluetooth_success), Toast.LENGTH_LONG).show();
String s = "Successfully Connected";
connectedThread.write(s);
break;
case MESSAGE_READ:
byte[] readBuff = (byte[]) msg.obj;
String string = new String(readBuff);
Toast.makeText(getApplicationContext(), string, Toast.LENGTH_LONG).show();
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
init();
btAdapter = BluetoothAdapter.getDefaultAdapter();
if (btAdapter == null) {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.error2), Toast.LENGTH_LONG).show();
} else {
if (!btAdapter.isEnabled()) {
turnOnBT();
}
getPairedDevices();
startDiscovery();
}
}
private void startDiscovery() {
btAdapter.cancelDiscovery();
btAdapter.startDiscovery();
}
private void turnOnBT() {
Intent intent = new Intent((BluetoothAdapter.ACTION_REQUEST_ENABLE));
startActivityForResult(intent, 1);
}
private void getPairedDevices() {
devicesArray = btAdapter.getBondedDevices();
if (devicesArray.size() > 0) {
for (BluetoothDevice device : devicesArray) {
pairedDevices.add(device.getName());
}
}
}
private void init() {
fab = (FloatingActionButton) findViewById(R.id.fab);
forward = (Button) findViewById(R.id.bForward);
reverse = (Button) findViewById(R.id.bReverse);
left = (Button) findViewById(R.id.bLeft);
right = (Button) findViewById(R.id.bRight);
stop = (Button) findViewById(R.id.bStop);
speech = (TextView) findViewById(R.id.textView3);
listView = (ListView) findViewById(R.id.listView);
listView.setOnItemClickListener(this);
listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 0);
listView.setAdapter(listAdapter);
pairedDevices = new ArrayList<String>();
filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
devices = new ArrayList<BluetoothDevice>();
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
devices.add(device);
String s = "";
for (int a = 0; a < pairedDevices.size(); a++) {
if (device.getName().equals(pairedDevices.get(a))) {
s = "(Paired)";
break;
}
}
listAdapter.add(device.getName() + " " + s + " " + "\n" + device.getAddress());
} else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
} else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
if (btAdapter.getState() == btAdapter.STATE_OFF) {
turnOnBT();
}
}
}
};
registerReceiver(receiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
registerReceiver(receiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(receiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(receiver, filter);
fab.setOnClickListener(this);
stop.setOnClickListener(this);
buttons();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bStop:
connectedThread.write("e");
break;
case R.id.fab:
speech();
break;
}
}
private void speech() {
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
i.putExtra(RecognizerIntent.EXTRA_PROMPT, getString(R.string.speech_prompt));
try {
startActivityForResult(i, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(), getString(R.string.error), Toast.LENGTH_LONG).show();
startActivityForResult(i, REQ_CODE_SPEECH_INPUT);
}
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQ_CODE_SPEECH_INPUT && resultCode == RESULT_OK) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
speech.setText(result.get(0));
text = speech.getText().toString();
if (text == getString(R.string.forward)) {
Toast.makeText(getApplicationContext(), "Forward", Toast.LENGTH_LONG).show();
} else if (text == getString(R.string.reverse)) {
Toast.makeText(getApplicationContext(), "Reverse", Toast.LENGTH_LONG).show();
} else if (text == getString(R.string.left)) {
Toast.makeText(getApplicationContext(), "Left", Toast.LENGTH_LONG).show();
} else if (text == getString(R.string.right)) {
Toast.makeText(getApplicationContext(), "Right", Toast.LENGTH_LONG).show();
} else if (text == getString(R.string.stop)) {
Toast.makeText(getApplicationContext(), "Stop", Toast.LENGTH_LONG).show();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (btAdapter.isDiscovering()) {
btAdapter.cancelDiscovery();
}
if (listAdapter.getItem(position).contains("(Paired)")) {
BluetoothDevice selectedDevices = devices.get(position);
ConnectThread connect = new ConnectThread(selectedDevices);
connect.start();
} else {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.bluetooth_unpaired), Toast.LENGTH_LONG).show();
}
}
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
BluetoothSocket tmp = null;
mmDevice = device;
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
}
mmSocket = tmp;
}
public void run() {
btAdapter.cancelDiscovery();
try {
mmSocket.connect();
} catch (IOException connectException) {
try {
mmSocket.close();
} catch (IOException closeException) {
}
return;
}
mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
}
}
}
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[9600];
int bytes;
while (true) {
try {
buffer = new byte[9600];
bytes = mmInStream.read(buffer);
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
break;
}
}
}
public void write(String message) {
byte[] msgBuffer = message.getBytes();
try {
mmOutStream.write(msgBuffer);
} catch (IOException e) {
e.printStackTrace();
}
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
}
}
}
private void buttons() {
forward.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
try {
connectedThread.write("e");
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Forward", Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_UP:
connectedThread.write("e");
Toast.makeText(getApplicationContext(), "Stop", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
reverse.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
connectedThread.write("d");
Toast.makeText(getApplicationContext(), "Reverse", Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_UP:
connectedThread.write("e");
Toast.makeText(getApplicationContext(), "Stop", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
left.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
connectedThread.write("b");
Toast.makeText(getApplicationContext(), "Left", Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_UP:
connectedThread.write("e");
Toast.makeText(getApplicationContext(), "Stop", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
right.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
connectedThread.write("c");
Toast.makeText(getApplicationContext(), "Right", Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_UP:
connectedThread.write("e");
Toast.makeText(getApplicationContext(), "Stop", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
}
}
My activity_main.xml:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ECEFF1"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:background="#009688"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.amdroic.co,/apk/res-auto"
android:id="@+id/controller"
android:layout_width="fill_parent"
android:layout_height="320dp"
android:layout_below="@+id/toolbar"
android:layout_marginTop="10dp"
card_view:cardCornerRadius="4dp">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/bStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/ic_stop_black_24dp" />
<Button
android:id="@+id/bLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/bStop"
android:layout_toStartOf="@+id/bStop"
android:background="@drawable/ic_keyboard_arrow_left_black_24dp" />
<Button
android:id="@+id/bRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/bStop"
android:layout_toRightOf="@+id/bStop"
android:background="@drawable/ic_keyboard_arrow_right_black_24dp" />
<Button
android:id="@+id/bForward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/bStop"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="@drawable/ic_keyboard_arrow_up_black_24dp" />
<Button
android:id="@+id/bReverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/bStop"
android:layout_centerHorizontal="true"
android:background="@drawable/ic_keyboard_arrow_down_black_24dp" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Controller"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/card2"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_below="@+id/controller"
android:layout_marginTop="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="Bluetooth"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/textView2"
android:clickable="true"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/cardView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/card2"
android:layout_marginTop="10dp"
android:checked="false">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="Monitor"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
</ScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:src="@drawable/ic_mic_black_48dp"
app:backgroundTint="#009688"
app:elevation="6dp"
app:fabSize="normal" />
My logcat:
10-01 18:32:00.466 21197-21197/marggraffd.developer.com.robot W/System.err: java.lang.NullPointerException
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at marggraffd.developer.com.robot.MainActivity$3.onTouch(MainActivity.java:429)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.View.dispatchTouchEvent(View.java:8130)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2112)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2112)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2112)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2112)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2112)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2112)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2112)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422)
10-01 18:32:00.486 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2112)
10-01 18:32:00.486 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422)
10-01 18:32:00.486 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2112)
10-01 18:32:00.486 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422)
10-01 18:32:00.486 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2112)
10-01 18:32:00.486 21197-21197/marggraffd.developer.com.robot W/System.err: at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2295)
10-01 18:32:00.486 21197-21197/marggraffd.developer.com.robot W/System.err: at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1622)
10-01 18:32:00.486 21197-21197/marggraffd.developer.com.robot W/System.err: at android.app.Activity.dispatchTouchEvent(Activity.java:2565)
10-01 18:32:00.486 21197-21197/marggraffd.developer.com.robot W/System.err: at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
10-01 18:32:00.486 21197-21197/marggraffd.developer.com.robot W/System.err: at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)