Hardware Reference
In-Depth Information
constructor. This is where you add code to handle notification events that occur from
the BLE stack.
In software programming, a callback is a reference to executable code passed in as an
argument to a function, where the function is expected to callback the code after it's
done processing something. For now, we won't add any callbacks to the constructor, but
we'll revisit this later.
After the constructor, the code performs a check to make sure the BLE hardware is
available to the system. If it's not, a popup message window called “Toast” notifies the
user about the missing BLE hardware and then shuts down the app.
That completes the code needed to execute when the app starts up in the onCreate()
method. Now, to move to the onResume() method, which is where the code goes when
it starts up and whenever it wakes from sleep:
@Override
protected void onResume () {
super . onResume ();
// check for Bluetooth enabled on each resume
if ( mBleWrapper . isBtEnabled () == false )
{
// Bluetooth is not enabled. Request to user to turn it on
Intent enableBtIntent = new Intent ( BluetoothAdapter .
ACTION_REQUEST_ENABLE );
startActivity ( enableBtIntent );
finish ();
}
// init ble wrapper
mBleWrapper . initialize ();
}
}
Eclipse provides a quick shortcut for any of these functions. Just type
the first few letters of the method name and then Ctrl+Spacebar.
Choose the function from the autocomplete list that pops up to let
Eclipse build the function for you.
Here, the boilerplate code is at the top with super.onResume() . You're going to add your
own code beneath that. In this case, you need to check if Bluetooth is enabled each time
the device comes back from a different context or from sleep. While some other program
is in use, Bluetooth might have been turned off. Not catching that change and just
assuming Bluetooth was still on would eventually cause some type of malfunction or
exception to the software. To handle this situation, if the program finds that Bluetooth
Search WWH ::




Custom Search