Hardware Reference
In-Depth Information
is off, it uses Android intents to send a request to the user to turn Bluetooth on and then
exits the app. The next time the app restarts, if Bluetooth is on, it can proceed.
Once the code gets past that last check, it can initialize the BleWrapper , which opens up
the Bluetooth interface and gets an instance of the Android Bluetooth adapter. Once
you have this, you can access the BLE radio and protocol functions.
Finally, you need to handle one last Android app lifecycle method: onPause() . This
method gets called whenever context is lost, the device goes to sleep, or the app is shut
down:
@Override
protected void onPause () {
super . onPause ();
mBleWrapper . diconnect ();
mBleWrapper . close ();
}
In our example, the Android device will act as a GAP central and a GATT client and
the SensorTag device will be a GAP peripheral and a GATT server (see “Roles” on page
36 and “Roles” on page 51 ).
Once again, the boilerplate code is kept on top. After that, it calls two methods from the
wrapper. The first method allows you to “diconnect” (sic) our Android device from the
remote device. This actually disconnects from the remote peripheral device and also
issues a call to the uiDeviceDisconnected() callback method. If you need to handle
anything after you disconnect from the peripheral, you'd override the uiDeviceDiscon
nected() callback. Finally, we close the BleWrapper , which closes the local GATT client
and central completely.
Connecting to a Remote Device
Now that the initialization and cleanup are out of the way, we can focus on the real meat
of the code. The principle tasks associated with BLE you'll need to handle are scanning
and connecting to remote devices, communicating data, and performing any necessary
management or security tasks.
When initiating a scanning procedure (see “Advertising and Scanning” on page 19 ), you
let the BLE library know that you want to be notified of any advertising packets it receives
from remote devices. Whenever a device is found, the BleWrapper makes a call to the
uiDeviceFound() callback function with information about the device. You can use the
device information to decide if it's the device you want to connect to.
For scanning, you're going to need to create two buttons: the first to start the scan and
the second to stop it. To create the buttons, you'll need to go into the project's /res/
menu directory and edit the main.xml file. If you double-click that file, Eclipse will take
Search WWH ::




Custom Search