[Java for Android App Development] microzee:
I would like to thank everyone for all the help. The amount of replies just shows how useful this forum is to beginning devs.

Anyways, after three days of straight research, I pumped out a working app. Now I would like for someone to review my code and see if anything can be revised or done better, as I really don't know what I'm doing.

The main activity:

package com.example.bluetooth;

//Import needed files
import java.util.ArrayList;
import java.util.Set;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

//Main activity class
public class MainActivity extends ActionBarActivity {

//Creates object variables
private Button list, disconnect;
private ListView lv;
private BluetoothAdapter BA;
private Set<BluetoothDevice> pairedDevices;
private Toast toast;

@Override
//on create
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//creates variables containing id's
list = (Button) findViewById(R.id.button1);
disconnect = (Button) findViewById(R.id.button2);
lv = (ListView) findViewById(R.id.listView1);

//When you click the list item
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object obj = lv.getAdapter().getItem(position);
String str = obj.toString();
int strStartIndex = str.indexOf("\n");
str = str.substring(strStartIndex + 1);

//check to see if bluetooth is enabled before
//starting the service
if (BA.isEnabled()) {
toast = Toast.makeText(getApplicationContext(),
"Connecting to: " + str, Toast.LENGTH_SHORT);
toast.show();
//connects to item you tapped, creating a service
Intent btConnectIntent = new Intent(MainActivity.this, ForegroundService.class);
btConnectIntent.setAction("btConnect");
btConnectIntent.putExtra("mac", str);
startService(btConnectIntent);
}
//Bluetooth isn't enabled
else{
toast = Toast.makeText(getApplicationContext(),
"Bluetooth must be enabled!", Toast.LENGTH_SHORT);
toast.show();
}

}
});
}

//When you click the list button
public void list(View view) {
//Get bluetooth adapter and set it to BA
BA = BluetoothAdapter.getDefaultAdapter();
//Make sure device has bluetooth
if (BA != null) {
//See if bluetooth is enabled and if it is, get the list of paired devices
if (BA.isEnabled()) {
bluetoothList();
}
//If bluetooth isn't enabled, enable it
else {
//Start new intent to see if they turn on bluetooth
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
toast = Toast.makeText(getApplicationContext(),
"Bluetooth must be on!", Toast.LENGTH_SHORT);
toast.show();
}
}
//Device doesn't have a bluetooth adapter
else {
toast = Toast.makeText(getApplicationContext(),
"Your device doesn't support bluetooth!", Toast.LENGTH_SHORT);
toast.show();
}

}

//Waiting to see if they turn on bluetooth
//If they do, show list
public void onActivityResult(int requestCode, int resultCode, Intent turnOn) {
//if it turned on successfully, get the list of paired bluetooth devices
if (resultCode == RESULT_OK) {
bluetoothList();
}
}

//When they click disconnect button, stop the service
public void disconnect(View view) {
Intent stopIntent = new Intent(MainActivity.this, ForegroundService.class);
stopIntent.setAction("stopForeground");
startService(stopIntent);
}

//Method that gets the list of paired devices
private void bluetoothList() {
pairedDevices = BA.getBondedDevices();

ArrayList list = new ArrayList();
for (BluetoothDevice bt : pairedDevices)
list.add(bt.getName() + "\n" + bt.getAddress());

//create array adapter
final ArrayAdapter adapter = new ArrayAdapter
(this, android.R.layout.simple_list_item_1, list);
//connect our list view to the adapter
lv.setAdapter(adapter);

toast = Toast.makeText(getApplicationContext(),
"Showing paired devices", Toast.LENGTH_SHORT);
toast.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;
}
}

And the foreground service:

package com.example.bluetooth;

//Import needed files

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

