Tip:
Highlight text to annotate it
X
[MUSIC PLAYING]
Hi.
My name is Fred Chung, part of Android Developer Relations.
Today we're going to talk about Bluetooth Low Energy.
In Android 4.3, we are adding built-in platform support for
Bluetooth LE.
The new Nexus 7 and the Nexus 4 are the first two Nexus
devices that are going to be Bluetooth-smart ready, making
them capable of communicating with the many Bluetooth smart
peripherals on the market today.
I'm going to do a sample app walkthrough today.
But before doing that, I'd like to walk you through some
technical concepts of Bluetooth Low Energy so that
you can make sense out of the API objects.
As its name implies, Bluetooth Low Energy uses less energy as
compared with Bluetooth Classic.
It is not uncommon to have Bluetooth Low Energy devices
that last for weeks, if not years, before needing to
recharge or replacing the battery.
The protocol itself is optimized for a small burst of
data exchange, which is ideal for applications such as
sensors, remote controllers, so on and so forth.
In contrast, Bluetooth Classic quite often is involved in
high bandwidth applications such as audio streaming.
Now let's have a look at how information is structured in
BLE applications.
BLE is based on a specification called general
attribute profile, or GATT, It governs sending and receiving
of short pieces of data known as attributes.
The Android app can be either a GATT client or a GATT server
depending on the application.
The GATT server can support a collection of services.
You can think of services as different features offered by
the device.
Each service is uniquely identified by a UUID
referenceable by your application.
Under each service, there could be a collection of
characteristics which you can perform read or write
operations depending on the BTLE profile
you're working with.
To put things in perspective, let's have a look at the heart
rate measurement profile which is one of
the standard profiles.
The heart rate sensor is the GATT server.
The Android app in this diagram is the GATT client.
The GATT server hosted on the heart rate sensor device
offers a number of services.
Under the service, there's a collection of GATT
characteristics.
There's one for providing heart rate measurements which
you can receive notification on when that indicates the
location of the sensor which you can read from.
Now that we have some basic understanding on Bluetooth Low
Energy technology, let's have a look at some code.
Let's first take a look at the Android manifest.
There's a new feature called android.hardware.bluetoothle.
You declare it if your application would only work on
devices that has that feature.
You can omit it if your application would work on a
device without that feature so that you can optionally
disable the Bluetooth Low Energy
features within your app.
Now the debugging session is up.
I'm going to go ahead and click Scan.
So I've put some break points here.
What it's trying to do is to call a
startLeScan method call.
And as you can see in the API documentation, a lot of the
BLE operation methods are asynchronous in nature.
You have to specify a callback such that when interesting
events happen, in this case, when devices are discovered,
the callback methods are invoked.
So I'm going to let this breakpoint go.
As you can see in the app, it's able to
find a couple of devices.
Let's go ahead and click on Heart Rate Sensor, which
corresponds to the test kit that I'm trying to test with.
So let's go ahead and click on it.
Now it brings you to another activity which is bound to a
background service.
And I've put a breakpoint in the service.
What it's trying to do is to connect to the GATT server
that's hosted on this device by invoking this method called
connectGatt.
Just like the previous example, since all APIs that
have to do with BLEs are asynchronous in nature, you
have to pass another callback here.
Now that we've connected to the GATT server, it's trying
to invoke this method called discoverServices by trying to
discover the available GATT services
hosted on this device.
Let's go ahead and release the breakpoint.
And now the callback on services discovered is
invoked, meaning the app has found the services that are
available on the device.
The app's going to iterate through the collection of
services found and display on the UI, as seen here.
Since this is a heart rate sensor, we're going to do some
heart rate measurements.
Let's go ahead and locate the heart rate service, expand it,
and you'll see Heart Rate Measurement characteristic.
Let's go ahead and click on it.
What happens is that since it knows that the characteristic
supports notification, it tries to enable the
notification.
So let's go ahead and release this breakpoint.
Now, as soon as it begins getting heart rate updates
reading, you'll get this callback on characteristic
change indicating that you are receiving a notification.
So we're going to go ahead and release the breakpoint.
As you can see on the screen here, we are receiving heart
rate measurement readings in this very, very tiny text.
I don't know if you can see it.
But it's updating with heart rate measurement notifications
receiving from the peripherals.
Now, I want to illustrate a very important point.
And that is whenever you are done with a particular
Bluetooth LE connection with a peripheral, you should really
call this close method so that the stack can do
it's clean up properly.
That's it for the walkthrough.
If you want to learn more, do check out the Bluetooth Low
Energy developer guide on d.android.com.
Thank you for watching.