As promised, we are going to start going into the code, right Jacob? >> Yep. >> Okay, you excited? >> Yes I am. >> Okay, get pumped. Let's start. >> All right, let's dive right in. All right so what we have here is the BluetoothManager class and what this will do is it has three different types of threads. An AcceptThread that will be used to listen for incoming connections, a ConnectThread that will try to connect the two devices together and a ConnectedThread that will be in charge of maintaining the connection between the two devices. Now in this BluetoothManager class we have a bunch of different aspects for it. So we have the default constructor, we have some getters and setter methods. So, the state that this is referring to will be what the class will use internally to determine whether or not it's connecting or it's listening for something. We also have these listen, connect, and connected methods. And what these will do is because it is a BluetoothManager, it will make sure that the threads that are currently, or the other threads that are available are cancelled and are stopped before we execute the other codes. So in this listen we will first cancel the different threads, and then start accepting thread. In connect we're gonna cancel the ConnectedThreads and then we're gonna start the ConnectThread. And in connected, we will do a similar process where we cancel all the threads and then start the ConnectedThread. Pretty straight forward I think, in terms of what it's trying to do. We also have this stop method. And what this will do, is it'll just revert the manager back to the idle state and just let the different classes that it's using know that it's currently not doing anything. We also have this write method. And what this write method will do is, it'll just take in an input and then it'll send it to the ConnectedThread only if we are connected. So, you don't wanna write out to some random output that doesn't exist cuz then your program will crash. And then we have a couple of UI notifications that will just change the UI and perhaps give you a toast message and what not. These are helper methods and finally we have the AcceptThread. So the AcceptThread as I said earlier is the thread that will be in charge of listening for incoming connections. So in its constructor what we first try to do is if it's, we choose either a secure or insecure connection depending on what's available and we use a Bluetooth adapter to listen using a Rf com with service record. Now, after that, because this is a thread it will fire off of this run method. And what the run method will do is it will continuously search for a socket. So while we're not connected, we're going to continuously try to accept a server socket. And once that socket is done, so that would mean that the socket isn't null, we would then delegate to the switch case what we would like to do next. Whether we want to close the socket if it's already connected or try the connection because we need to, or seal the connection because we need to, we haven't done that. [COUGH] Then we have the ConnectThread. So the ConnectThread will be in charge of making sure that we try a connection. So whenever you try to connect from my phone to the Dragon board, this thread will fire off. Now similarly to the AcceptThread as opposed to listening. This will create a socket, an Rf com socket so that you can connect to it. And this run thread will just try and make an attempt to connect with the chosen Bluetooth Device. Now if it doesn't succeed then it'll just close itself. However if it does succeed as we see here, we will call the Bluetooth Manager's connected method, which will port us to ConnectedThread. So ConnectedThread will be the thread that maintains the connection between the two Bluetooth devices. What we first try to do in the constructor is instantiate the input and output stream, and so this is the way for my application on the phone or the remote to contact the receiver. So what we have in the run method is the run method for the ConnectedThread will be in charge of listening to the inputs. So here, while this thread is running, we're gonna try to get something from the buffer. [COUGH] Or from the input stream, put it in the buffer, and then delegate the task to the GpioInputParser. Now if it fails, then we have a couple of UI changes that we have to let the user know of this error, there was an error. Now we have this write method that will just write to the output buffer. So the run method for ConnectedThread is the one that's listening for input, and the write method will be the one that sends it to the output. And this is only instantiated, or this is only called when the user clicks on the button. And with that, that sums up the Bluetooth manager. It's a lot of information to take in, so I highly suggest you if there was a few bits of information that you weren't sure of or that I kind of glossed over, to look into the code because it's pretty, I would say, well documented, in a sense. So, yeah.