//Service class
public class ForegroundService extends Service {
int FOREGROUND_ID = 1997;
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private BluetoothAdapter BA;
private BluetoothSocket BS;
private StringBuilder sb = new StringBuilder();
private Toast toast;
private String mac;
private Handler h;

//When the service is created
//Doesn't run again unless service is totally killed
public void onCreate() {
//Set service as foreground service
startForeground(FOREGROUND_ID, buildForegroundNotification());
toast = Toast.makeText(getApplicationContext(), "Service started", Toast.LENGTH_SHORT);
toast.show();

//Get bluetooth adapter and set it to BA
BA = BluetoothAdapter.getDefaultAdapter();

//Create a handler to listen for data
//New threads send data to this handler
//If it receives data, show it in a toast
h = new Handler() {
public void handleMessage(Message msg) {
Bundle bundle = msg.getData();
toast = Toast.makeText(getApplicationContext(), bundle.getString("str"), Toast.LENGTH_SHORT);
toast.show();
}

;
};
}

//Called every time an activity runs startService
public int onStartCommand(Intent intent, int flags, int startId) {
//When an activity starts the service, it sends an action
//Check if action = btConnect
if (intent.getAction().equals("btConnect")) {
//Make sure there's not already an open socket
if (BS == null) {
//Creates a bundle to get data
Bundle extras = intent.getExtras();
//Extracts data from bundle, in this case the mac address
mac = extras.getString("mac");

//Creates another handler to listen for data
final Handler mHandler = new Handler();

//A runnable called when can't connect
final Runnable hUnsuccessful = new Runnable() {
public void run() {
toast = Toast.makeText(getApplicationContext(),
"Unsuccessful", Toast.LENGTH_SHORT);
toast.show();
toast = Toast.makeText(getApplicationContext(), "Service stopped", Toast.LENGTH_SHORT);
toast.show();
//Kill the service
stopForeground(true);
stopSelf();
}
};

//A runnable called when successfully connected
final Runnable hSuccessful = new Runnable() {
public void run() {
//Run method using socket BS
//Method listens for data coming across socket
ConnectedThread(BS);
toast = Toast.makeText(getApplicationContext(),
"Successful", Toast.LENGTH_SHORT);
toast.show();
}
};
//Creates a new thread since BS.connect
//will block the main thread otherwise
Thread thread = new Thread() {
public void run() {

//Creates BluetoothDevice from the mac address
//mac address was passed to service in onStartCommand
BluetoothDevice device = BA.getRemoteDevice(mac);
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
// MY_UUID is the app's UUID string, also used by the server code
BS = device.createRfcommSocketToServiceRecord(MY_UUID);
}
//If it can't create a socket
catch (IOException e) {
//Send unsuccessful to the handler
mHandler.post(hUnsuccessful);
return;
}

// Cancel discovery because it will slow down the connection
BA.cancelDiscovery();
//Try to connect
try {
BS.connect();
}
// Unable to connect, send a toast and close the socket
catch (IOException connectException) {
//Try to close socket
try {
if (BS != null) {
BS.close();
BS = null;
}
//Send unsuccessful to the handler
mHandler.post(hUnsuccessful);
return;
}
//Can't close socket, do nothing
catch (IOException closeException) {
}
}
//If it made it to here, it was successful
//Send successful to the handler
mHandler.post(hSuccessful);
}
};
//Start the thread
thread.start();
}
//if BS != null
//Currently connected to a socket
else {
toast = Toast.makeText(getApplicationContext(), "You must disconnect first", Toast.LENGTH_SHORT);
toast.show();
}
}

//When an activity starts the service, it sends an action
//Check if action = stopForeground
if (intent.getAction().equals("stopForeground")) {
toast = Toast.makeText(getApplicationContext(), "Service stopped", Toast.LENGTH_SHORT);
toast.show();
//Try to close socket if one is open
try {
if (BS != null) {
BS.close();
BS = null;
toast = Toast.makeText(getApplicationContext(),
"Bluetooth device disconnected", Toast.LENGTH_SHORT);
toast.show();
}
}
//If it can't close it, do nothing
catch (IOException e) {
}
//Kill the service
stopForeground(true);
stopSelf();
}
//Service should stay alive, unless we kill it
return START_STICKY;
}

//Method to create foreground notification
private Notification buildForegroundNotification() {
NotificationCompat.Builder notification = new NotificationCompat.Builder(this);

notification.setOngoing(true);
notification.setContentTitle("Bluetooth Service")
.setContentText("Service to listen for bluetooth data")
.setSmallIcon(android.R.drawable.sym_def_app_icon)
.setPriority(Notification.PRIORITY_MIN);
return (notification.build());
}

//Method to create a notification
private void notification(String title, String text) {
int mId = 0;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(android.R.drawable.sym_def_app_icon)
.setContentTitle(title)
.setContentText(text)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.setPriority(NotificationCompat.PRIORITY_MAX);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(alarmSound);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
}

//Looping method to listen for data over bluetooth socket
private void ConnectedThread(BluetoothSocket socket) {
final InputStream mmInStream;
InputStream tmpIn = null;

// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
} catch (IOException e) {
}

mmInStream = tmpIn;

//Create new thread as it will otherwise block main thread
Thread thread = new Thread() {
public void run() {
byte[] buffer = new byte[256]; //buffer store for the stream
int bytes; //bytes returned from read()

//Keep listening to the InputStream until an exception occurs
while (true) {
try {
Message msg = new Message();
Bundle bundle = new Bundle();
// Read from the InputStream
bytes = mmInStream.read(buffer); // Get number of bytes and message in "buffer"
String strIncom = new String(buffer, 0, bytes);
sb.append(strIncom);
int endOfLineIndex = sb.indexOf("\r\n");
if (endOfLineIndex > 0) {
String sbprint = sb.substring(0, endOfLineIndex);
sb.delete(0, sb.length());
bundle.putString("str", sbprint);
msg.setData(bundle);
h.sendMessage(msg);
notification("Sensor Data:", sbprint);
}
}
//If InputStream gets interrupted
catch (IOException e) {
Message msg = new Message();
Bundle bundle = new Bundle();
bundle.putString("str", "Lost connection");
msg.setData(bundle);
h.sendMessage(msg);
notification("Error", "Lost connection");
Intent stopIntent = new Intent(getApplicationContext(), ForegroundService.class);
stopIntent.setAction("stopForeground");
startService(stopIntent);
break;
}
}
}
};
//Start the thread
thread.start();
}

//Required
public IBinder onBind(Intent intent) {
return null;
}
}