Hardware Reference
In-Depth Information
The line added in this file will hold the instance of the BleWrapper class, which will also
contain the methods you'll be using to access the underlying Android BLE library. In
your constructor, you'll also be adding various callbacks that you'll need to handle events
from the Android BLE library.
In the previous code snippet, notice an automatically generated function called
onCreate() . This function is called when the application first starts and you'll use it to
intialize things that need to be initialized on startup and only once. This includes mem‐
bers that don't need to be reinitialized if the device goes to sleep or loses context and
then comes back.
The Android documentation actually outlines an “activity lifecycle,” which describes
the method as an app goes through different stages of its “life.” For example, when an
app is started, three separate lifecycle methods get called: onCreate() , onStart() , and
onResume() . Initializing variables inside an app class occurs in onCreate() when they're
initialized only once, and in onResume() when they need to be initialized again if the
device goes to sleep or loses context.
The documentation provides more information on this activity lifecycle, but it's an
important concept to know, because you'll be doing part of the init routines in on
Create() and part of them in onResume() . You'll also need to handle cleanup when you
shut down, which will be in onPause() or onStop() .
Inside the onCreate() method, you'll see some boilerplate code generated by the project
wizard. Beneath that, you'll be adding more code to initialize your mBleWrapper object
and perform other init tasks:
@Override
protected void onCreate ( Bundle savedInstanceState )
{
super . onCreate ( savedInstanceState );
setContentView ( R . layout . activity_main );
mBleWrapper = new BleWrapper ( this , new BleWrapperUiCallbacks . Null ()
{
});
if ( mBleWrapper . checkBleHardwareAvailable () == false )
{
Toast . makeText ( this , "No BLE-compatible hardware detected" ,
Toast . LENGTH_SHORT ). show ();
finish ();
}
}
The preceding code allocates a new BleWrapper object to the mBleWrapper member
variable (which previously was null) by instantiating it and calling its constructor. When
instantiating the wrapper, you also need to add the BLE callbacks as an argument to the
Search WWH ::




Custom Search