Hardware Reference
In-Depth Information
If the connection to the GATT server and service discovery is successful, another call‐
back is issued. This time, the callback is
uiAvailableServices()
and one of the argu‐
ments is the
service list
, which lists all the Bluetooth GATT services (see
“Services” on
page 58
for further information on services and characteristics) available on the remote
device. In order to communicate with the device, we'll need to access the services and
then the characteristics inside the services.
At this time, you could just cycle through the list and print out the services, but they're
not in a human-readable format. The services are all listed by 128-bit UUID (see
“UUIDs” on page 52
). The class library files you copied over earlier form the Bluetooth
Application Accelerator included a class called
BleNamesResolver
. This class has var‐
ious methods to resolve UUIDs into BLE names. It can resolve both services and char‐
acteristics, so this library is quite useful.
The list of known UUIDs is located in the
BleDefinedUUIDs.java
file, which you'll need
to add some proprietary service UUIDs to list later on. For now, rather than simply
scrolling through the service list and printing out the service UUIDs, you'll be resolving
them into human-readable names and printing those out (just to
logcat
for now). The
code is another callback override and goes in the
onCreate
method and inside the
mBleWrapper
constructor:
@Override
public
void
uiAvailableServices
(
BluetoothGatt
gatt
,
BluetoothDevice
device
,
List
<
BluetoothGattService
>
services
)
{
for
(
BluetoothGattService
service
:
services
)
{
String
serviceName
=
BleNamesResolver
.
resolveUuid
(
service
.
getUuid
().
\
toString
());
Log
.
d
(
"DEBUG"
,
serviceName
);
}
}
This code cycles through each element in the service list. For each service, it then con‐
verts the UUID to a string value and passes it into the
BleNamesResolver.resol
veUuid()
method. This method looks through the list of known UUIDs and, when it
finds a match for the UUID, returns the associated human-readable UUID name. It
prints that name out to
logcat
in the Eclipse IDE, as shown in
Figure 8-9
. It's also
possible to dump these directly into a textbox for viewing, but that's slightly more com‐
plicated.